没有 if 的语言?
一位同事说他听说过一种没有“如果”概念的语言。这可能吗?如果是的话,它是什么语言?
A colleague said he heard of a language that did not have the concept of "if". Is that possible? If so, what language is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除了 Prolog 之外,我不知道任何特定的语言,但我可以想到没有 if 语句 的语言可以工作的几种方式。事实上,您也不需要循环构造。显然,您需要某种条件分支和循环的方式。
例如,如果您具有以下功能:函数、函数参数上的 ML 样式模式匹配和尾部调用优化,您可以没有 if 或循环的程序。
会变成类似
or 的类似 ML 语法:
当然,您仍然需要常用的比较运算符,然后您可以使用简单的 true/false 函数参数模式匹配而不是条件。或者您可以匹配任意值。或者该语言可以支持保护表达式,这些表达式基本上是确定函数重载是否有效的 if 语句。
上面的例子显然是人为的,没有 ifs/loops 的代码比原来的代码更难看、更难理解,但它演示了如何凑合。更多或不同的语言功能可能使编写没有 if/循环的干净程序成为可能。
另一种方法是这样的,如果 true == 1 且 false == 0。
也就是说,将 true 和 false 分支存储在列表或元组或任何可以通过 true 和 false 索引的内容中,然后使用结果以条件为索引,查找分支并调用函数。如果您的语言支持宏,则可以将传统条件翻译成这种格式。
Besides perhaps Prolog, I don't know of any specific languages, but I can think of a few ways a language without if statements may work. In fact, you don't need loop constructs either. You obviously needs some way of conditional branches and looping.
If, for example, you had the following features: functions, ML-style pattern matching on function arguments and tail-call optimization, you could program without if's or loops.
would become soemthing like
or with ML-like syntax:
Of course, you still need the usual comparison operators and then you can use simple true/false function argument pattern matching instead of conditionals. Or you could match on arbitrary values. Or the language could support guard expressions, which are basically if statements which determine if a function overload is valid or not.
The example above is obviously contrived and the code without ifs/loops is a lot uglier and harder to understand than the original, but it demonstrates how you can make do. More or different language features may make it possible to write clean programs without if/loops.
Another way would be something like this, if true == 1 and false == 0.
That is, store the true and false branch in a list or tuple or whatever you have that can be indexed by true and false and then use the result of the condition as the index, look up the branch and call the function. If your language supports macros, it may be possibly to translate traditional conditionals into this format.
Smalltalk被认为是“真正的”面向对象语言,没有“if”语句,也没有“for”语句,没有“while”语句。还有其他例子(例如 Haskell),但这是一个很好的例子。
来源:没有 ifs
Smalltalk, which is considered as a "truly" object oriented language, has no "if" statement, and it has no "for" statement, no "while" statement. There are other examples (like Haskell) but this is a good one.
Source: Without ifs
C++ 模板编程没有“if”构造,但通过模板专门化实现了图灵完备:
来自 Wikipedia 关于模板的文章元编程
C++ template programming does not have an 'if' construct but is Turing-complete via template specialization:
from Wikipedia's article on template metaprogramming
我相信一种语言必须有一些进行选择的方法,才能图灵完备。然而,这意味着不一定是经典的 if 语句形式。
也许最熟悉的例子是正则表达式语言。 (a | b*) 根据
|
两侧的内容做出决定。不完全是一个“如果”陈述。I believe a language has to have some means of doing selection, in order to be Turing-Complete. However, that means would not have to be your classic if-statement form.
Probably the most familiar example would be regular expression languages. (a | b*) makes a decision based on what's on the opposite sides of that
|
. Not exactly an "if" statement.有些逻辑语言由语句组成。查询的结果是一个逻辑评估,检查结果是否可以由“编码”的规则组假定。
例如,请查看 Prolog。
There are logical languages that consist of statements. The results to a query is a logical assessment that checks if the result CAN be assumed by the group of rules that were "coded" .
Look at Prolog for example.