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

前端 未结 24 806
一个人的身影
一个人的身影 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:46

    You'll love Perl 6 because it has:

    • chaining comparison operators:

      (1 <= $someNumber <= 100) || (1000 <= $someNumber <= 2000))

    • junctive operators:

      $isPrimaryColor = $someColor ~~ "Red" | "Blue" | "Yellow"

    And you can combine both with ranges:

    $someNumber ~~ (1..100) | (1000..2000)
    
    0 讨论(0)
  • 2020-12-08 14:47

    My guess would be that languages are designed by force of habit. Early languages only would have had binary comparison operators because they are simpler to implement. Everyone got used to saying (x > 0 and x < y) until language designers didn't ever bother to support the common form in mathematics, (0 < x < y).

    In most languages a comparison operator returns a boolean type. In the case of 0 < x < y, if this is interpreted as (0 < x) < y it would be meaningless, since < does not make sense for comparing booleans. Therefore, a new compiler could interpret 0 < x < y as tmp:=x, 0 < tmp && tmp < y without breaking backward compatibility. In the case of x == y == z, however, if the variables are already booleans, it is ambiguous whether this means x == y && y == z or (x == y) == z.

    In C# I use the following extension method so that you can write someColor.IsOneOf("Red", "Blue", "Yellow"). It is less efficient than direct comparison (what with the array, loop, Equals() calls and boxing if T is a value type), but it sure is convenient.

    public static bool IsOneOf<T>(this T value, params T[] set) 
    {
        object value2 = value;
        for (int i = 0; i < set.Length; i++)
            if (set[i].Equals(value2))
                return true;
        return false;
    }
    
    0 讨论(0)
  • 2020-12-08 14:47

    It's because programming languages are influenced by mathematics, logic and set theory in particular. Boolean algebra defines ∧, ∨ operators in a way that they do not work like spoken natural language. Your example would be written as:

    Let p(x) be unary relation which holds if and only if x is a primary color
    p(x) ⇔ r(x) ∨ g(x) ∨ b(x)
    or
    p(x) ⇔ (x=red) ∨ (x=green) ∨ (x=blue)
    

    As you see, it's pretty similar to notation that would be used in programming language. As mathematics provide strong theoretic foundations, programming languages are based on mathematics rather than natural language which always leaves a lot of space for interpretation.

    EDIT: Above statement could be simplified by using set notation:

    p(x) ⇔ x ∈ {red, green, blue}
    

    and indeed, some programming languages, most notably Pascal, included set, so you could type:

    type
        color = (red, green, blue, yellow, cyan, magenta, black, white);
    
    function is_primary (x : color) : boolean;
    begin
        is_primary := x in [red, green, blue]
    end
    

    But sets as a language feature didn't catch on.

    PS. Sorry for my imperfect English.

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

    In Haskell, it is easy to define a function to do this:

    matches x ps = foldl (||) False $  map (\ p -> p x) ps
    

    This function takes a value list of predicates (of type a -> Bool) and returns True if any of the the predicates match the value.

    This allows you to something like this:

    isMammal m = m `matches` [(=="Dog"), (=="Cat"), (=="Human")]
    

    The nice thing is that it doesn't have to just be equality, you can use anything with the correct type:

    isAnimal a = a `matches` [isMammal, (=="Fish"), (=="Bird")]
    
    0 讨论(0)
  • 2020-12-08 14:48

    In perl 6 you could do this with junctions:

    if $color eq 'Red'|'Blue'|'Green' {
        doit()
    }
    

    Alternately you could do it with the smart match operator (~~). The following is roughly equivalent to python's if value in list: syntax, except that ~~ does a lot more in other contexts.

    if ($color ~~ qw/Red Blue Green/) {
        doit()
    }
    

    The parens also make it valid perl 5 (>=5.10); in perl 6 they're optional.

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

    So far, nobody has mentioned SQL. It has what you are suggesting:

    SELECT
        employee_id
    FROM 
        employee
    WHERE
        hire_date BETWEEN '2009-01-01' AND '2010-01-01' -- range of values
        AND employment_type IN ('C', 'S', 'H', 'T')     -- list of values
    
    0 讨论(0)
提交回复
热议问题