Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefix function as a predicate for filter function

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!

like image 982
Ghost Bunnies Avatar asked Jan 18 '26 03:01

Ghost Bunnies


1 Answers

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!"
like image 67
Bartek Banachewicz Avatar answered Jan 19 '26 19:01

Bartek Banachewicz