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