编程语言中的链式不等式符号 [英] Chained inequality notation in programming languages
问题描述
是否有一种支持链式符号<的编程语言。 b< c代替< b和b<
Is there a programming language that supports chained notation a < b < c to be used instead of a < b and b < c in conditional statements?
示例:
- if(2
- 如果(2< x& x< 5)
第一个语句对我来说看起来更好,更容易理解,并且编译器可以使用传递性属性来警告错误(例如5< x< 2会给出警告)。
First statementlooks better to me, it's easier to understand and the compiler could use transitivity property to warn about mistakes (e.g. 5 < x < 2 would give a warning).
推荐答案
图标做到了,而且不属于任何骇人的特殊情况链接的一部分;它是Icon的目标导向的评估模型的一部分。任何比较成功或失败。如果成功,它将产生右侧。因此,如果0< = i< = j<,您可以写出
Icon does this, and it is not part of any hacky special-case "chaining"; it is part of Icon's goal-directed evaluation model. Any comparison either succeeds or fails. If it succeeds, it produces the right-hand side. So you can write
if 0 <= i <= j < n then ...
,它的运行方式完全符合您的期望。但这不仅适用于比较,而且适用于 any 表达式;这意味着您可以编写完全相同的链接函数。我喜欢Icon的这一方面,希望更多的语言可以结合目标导向的评估。
and it works exactly the way you would expect. But it works not just for comparisons but for any expression; this means you can write your own functions that "chain" in exactly the same way. I love this aspect of Icon and wish more languages could incorporate goal-directed evaluation.
N.B在Guido在VHLL(1990年代中期)介绍Python的论文中,他明确提到Icon是Python设计灵感的来源。
N.B. In Guido's paper introducing Python at VHLL (middle 1990s) he mentions Icon explicitly as a source of inspiration in the design of Python.
这篇关于编程语言中的链式不等式符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!