是否有针对此Haskell问题的标准库解决方案?

Mat*_*ick 5 haskell functional-programming standard-library

我想使用Data.List.groupBy根据snd元素的相等性对元组列表进行分组.
我能做到这一点:

groupBy (\l r -> snd l == snd r) listOfTuples
Run Code Online (Sandbox Code Playgroud)

但是它在比较函数中给我带来了太多的样板 - 特别是因为如果我进行更复杂的比较,它可能会变得更加混乱.我想做的事情如下:

groupBy (comparing snd) listOfTuples
Run Code Online (Sandbox Code Playgroud)

但是比较的类型签名是comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering,所以在这个例子中它不能编译.
我也可以这样做:

groupBy (\l r -> (comparing snd l r) == EQ) listOfTuples
Run Code Online (Sandbox Code Playgroud)

但这并不比第一次尝试好.在我自己推出之前,是否有针对此问题的标准库解决方案?

Dan*_*ner 15

groupBy ((==) `on` snd) listOfTuples
Run Code Online (Sandbox Code Playgroud)

我认为曾经有equating = on (==)过标准库,但我现在似乎无法找到它.

  • @Matt:Data.Function(http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Function.html#v:on)一般来说你可以搜索hoogle上的函数:http :?//www.haskell.org/hoogle/ hoogle =上 (3认同)