比较列表长度 [英] Comparing list length

查看:75
本文介绍了比较列表长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表清单,比如说:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆