Search code examples
haskellequalityfold

Haskell: Confusion over 'foldl' with parenthesized operator (==)


Here are a few results of applying 'foldl' to this particular equality operator. I do not understand them - I thought that each line should return true, since "False == False == False ..." is true regardless of the length of the list. I am fairly new to Haskell.

Prelude> foldl (==) False [False]
True
Prelude> foldl (==) False [False,False,False]
True
Prelude> foldl (==) False [False,False,False,False]
False
Prelude> foldl (==) False [False,False,False,False,False]
True
Prelude> foldl (==) False [False,False,False,False,False,False]
False

I found these results when trying to write a function that tests whether a list of functions give the same result when applied to a common argument (returning a Boolean).


Solution

  • == is a binary operator. When you write False == False == False, you’re really writing (False == False) == False, which is True == False. Perhaps now you can understand what’s happening.

    Answer submitted in comments by 'Alexis King'