2
votes

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).

1
== 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. - Alexis King
Of course! Thanks so much. - Daniel Philpott
If you are familiar with Python, you might have confused this with Python's comparison chaining, where False == False == ... == False is, indeed, equivalent to (False == False) and (False == False) and ... and (False == False). - chepner
Yes, and I misunderstood the fold function's bracketing. - Daniel Philpott
FYI, to check if all the elements are False, use all (==False) [False, False,False]. - Karl Bielefeldt

1 Answers

4
votes

== 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'