2

Pardon me if the title doesn't make much sense. I had to choose between Is Lua's way of comparison usefull? and Comparison in Lua.

I wanted to do something like this today:

 if currChar == nextChar == "-" then
    ... 
 end

but it kept returning false everytime:

> currChar="-"
> nextChar="-"
> =currChar == nextChar == "-"
false
>
-- All true in Python
print(5 == 5)                    -- true
print(5 == 5 == 5)               -- false
print((5 == 5) == (5 == 5))      -- true
print(5 == (4 + 1) == (6 - 1))   -- false

I fiddled with the values for some time and found out that for some reason, Lua compares values pairwise from left to right:

> = 52 > 3 > 2
stdin:1: attempt to compare number with boolean
stack traceback:
        stdin:1: in main chunk
        [C]: in ?
>

I's there a case when such a form of comparison is usefull?
Why do comparisons that way?

Plakhoy
  • 1,846
  • 1
  • 18
  • 30
  • 8
    Coming from a C-based background, I find it more confusing that `5 == 5 == 5` returns true in Python. `==` is a binary operator, and that expression would seem to break down to `True == 5`, which is false. Basically, Python is atypical in supporting arbitrary chaining of comparison operators. – Dave Dec 11 '13 at 02:02
  • Really? it seems very natural to me. It should have been added in Lua. Consider these two, `num1 == 2 and num2 == 2` and `num1 == num2 == 2`? – Plakhoy Dec 11 '13 at 02:27
  • 1
    I agree it makes sense to read it from the human perspective, but it breaks the pattern of execution for binary operators. Two things my professors used to say come to mind: "Computers are stupid. People are smart." and "That's just syntactic sugar." – Dave Dec 11 '13 at 03:20
  • A similar question for C: http://stackoverflow.com/q/6961643/827263; [my answer](http://stackoverflow.com/a/6961728/827263) goes into a bit more detail. – Keith Thompson Dec 11 '13 at 03:23

1 Answers1

5

Lua's comparison operators are true binary operators. They work on two operands and that's it. In Lua, 5 == 5 == 5 is evaluated as (5 == 5) == 5, which simplifies to True == 5 and is false. In Python, on the other hand, 5 == 5 == 5 is evaluated as 5 == 5 and 5 == 5, which is true.

Python is atypical in supporting chaining of comparison operators, where x < y < z is converted to x < y and y < z. There aren't many languages of which I am aware that support that syntax.

As to whether or not it is useful, that's completely arbitrary. The chaining syntax is simply shorthand.

Dave
  • 4,282
  • 2
  • 19
  • 24
  • @KeithThompson `(5 == 5) == 5` **is** false in Python. I checked before I typed it. The parentheses matter. – Dave Dec 11 '13 at 03:23
  • Sorry, I misread it. The current wording is accurate, but potentially confusing. I suggest: "In Lua, `5 == 5 == 5` is evaluated as `(5 == 5) == 5`, which is false. In Python, `5 == 5 == 5` is evaluated as `5 == 5 and 5 == 5`, which is true. – Keith Thompson Dec 11 '13 at 03:29
  • Fair enough. I realized how you must have read it after changing it back. – Dave Dec 11 '13 at 03:30