I'm learning Haskell, and I'm wondering how to have a predicate that's a bit more complex.
For example, I can do this:
any ( >= 5 ) my_list
But I can't find a way how to do something like this:
any (x `mod` 2 == 0) my_list
How could I do this?
Use lambda functions:
any (\x -> x `mod` 2 == 0) my_list
For really complex stuff, you are better off, defining a separate function. For smaller cases, you could use a lambda or even something like
any predicate myList
where predicate x = even x
EDIT: even x is just a simplification. You could put something like where predicate x = (mod x 3) == 1
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