Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining predicates with logical && in filter function

Tags:

haskell

I just stumbled upon this sentence at learnyouahaskell.com:

The filter equivalent of applying several predicates in a list comprehension is either filtering something several times or joining the predicates with the logical && function.

Could someone give an example for the last part (joining the predicates with the logical && function)?

Does the author mean something like this (which doesn't work):

filter ((>3) && (<10)) [5,3]
like image 289
Chris Graf Avatar asked Aug 15 '26 06:08

Chris Graf


1 Answers

filter's predicate argument is a function, so you would need to use a lambda:

filter (\x -> x > 3 && x < 10) [5,3]

I suppose the author was assuming the reader would do the appropriate expansion.

It's also possible to lift boolean operators to operate on predicates using the (a ->) Applicative instance:

filter (liftA2 (&&) (> 3) (< 10)) [5,3]

But I don't personally find that particularly nice.

like image 186
luqui Avatar answered Aug 17 '26 01:08

luqui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!