Here is the type signature and the definition of the filter function from Learn You a Haskell for Great Good!:
filter' :: (a -> Bool) -> [a] -> [a]
filter' _ [] = []
filter' p (x:xs)
| p x = x : filter' p xs
| otherwise = filter' p xs
An usage example in the book is with elem is as follows:
filter' (`elem` ['a'..'z']) "Hell0!"
Which returns:
"ell"
In this particular example, is there a possible way of using elem as a prefix function instead of an infix function as a predicate?
In a more general sense, is there a way to supply only the second parameter in order to partially apply a prefix function?
Thanks in advance for your help!
Either by creating a lambda (which will work for functions taking more than 2 parameters):
filter' (\a -> elem a ['a'..'z']) "Hell0!"
Or by using flip:
filter' (flip elem ['a'..'z']) "Hell0!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With