
Don Stewart-2 wrote:
G.C.Stavenga:
Hi, I'm just started to learn Haskell. Coming from a programming contest background (where it is important to be able to solve problems in a small amount of code) I'm wondering what the best way is for simple IO.
A typical input file (in a programming contest) is just a bunch of numbers which you want to read one by one (sometimes interspersed with strings). In C/C++ this is easily done with either scanf or cin which reads data separated by spaces. In Haskell I have not found an equally satisfactionary method. The methods I know of
1) Stay in the IO monad and write your own readInt readString functions. A lot of code for something easy.
2) Use interact together with words and put the list of lexemes in a State monad and define getInt where at least you can use read.
3) Use ByteString.Char8 which has readInt (but I couldn't find a readString). But one has to put it also in a State monad.
I think that there must be standard function that can do this. What do experienced Haskellers use?
map read . lines
Thank you for the reply. But this only works for if you read only integers all on different lines. But in general you have a structure like
first line -- integer specifying the number of testcases (n) Then for each testcase a line with an integer specifying the number of edges (e) a line with e pairs of string s and int p where p is the number asociated with string s, etc.
Such a structure cannot be parsed by map read.lines What I used is "words" to tokenize and put the list in a State monad with readInt, readString, etc. functions, to mimic C code. This seems to be a lot of overkill, so there must be an simpler way _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/%28no-subject%29-tp25088427p25088830.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.