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

前端 未结 24 1014
一个人的身影
一个人的身影 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条回答
  •  猫巷女王i
    2020-12-08 14:53

    As a mathematician, I would say that the colour is primary if and only if it is a member of the set {red, green, blue} of primary colours.

    And this is exactly how you could say in Delphi:

    isPrimary := Colour in [clRed, clGreen, clBlue]
    

    In fact, I employ this technique very often. Last time was three days ago. Implementing my own scripting language's interpreter, I wrote

    const
      LOOPS = [pntRepeat, pntDoWhile, pntFor];
    

    and then, at a few lines,

    if Nodes[x].Type in LOOPS then
    

    The Philosophical Part of the Question

    @supercat, etc. ("As to why nobody's done that, I don't know."):

    Probably because the designers of programming languages are mathematicians (or, at least, mathematically inclined). If a mathematician needs to state the equality of two objects, she would say

    X = Y,
    

    naturally. But if X can be one of a number of things A, B, C, ..., then she would define a set S = {A, B, C, ...} of these things and write

    X ∈ S.
    

    Indeed, it is extremely common that you (mathematicians) write X ∈ S, where S is the set

    S = {x ∈ D; P(x)}
    

    of objects in some universe D that has the property P, instead of writing P(X). For instance, instead of saying "x is a positive real number", or "PositiveReal(x)", one would say x ∈ ℝ⁺.

提交回复
热议问题