My understanding is that foldl
and foldr
executes like :
foldl f a [1..30]
=> (f (f (f ... (f a 1) 2) 3) ... 30)
and
foldr f a [1..30]
=> (f 1 (f 2 (f 3 (f ....(f 30 a)))))..)
so.. foldr (&&) False (repeat False)
can shortciruit as outermost f
sees (&&) False ((&&) False (....))
sees first argument as false and does not need to evaluate the second argument (which is a large thunk).
so what happens with
andFn :: Bool -> Bool -> Bool
andFn _ False = False
andFn x True = x
and
foldl andFn True (repeat False) -- =>
-- (andFn (andFn ...(andFn True False) ... False) False)
-- ^^ outermost andFn
But this is taking forever.
I thought that outermost andFn
would know that by pattern matching on second argument, the answer is False
..
What else is happening here ?