我想使用Data.List.groupBy根據snd
元素的等同性對元組列表進行分組。
我可以這樣做:有沒有一個標準的庫解決這個Haskell問題?
groupBy (\l r -> snd l == snd r) listOfTuples
但是這讓我在比較功能太多樣板 - 尤其是因爲它可以得到更多的混亂,如果我在做一個更復雜的比較。我想這樣做:
groupBy (comparing snd) listOfTuples
,但比較的類型簽名是comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering
,所以它不會在這個例子編譯。
我也可以這樣做:
groupBy (\l r -> (comparing snd l r) == EQ) listOfTuples
但是,這並不比第一次嘗試更好。在我推出自己的產品之前,是否有標準庫解決方案來解決這個問題?
'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=on – porges
@Porges:謝謝! –