简体   繁体   English

Haskell中混乱的函数应用程序和函数组合

[英]Confusing function application and function composition in Haskell

The operation 手术

(filter (`notElem` "'\"").[(1,'a','%',"yes")])

gives an error. 给出一个错误。 How can apply this filter on that list properly? 如何将此过滤器正确地应用于该列表?

You've got a couple of serious problems. 您遇到了几个严重的问题。 First, your syntax is wacky ( . definitely shouldn't be there). 首先,您的语法很古怪( .绝对不应该在那里)。 But the bigger problem is that what you're trying to filter is of the type [(Int,Char,Char,[Char])] (that is, a list containing a 4-tuple). 但更大的问题是,您要过滤的内容是[(Int,Char,Char,[Char])] (即包含4个元组的列表)。

And your list has only one element, which is (1,'a','%',"yes") . 并且您的列表只有一个元素,即(1,'a','%',"yes") So filtering that is useless anyway. 因此过滤仍然无济于事。 When function you provide for filtering must be of type a -> Boolean , where a is the type of all the elements of the list. 当您提供过滤的函数必须是a -> Boolean类型时,其中a是列表中所有元素的类型。

Seems like you wanted some sort of wonky heterogenous list or something. 似乎您想要某种类型的异类列表或其他内容。

The . . operator in Haskell is function composition -- it composes two functions together. Haskell中的operator是函数组合-它将两个函数组合在一起。

So your code, 所以你的代码

(`notElem` "'\"") . [(1,'a','%',"yes")]

looks like the composition of the notElem function and some list. 看起来像notElem函数和一些列表的组成。 That's just wrong. 那是错误的。

Remove the . 删除. , and make sure to show the list first: ,并确保首先show该列表:

> filter (`notElem` "'\"") (show [(1,'a','%',"yes")])
"[(1,a,%,yes)]"

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

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