[英]Filtering a List Haskell
I just stated learning Haskell about filtering lists. 我刚刚说过要学习Haskell有关过滤列表的知识。 Suppose I have the following list : [2, 3, 4, 5, 8, 10, 11]
I would like keep only those numbers in the list, which are not divisible by the other members. 假设我有以下列表: [2, 3, 4, 5, 8, 10, 11]
我只想将那些不能被其他成员整除的数字保留在列表中。
The result of our example would be : [2, 3, 5, 11]
我们的示例的结果将是: [2, 3, 5, 11]
[x | x <- src, all (\y -> x `rem` y /= 0) (filter (<x) src)]
where src = [2,3,4,5,8,10,11]
It should be noted that you actually also mean dividable by other numbers that are below it, and not just any number in that list, which is why there's a filter
in the 2nd argument for all
. 应该注意的是,您实际上还意味着可以被其下的其他数字整除,而不仅仅是该列表中的任何数字,这就是为什么在第二个参数中对all
进行filter
原因。
The result, of course, is the one you expect in your question: [2,3,5,11]
. 结果当然是您在问题中所期望的: [2,3,5,11]
。
Here's how it works (and if I'm missing anything, let me know and I'll update). 这是它的工作方式(如果我缺少任何内容,请告诉我,我会进行更新)。
I'll explain the code side-by-side with normal English. 我将与普通英语并排解释代码。 I suggest you just read just the English first, and afterwards see how each statement is expressed in code - I think it should be the most friendly for a newcomer. 我建议您先阅读英语,然后再看每个语句在代码中的表达方式-我认为对于新手来说,它应该是最友好的。
Also note that I flipped the arguments for filter
and all
below (it is invalid!) to make the explanation fluid. 还要注意,我将filter
和all
参数all
翻转了(这是无效的!),以使说明流畅。
[x|
: Construct a list made out of x
:构造一个由x
的列表
x <- src
: Where x
is an element from src
x <- src
:其中x
是src
的元素
,
: But only the elements that satisfy the following predicate/rule: ,
:,但只有满足以下谓词/规则的元素:
all
of the numbers from 来自的all
数字
(filter src (<x))
: src
that are lesser-than the current x
(filter src (<x))
:小于当前x
src
(\\y -> x 'rem' y /= 0)
: must not yield a remainder equal to 0. (\\y -> x 'rem' y /= 0)
:不得产生等于0的余数。
]
For the code part to make sense, make sure you've familiarized yourself with all
, filter
, rem
, and the syntax for: list comprehensions , lambda expressions , sections , and backticks . 为了使代码部分有意义,请确保您已熟悉all
, filter
, rem
和以下语法: 列表理解 , lambda表达式 , 部分和反引号 。
Using filter
使用filter
filter :: (a -> Bool) -> [a] -> [a]
and from Data.Numbers.Primes
the function 并从Data.Numbers.Primes
函数
isPrime :: Integral int => int -> Bool
may be 也许
filter isPrime [2, 3, 4, 5, 8, 10, 11]
or using list comprehension 或使用列表理解
[ x | x <- [2, 3, 4, 5, 8, 10, 11], isPrime x]
change filter predicate as you wish, eg 根据需要更改过滤谓词,例如
-- None `xs` element (different than `x`) divide `x`
noneDiv xs x = and [x `mod` y /= 0 | y <- xs, x /= y]
now 现在
myFilter xs = filter (noneDiv xs) xs
or 要么
myFilter xs = [x | x <- xs, noneDiv xs x]
On GHC, 在GHC上
Prelude> :m + Data.List
Prelude Data.List> nubBy (\a b -> rem a b == 0) [2,3,4,5,8,10,11]
[2,3,5,11]
does the trick. 绝招。 On Haskell98-compatible systems (eg Hugs), use nubBy (\\ba -> rem ab == 0)
. 在与Haskell98兼容的系统(例如Hugs)上,使用nubBy (\\ba -> rem ab == 0)
。
This answer was posted as a comment by Will Ness . 该答案由Will Ness 发表 , 作为评论 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.