Haskell - Selectively Adding Lists

后端 未结 1 1784
甜味超标
甜味超标 2021-01-16 05:21

I have the following type:

type Rating = (String, Int)
type Film = (String, String, Int, [Rating])

testDatabase :: [Film]
testDatabase = [(\"Director 1\",\"         


        
相关标签:
1条回答
  • 2021-01-16 06:21

    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...

    0 讨论(0)
提交回复
热议问题