简体   繁体   中英

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).

And your list has only one element, which is (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.

Seems like you wanted some sort of wonky heterogenous list or something.

The . operator in Haskell is function composition -- it composes two functions together.

So your code,

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

looks like the composition of the notElem function and some list. That's just wrong.

Remove the . , and make sure to show the list first:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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