A frequency map of values is often useful to detect outliers. We will use an existing library that does much of the work for us.
Create a new file, which we will call Main.hs
, and perform the following steps:
We will use the
fromList
andtoOccurList
functions fromData.MultiSet
:import Data.MultiSet (fromList, toOccurList)
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 = toOccurList . fromList $ items print freq
Run the code to display the frequency list:
$ runhaskell Main.hs [ (Red, 2), (Green, 4), (Blue, 1) ]