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

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

提交回复
热议问题