read int lists

Claus Reinke [email protected]
Tue, 30 Apr 2002 23:24:16 +0100


>   Import two space separated columns of integers from file.
> -----------
> 
> 
> 1
> 2
> 
> 
> 3
> 4
> 
> 
> -----------
> , and it should convert to  ([1,2],[3,4]) :: ([Integer], [Integer])

-- working on lines makes life easier
mapFst f (x,y)  = (map f x,y)
getListLines ls = span (/="") $ dropWhile (=="") ls
lineToInteger l = read l::Integer
readIntegers    = (mapFst lineToInteger).getListLines

-- testing, 1..3
main = do
  cts <- readFile "data"
  let (first,rest) = readIntegers $ lines cts
      (second,_)   = readIntegers $ rest
  print (first,second)

Error checking (recommended!) and generalisation to more than
two columns left as an exercise..

Hth,
Claus
--
Haskell Communities and Activities Report (May edition)
All contributions are due in this week!
http://www.haskell.org/communities/