[Haskell-begin] Using read, reads...

apfelmus apfelmus at quantentunnel.de
Mon Jul 21 03:25:07 EDT 2008


Felipe Lessa wrote:
> Rafael Gustavo da Cunha Pereira Pinto  wrote:
>> -------------sample.in-------------
>> 01 02 03 04 05 06
>> 10 11 15 18 29 45
>> 19 22 10 01 23 14
>> -----------------------------------------
>>
> convert :: String -> [Int]
> convert = map read . words
> 
> read :: FilePath -> IO [[Int]]
> read fp = (map convert . lines) `fmap` readFile fp

Naming the new function  read  is a bad idea :)

> Right now I am using hGetLine and getting a string. Is is possible to
> use read to convert this in a list of Num?

As Felipe demonstrates, putting the entire file into a String via  readFile  is 
often easier than reading it piecemeal with  hGetLine  .


Regards,
apfelmus



More information about the Beginners mailing list