Why do most programming languages only have binary equality comparison operators?

前端 未结 24 805
一个人的身影
一个人的身影 2020-12-08 14:20

In natural languages, we would say \"some color is a primary color if the color is red, blue, or yellow.\"

In every programming language I\'ve seen, that translates

相关标签:
24条回答
  • 2020-12-08 14:34

    I think that most people consider something like

    isPrimaryColor = ["Red", "Blue", "Yellow"].contains(someColor)
    

    to be sufficiently clear that they don't need extra syntax for this.

    0 讨论(0)
  • 2020-12-08 14:37

    In Python you can say ...

    isPrimaryColor = someColor in ('Red', 'Blue', 'Yellow')
    

    ... which I find more readable than your (== "Red" or == "Blue") syntax. There's a few reasons to add syntax support for a language feature:

    • Efficiency: Not a reason here, since there's no speed improvement.
    • Functionality: Also not a concern; there's nothing you can do in the new syntax that you can't do in the old.
    • Legibility: Most languages handle the case where you're checking the equality of multiple values just fine. In other cases (e.g., someNumber (> 1 and < 10)) it might be more useful, but even then it doesn't buy you much (and Python allows you to say 1 < someNumber < 10, which is even clearer).

    So it's not clear the proposed change is particularly helpful.

    0 讨论(0)
  • 2020-12-08 14:38

    Icon has the facility you describe.

    if y < (x | 5) then write("y=", y)
    

    I rather like that aspect of Icon.

    0 讨论(0)
  • 2020-12-08 14:39

    I'm reminded of when I first started to learn programming, in Basic, and at one point I wrote

    if X=3 OR 4
    

    I intended this like you are describing, if X is either 3 or 4. The compiler interpreted it as:

    if (X=3) OR (4)
    

    That is, if X=3 is true, or if 4 is true. As it defined anything non-zero as true, 4 is true, anything OR TRUE is true, and so the expression was always true. I spent a long time figuring that one out.

    I don't claim this adds anything to the discussion. I just thought it might be a mildly amusing anecdote.

    0 讨论(0)
  • 2020-12-08 14:42

    In python you can do something like this:

    color = "green"
    
    if color in ["red", "green", "blue"]:
        print 'Yay'
    

    It is called in operator, which tests for set membership.

    0 讨论(0)
  • 2020-12-08 14:43

    This can be replicated in Lua with some metatable magic :D

    local function operator(func)
        return setmetatable({},
            {__sub = function(a, _)
                return setmetatable({a},
                    {__sub = function(self, b)
                        return f(self[1], b)
                    end}
                )
            end}
        )
    end
    
    
    local smartOr = operator(function(a, b)
        for i = 1, #b do
            if a == b[i] then
                return true
            end
        end
        return false
    end)
    
    
    local isPrimaryColor = someColor -smartOr- {"Red", "Blue", "Either"}
    

    Note: You can change the name of -smartOr- to something like -isEither- to make it even MORE readable.

    0 讨论(0)
提交回复
热议问题