0

How do i read the following file which has columns of data with header and space or tab delimited, in Haskell 98.

How can i access each element in the input file for example if this 4 by 7 matrix, accessing [1][2] gives YR.

Also how to get the difference in dates in Haskell 98

ID YR MO DA YrM MoM DaM
100 2010 2 20 2010 8 30
110 2010 4 30 2010 9 12
112 2010 8 20 2010 10 20

2 Answers 2

4

For the former, readFile, lines, and unfoldr are your friends. For the latter, see Data.Time, which is also well documented.

Sign up to request clarification or add additional context in comments.

4 Comments

Add to this list the function "words", and you should be set to go! Props @sclv for giving hints and not just posting a solution.
I am having trouble implementing this, since i never used this language. Can you please give me some code ideas. i need to compare this language to python code, which i did already.
Daniel's right by the way, you can replace unfoldr by words and then everything is extremely straightforward. To be really exhaustive, map will be useful as well.
readFile gives you back a string. lines splits a string into lines (i.e. a list of strings). words splits on whitespace rather than just newlines, so it can give you a list of words in a line. Those and many more are documented here: haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/…
1

This is a short demo using readFile, (!!), map, words, and lines:

> f <- readFile "test.txt"
> ((map words $ lines f) !! 0) !! 1
"YR"

It's probably not exactly what you want, but should get you on the right track. Keep in mind that this is implementing your solution using lists rather than an array.


Forgot to mention, stick your matrix data into a file named test.txt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.