Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell more complex predicate

Tags:

haskell

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?

like image 979
Nathan Avatar asked Dec 11 '22 14:12

Nathan


2 Answers

Use lambda functions:

any (\x -> x `mod` 2 == 0) my_list
like image 69
m0nhawk Avatar answered Feb 12 '23 10:02

m0nhawk


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

like image 33
Atmaram Shetye Avatar answered Feb 12 '23 08:02

Atmaram Shetye