比较列表长度
我有一个列表列表,比方说:
import Data.List
xs = [[1,2], [1,2,3], [2,3]]
我想获取包含最多项目的内部列表,在本例中为 [1,2,3]
。
我正在尝试使用 Data.List
库中的 maximumBy
函数:
maximumBy (compare `on` length) xs
但出现以下错误:不在范围“on”
谁能告诉我出了什么问题,或者您是否有更好的方法来获取列表?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
on
在 数据中定义.Function,所以你需要导入它。或者,您可以使用 数据.Ord:
on
is defined in Data.Function, so you need to import that.Alternatively, you can use
comparing
from Data.Ord:虽然使用
maximumBy
与比较长度
或比较 `on` 长度
对于短列表来说效果很好,但请注意,这不是一个非常好的方法。如果列表很长,则这是有效的解决方案,因为每次算法比较两个列表时,它都会重新计算它们的长度。例如,如果我们有一个很长的第一个列表,后面跟着许多短列表,那么使用 maximumBy 会非常慢,因为第一个列表的长度将在每一步重新计算。
我们可以通过缓存列表的长度来获得更有效的解决方案:
当然,如果您的列表很小,这可能不会产生很大的影响,但值得注意。
While using
maximumBy
withcomparing length
orcompare `on` length
will do the job just fine for short lists, note that this is not a very efficient solution if the lists are long, since each time the algorithm compares two lists, it will re-calculate their lengths.For example, if we have a very long first list, followed by many short lists, using
maximumBy
will be very slow since the length of the first list will be re-calculated at each step.We can get a more efficient solution by caching the lengths of the lists:
Of course, this might not make a big difference if your lists are small, but it's worth taking note of.
或者你可以说得更明确一点:
也许这样更容易理解。
or you can make it a bit more explicit:
maybe it's easier to understand this way.
尝试
或
或
Try
or
or
受到哈马尔解决方案的启发,但只需浏览一下列表即可:
Inspired by hammar's solution, but with just one pass thru the list: