本文介绍了在Haskell中按照长度过滤列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像 [a,ab,abc,abcd]
的列表
如何得到一个只有长度> 2的项目的列表。
意味着结果是 [abc, abcd]。
解决方案
Natalie的回答完全正确,但作为替代形式,您可以还将其写为
filter((> 2)。length)[a,ab,abc ,abcd]
或者将列表理解为
[str | str< - [a,ab,abc,abcd],长度str> 2]
所有这三个元素都是相同的
I have a list like ["a","ab","abc", "abcd"]
How to get a list that only has the items which have a length > 2.
Means the result is ["abc","abcd"].
解决方案
Natalie's answer is perfectly correct, but as an alternate form you could also write it as
filter ((> 2) . length) ["a", "ab", "abc", "abcd"]
Or with list comprehension as
[str | str <- ["a", "ab", "abc", "abcd"], length str > 2]
All three are equivalent
这篇关于在Haskell中按照长度过滤列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!