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

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

    Just to add to language examples

    Scheme

    (define (isPrimaryColor color)
      (cond ((member color '(red blue yellow)) #t)
            (else #f)))
    
    (define (someNumberTest x)
      (cond ((or (and (>= x 1) (<= x 100)) (and (>= x 10000 (<= x 2000))) #t)
            (else #f)))
    
    0 讨论(0)
  • 2020-12-08 14:50

    Ruby

    Contained in list:

    irb(main):023:0> %w{red green blue}.include? "red"
    => true
    irb(main):024:0> %w{red green blue}.include? "black"
    => false
    

    Numeric Range:

    irb(main):008:0> def is_valid_num(x)
    irb(main):009:1>   case x
    irb(main):010:2>     when 1..100, 1000..2000 then true
    irb(main):011:2>     else false
    irb(main):012:2>   end
    irb(main):013:1> end
    => nil
    irb(main):014:0> is_valid_num(1)
    => true
    irb(main):015:0> is_valid_num(100)
    => true
    irb(main):016:0> is_valid_num(101)
    => false
    irb(main):017:0> is_valid_num(1050)
    => true
    
    0 讨论(0)
  • 2020-12-08 14:50

    Python actually gives you the ability to do the last thing quite well:

    >>> x=5
    >>> (1<x<1000 or 2000<x<3000)
    True
    
    0 讨论(0)
  • 2020-12-08 14:50

    I don't see an Objective-C answer yet. Here is one:

    BOOL isPRimaryColour = [[NSSet setWithObjects: @"red", @"green", @"blue", nil] containsObject: someColour];
    
    0 讨论(0)
  • 2020-12-08 14:51

    You'll have to go a bit down the abstraction layer to find out the reason why. x86's comparison/jump instructions are binary (since they can be easily computed in a few clock cycles), and that's the way things have been.

    If you want, many languages offer an abstraction for that. In PHP, for example you could use:

    $isPrimaryColor = in_array($someColor, array('Red', 'White', 'Blue'));
    
    0 讨论(0)
  • 2020-12-08 14:51

    The question is reasonable, and I wouldn't regard the change as syntactic sugar. If the value being compared is the result of computation, it would be nicer to say:

      if (someComplicatedExpression ?== 1 : 2 : 3 : 5)
    

    than to say

      int temp;
      temp = someComplicatedExpression;
      if (temp == 1 || temp == 2 || temp == 3 || temp == 5)
    

    particularly if there was no other need for the temp variable in question. A modern compiler could probably recognize the short useful lifetime of 'temp' and optimize it to a register, and could probably recognize the "see if variable is one of certain constants" pattern, but there'd be no harm in allowing a programmer to save the compiler the trouble. The indicated syntax wouldn't compile on any existing compiler, but I don't think it would be any more ambiguous than (a+b >> c+d) whose behavior is defined in the language spec.

    As to why nobody's done that, I don't know.

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