Haskell:删除最大的列表 [英] Haskell: Remove largest list

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

问题描述

  extract :: [高原]  - >高原

extract(x:xs)
| x == maximumBy(比较`on`长度)(x:xs)= x
|否则= (xs)
extract [] = []

现在我需要一个函数采取相同的 [Plateau] 并返回一个新的 [Plateau] >

  prune :: [Plateau]  - > [Plateau] 
prune(x:xs)
| x | x> = maximumBy(比较`on`长度)(x:xs)= []
prune [] = []

我也用 sortBy 以确保最大的列表是最后一个:

 (extract。prune)(sortBy(compare`on`长度)(平台))

我的列表

  plateaus = [01000],[01010,11010 ,10010] [00101],[01101,01001]] 

  [[01000],[00101],[01101,01001 ],[01010,11010,10010]] 

prune正在返回一个

  [[01000],[00101]] 

告诉我,由于某种原因,Haskell认为

  [01101,01001]> = [01010,11010,10010] 

当我清楚地看到2> = 3是不正确的。



这是为什么?

解决方案

列表比较是词典编纂,而不是长度。所以你得到了你看到的结果,因为01101> =01010,这又是出于同样的原因 - 两个字符串的前两个字符是相等的,第一个字符的第三个字符大于第三个字符第二个。

I have a list of lists, and a function that returns the list with the most items:

extract ::[Plateau]->Plateau

extract(x:xs) 
  |x==maximumBy(compare `on` length)(x:xs)=x 
  |otherwise = extract(xs)
extract []=[""]

Now I need a function to take the same [Plateau] and return a new [Plateau] that has the previous largest removed:

prune::[Plateau]->[Plateau]
prune(x:xs)
  |x < (maximumBy(compare `on` length)(x:xs)=x : prune (xs)
  |x>=maximumBy(compare `on` length)(x:xs)=[]
prune [] = [] 

I also call prune with a sortBy as to make sure the largest list is last:

(extract . prune) (sortBy(compare `on` length)(plateaus))

This starts out to work correctly. my List plateaus looks like:

plateaus = ["01000"], ["01010", "11010", "10010"] ["00101"], ["01101", "01001"]]

here it is sorted:

 [["01000"], ["00101"], ["01101", "01001"], ["01010", "11010", "10010"]]

Now, my function prune is returning a list of

[["01000"], ["00101"]]

that tells me, for some reason Haskell thinks

["01101", "01001"] >= ["01010", "11010", "10010"]

When I can clearly see that 2 >= 3 is not true.

Why is this?

解决方案

List comparison is lexicographic, not by length. So you get the result you see because "01101" >= "01010", which in turn is for the same reason - the first two characters of the two strings are equal and the third character of the first is greater than the third character of the second.

这篇关于Haskell:删除最大的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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