This is my FIRST haskell program! “wordCount” takes in a list of words and returns a tuple with with each case-insensitive word paired with its usage count. Any suggestions for improvement on either code readability or performance?
import List;
import Char;
uniqueCountIn ns xs = map (\x -> length (filter (==x) xs)) ns
nubl (xs) = nub (map (map toLower) xs) -- to lowercase
wordCount ws = zip ns (uniqueCountIn ns ws)
where ns = nubl ws
Congrats on your first program!
For cleanliness: lose the semicolons. Use the new hierarchical module names instead (
Data.List,Data.Char). Add type signatures. As you get more comfortable with function composition, eta contract your function definitions (remove rightmost arguments). e.g.If you want to be really rigorous, use explicit import lists:
For performance: use a
Data.Mapto store the associations instead ofnubandfilter. In particular, seefromListWithandtoList. Using those functions you can simplify your implementation and improve performance at the same time.