[Haskell-beginners] List Manipulation

Lorenzo Isella lorenzo.isella at gmail.com
Thu Sep 9 13:12:21 EDT 2010


Dear All,
Thanks to the help I got on the mailing list, I was able to load a table 
of integers as a list where every entry is a list of integers.
To fix the ideas consider

main :: IO ()

main = do
    txt <- readFile "mydata.dat"

    let dat :: [[Integer]]

        dat = convert txt

    print dat -- this prints out my chunk of data



convert x = (map (map read . words) . lines) x


and the file mydata.txt is given by

1246191122 1336 1337
1246191142 1336 1337
1246191162 1336 1337
1246191182 1336 1337
1246191202 1336 1337
1246191222 1336 1337
1246191242 1336 1337
1246191262 1336 1337
1246191282 1336 1337
1246191302 1336 1337
1246191322 1336 1337
1246191342 1336 1337
1246191362 1336 1337
1246191382 1336 1337
1246191402 1336 1337
1246191422 1336 1337


Up to some extent, I am done (later on I found out that reading this 
into a matrix for hmatrix is a one-liner), but I would like to achieve 
more just performing standard list manipulations.
For instance, since my data files are always this simple (integers/real 
arrays of numbers)

(1) How can I tell the length of every sublist in my list?
(2) How can I e.g. stitch together the 1st element in every sublist in 
order to get the 1st column of the original data file?

Many thanks

Lorenzo


More information about the Beginners mailing list