I have the following type:
type Rating = (String, Int)
type Film = (String, String, Int, [Rating])
testDatabase :: [Film]
testDatabase = [(\"Director 1\",\"
There are many useful functions in Data.List for data analysis.
In particular, groupBy is super useful:
> :t groupBy
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
Given an equality function, group a list into buckets.
A function to access the directory:
> let fst4 (x,_,_,_) = x
Then, sort on the director name, and bucket (group) by that director.
> let db0 = sortBy (comparing fst4) testDatabase
> let db1 = groupBy ((==) `on` fst4) db0
[ [("Director 1","Film 1",2012, [("TestRat",8)])]
, [("Director 2","Film 2",2 ,[])]
]
groupBy is very useful...