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

前端 未结 24 808
一个人的身影
一个人的身影 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: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 ∈ ℝ⁺.

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

    The latter examples you give are effectively syntactic sugar, they'd have to evaluate to the same code as the longer form as at some point the executed code has to compare your value with each of the conditions in turn.

    The array comparison syntax, given in several forms here, closer and I suspect there are other languages which get even closer.

    The main problem with making syntax closer to natural language is that the latter is not just ambiguous, it's hideously ambiguous. Even keeping ambiguity to a minimum we still manage to introduce bugs into our apps, can you imagine what it would be like if you programmed in natural english?!

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

    Languages on computers compare as binary because they are all for a machine that uses binary to represent information. They were designed using similar logic and with broadly similar goals. The English language wasn't designed logically, designed to describe algorithms, and human brains (the hardware it runs on) aren't based on binary. They're tools designed for different tasks.

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

    COBOL uses 88 levels to implement named values, named groups of values and named ranges of values.

    For example:

    01 COLOUR         PIC X(10).
       88 IS-PRIMARY-COLOUR VALUE 'Red', 'Blue', 'Yellow'.
    ...
    MOVE 'Blue' TO COLOUR
    IF IS-PRIMARY-COLOUR
       DISPLAY 'This is a primary colour'
    END-IF
    

    Range tests are covered as follows:

    01 SOME-NUMBER    PIC S9(4) BINARY.
       88 IS-LESS-THAN-ZERO    VALUE -9999 THRU -1.
       88 IS-ZERO              VALUE ZERO.
       88 IS-GREATER-THAN-ZERO VALUE 1 THRU 9999.
    ...
    MOVE +358 TO SOME-NUMBER
    EVALUATE TRUE
        WHEN IS-LESS-THAN-ZERO
             DISPLAY 'Negative Number'
        WHEN IS-ZERO
             DISPLAY 'Zero'
        WHEN IS-GREATER-THAN-ZERO
             DISPLAY 'Positive Number'
        WHEN OTHER
             DISPLAY 'How the heck did this happen!'
    END-EVALUATE
    

    I guess this all happened because COBOL was supposed to emulate English to some extent.

    0 讨论(0)
  • 2020-12-08 15:01

    In C#:

    if ("A".IsIn("A", "B", "C"))
    {
    }
    
    if (myColor.IsIn(colors))
    {
    }
    

    Using these extensions:

    public static class ObjectExtenstions
    {
        public static bool IsIn(this object obj, params object [] list)
        {
            foreach (var item in list)
            {
                if (obj == item)
                {
                    return true;
                }
            }
    
            return false;
        }
    
        public static bool IsIn<T>(this T obj, ICollection<T> list)
        {
            return list.Contains(obj);
        }
    
        public static bool IsIn<T>(this T obj, IEnumerable<T> list)
        {
            foreach (var item in list)
            {
                if (obj == item)
                {
                    return true;
                }
            }
    
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 15:01

    Two possibilities

    Java

    boolean isPrimary = Arrays.asList("red", "blue", "yellow").contains(someColor);
    

    Python

    a = 1500
    if  1 < a < 10 or  1000 < a < 2000:
         print "In range"
    
    0 讨论(0)
提交回复
热议问题