[Haskell-beginners] Simple data summarization

Roland Zumkeller roland.zumkeller at gmail.com
Tue Mar 10 11:26:32 EDT 2009


Hi Andy,

Here is a function that parses a comma-separated list of strings:

> uncommas :: String -> [String]
> uncommas s = case break (==',') s of
>               (w,[]) -> [w]
>               (w,_:s') -> w : uncommas s'

We can then sum over the 4th column like this:

> main = putStrLn . show . sum . map (read . (!!4) . uncommas)
>        . tail . lines =<< getContents

This program is best read backwards: "getContents" gives stdin as a
string and "lines" breaks it into lines. The (!!) function yields the
nth element of a list. "read" and "show" convert between strings and
integers.

Best,

Roland


More information about the Beginners mailing list