比较列表长度 [英] Comparing list length
问题描述
我有一个列表清单,比如说:
import Data.List
xs = [[1,2],[1,2,3],[2,3]]
我想获得大多数项目的内部列表,在这种情况下 [1,2,3]
。
我试图使用 Data.List
库中的 maximumBy
函数:
maximumBy(比较`on`长度)xs
但我得到以下错误:不在范围内'on'
任何人都可以告诉我什么是错的,或者如果你有更好的方式来获取列表?
on
在 Data.Function中定义,您需要导入。
或者,您可以使用比较来自 =http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-O rb.html#v%3acomparing> Data.Ord :
maximumBy(比较长度)xs
I have a list of lists, let's say:
import Data.List
xs = [[1,2], [1,2,3], [2,3]]
I want to get the inner list with the most items, in this case [1,2,3]
.
I'm trying to use the maximumBy
function from the Data.List
library:
maximumBy (compare `on` length) xs
but I get the following error: not in scope 'on'
Can anyone tell me what is wrong, or if you have a better way to get the list?
on
is defined in Data.Function, so you need to import that.
Alternatively, you can use comparing
from Data.Ord:
maximumBy (comparing length) xs
这篇关于比较列表长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!