[Haskell-beginners] Counting Fruits
builes.adolfo at googlemail.com
Thu Feb 19 18:15:33 EST 2009
Hello everyone ,
I wanted to do a small program which read a txt with fruit's name in each
line and then print how many fruits I have of each type. something like
I came up whit this
import qualified Data.Map as Map
file <- readFile "fruits.txt"
let answer = proccessFile $ lines file
putStrLn (show answer)
proccessFile :: [String] -> [(String,Int)]
proccessFile file = Map.toAscList $ parseFile Map.empty file
where parseFile fruits  = fruits
parseFile fruits_map (x:xs) = parseFile (Map.insertWith (+)
x 1 fruits_map) xs
It works, but I would like to know how would you do it ?, Share different
points of view, different code. Was it a good idea to use a Map ?, Did I
separate the code in a proper way, I mean pure - impure ? How can we
improve the performance ?
Best Regards for everyone.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners