A frequency map of values is often useful to detect outliers. We can use it to identify frequencies that seem out of the ordinary. In this recipe, we will be counting the number of different colors in a list.
Create a new file, which we will call Main.hs
, and perform the following steps:
We will use the
group
andsort
functions fromData.List
:import Data.List (group, sort)
Define a simple data type for colors:
data Color = Red | Green | Blue deriving (Show, Ord, Eq)
Create a list of these colors:
main :: IO () main = do let items = [Red, Green, Green, Blue, Red, Green, Green]
Implement the frequency map and print it out:
let freq = map (\x -> (head x, length x)) . group . sort $ items print freq