Haskell: surprising behavior of “groupBy”

后端 未结 4 1048
無奈伤痛
無奈伤痛 2020-12-30 04:07

I\'m trying to figure out the behavior of the library function groupBy (from Data.List), which purports to group elements of a list by an \"equality test\" function passed i

4条回答
  •  孤独总比滥情好
    2020-12-30 04:39

    I'd just like to point out that the groupBy function also requires your list to be sorted before being applied.

    For example:

    equalityOp :: (a, b1) -> (a, b2) -> Bool
    equalityOp x y = fst x == fst y
    
    testData = [(1, 2), (1, 4), (2, 3)]
    
    correctAnswer = groupBy equalityOp testData == [[(1, 2), (1, 4)], [(2, 3)]]
    
    otherTestData = [(1, 2), (2, 3), (1, 4)]
    
    incorrectAnswer = groupBy equalityOp otherTestData == [[(1, 2)], [(2, 3)], [(1, 4)]]
    

    This behaviour comes about because groupBy is using span in its definition. To get reasonable behaviour which doesn't rely on us having the underlying list in any particular order we can define a function:

    groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
    groupBy' eq []     = []
    groupBy' eq (x:xs) = (x:similarResults) : (groupBy' eq differentResults)
        where similarResults   = filter (eq x) xs
              differentResults = filter (not . eq x) xs
    

提交回复
热议问题