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 these:
apple
apple
and then
[(apple,2)]
I came up whit this
import qualified Data.Map as Map
import Data.List
import System.IO
main =
do
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