简体   繁体   English

这在 Haskell 中有什么作用?

[英]What does this do in Haskell?

f = filter . flip notElem

我想知道这条线是如何工作的,这是一个应该接受两个字符串并返回一个字符串的函数。

flip :: (b -> a -> c) -> a -> b -> c flips the first two parameters of a function. flip :: (b -> a -> c) -> a -> b -> c翻转函数的前两个参数。 So since notElem :: Eq a => a -> [a] -> Bool takes a value and a list to check if the value is not in the list, flip notElem :: Eq a => [a] -> a -> Bool takes a list and a value to check if it is not in the list.因此,由于notElem :: Eq a => a -> [a] -> Bool接受一个值和一个列表来检查该值是否不在列表中,所以flip notElem :: Eq a => [a] -> a -> Bool接受一个列表和一个值来检查它是否不在列表中。

We thus will first partially apply the first parameter in f to flip notElem , and then filter a list.因此,我们将首先部分应用f的第一个参数来flip notElem ,然后filter一个列表。 Your expression is thus a short form of:因此,您的表达式是以下形式的简短形式:

f :: Eq a => [a] -> [a] -> [a]
f xs ys = filter (\y -> notElem y xs) ys

It will thus return all elements in ys (the second parameter) that are not in xs (the first parameter).因此,它将返回ys (第二个参数)中不在xs (第一个参数)中的所有元素。 For example:例如:

Prelude> f [1,3,0,2] [1,4,2,5]
[4,5]

Both 4 and 5 are elements of the second list, but not of the first one. 45都是第二个列表的元素,但不是第一个列表的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM