How can this function return true?
foldr (||) False [True,undefined]
=> True
The first fold looks like this:
undefined || True
, which should return an error
So im guessing haskell gives priority to the lazyness of the OR function over doing the folds step by step. Finds a True
on the way and returns that before starting the fold
Is this correct? In that case, does haskell always give priority to a lazy function over the non lazy ones? I believe that is the definition for being lazy but it seems like that can change the answer to make it wrong
undefined || True
? Because that is incorrect, so if you can explain why you believe that, we may be able to point to the error in your reasoning that led to that conclusion. – Chlorate(||)
evaluated iffoldr
actually folded from the right, the way it often does in strict languages. – Brunnfoldr
does fold from the right, denotationally. Its "first" fold isundefined || False
which isundefined
. The "second" isTrue || undefined
which isTrue
since OR is lazy (as it would also be in Java,C,etc.). – Alcornundefined || False
, notundefined || True
! This does (would) becomeundefined
, as you observed, but unlike in a lazy language, it turns out that just because some part of the computation hasundefined
doesn't mean the whole thing isundefined
... – Chlorate