Hello everyone , <br>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 these:<br> apple<br> apple<br><br>and then <br><br>
[(apple,2)]<br>
<br>I came up whit this<br><pre>import qualified Data.Map as Map<br>import Data.List<br>import System.IO<br><br>main =<br> do<br> file <- readFile "fruits.txt"<br> let answer = proccessFile $ lines file<br>
putStrLn (show answer)<br><br>proccessFile :: [String] -> [(String,Int)]<br>proccessFile file = Map.toAscList $ parseFile Map.empty file<br> where parseFile fruits [] = fruits<br> parseFile fruits_map (x:xs) = parseFile (Map.insertWith (+) x 1 fruits_map) xs </pre>
<br>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 ?<br>
<br>Best Regards for everyone.<br><br><br>Adolfo<br> <br>