Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mixing foldr with OR in Haskell (laziness?)

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

like image 528
pepe22 Avatar asked Jun 21 '18 18:06

pepe22


1 Answers

According to the definition of foldr,

foldr (||) False [True,undefined]
=
True || foldr (||) False [undefined]

According to the definition of (||),

True || _ = True

so there's no need to know the value of the right hand expression to know the answer.

foldr does not do steps on its own. The process is driven by the demands of the reducer function.

edit: Nothing funny's going on. Each evaluation step is straightforwardly done according to the definitions involved.

like image 50
Will Ness Avatar answered Sep 21 '22 12:09

Will Ness