Evaluating an "and" statement

Hi everyone, let’s say that I have a very big set S . I want to study this set
X= [ x | x<-S , cond1(x)==True , cond2(x)==True] where cond1 and cond2 are certain conditions that I want study.

Let’s say that cond1 is super easy to check but cond2 is very expensive (like 9000 times more difficult to evaluate than cond1).
I want to know if Haskell evaluates both conditions independently if cond1 is true or not.

1 Like

Hello Miguel,

List comprehensions can be desugared to:

import Control.Monad

prova :: [Int]
prova = do x <- someList
           guard (cond1 x)
           guard (cond2 x)
           return x

(do itself can be – and should be, for learning purposes – desugared
to >>=).

So no, if cond1 x is False, cond2 x will not be evaluated.
You can check it yourself by setting cond1 to const False and
cond2 to undefined.

1 Like