Haskell is a more complex predicate

I am studying Haskell and I am wondering how to have a predicate that is a little more complicated.

For example, I can do this:

any ( >= 5 ) my_list 

But I cannot find a way to do something like this:

  any (x `mod` 2 == 0) my_list 

How can i do this?

+4
source share
3 answers

Use lambda functions:

 any (\x -> x `mod` 2 == 0) my_list 
+5
source

For really complex things, you're better off defining a separate function. For small cases, you can use lambda or even something like

 any predicate myList where predicate x = even x
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

+5
source

Besides using anonymous (lambda functions), as in @ m0nhawk's answer, you can use the function:

 >>> any ( (==0) . (`mod` 2) ) [1,2,3] True 
+3
source

Source: https://habr.com/ru/post/1487531/


All Articles