语言如果不是? [英] language without if's?

查看:74
本文介绍了语言如果不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位同事说,他听说过一种语言,没有"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?

推荐答案

除了Prolog,我不知道任何特定的语言,但是我可以想到几种没有 if语句的语言可能有效.实际上,您也不需要循环构造.显然,您需要某种方式的条件分支和循环.

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.

例如,如果您具有以下功能:功能在函数参数上匹配的ML样式模式尾调用优化 ,您可以进行编程,而无需if或loops.

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.

foo () {
    for (i = 1 to 10) {
        if even(i) {
            print "even"
        }
    }
}

会变得像

print_if_true (true) {
    print "even"
}
print_if_true (false) {}

foo_loop (11) {
}
foo_loop (n) {
    print_if_true(even(n))
    foo_loop(n+1)
}

foo () {
    foo_loop(1)
}

或具有类似ML的语法:

or with ML-like syntax:

foo => 
    let loop 11 => 0
              n => p_i_t(n), loop n + 1
    and p_i_t true => print "even"
                 _ => unit
    in
        loop 1
    end

当然,您仍然需要常用的比较运算符,然后可以使用简单的true/false函数参数模式匹配来代替条件.或者您可以匹配任意值.或者该语言可以支持保护表达式,这基本上是if语句,该语句确定函数重载是否有效.

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.

上面的示例显然是人为设计的,没有ifs/loops的代码比原始代码丑陋且难以理解,但是它演示了如何实现.更多或不同的语言功能可能使编写没有if/loop的干净程序成为可能.

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.

如果true == 1和false == 0,则另一种方式是这样的.

Another way would be something like this, if true == 1 and false == 0.

[function(){else-clause}, function(){then-clause}][condition]()

也就是说,将true和false分支存储在列表或元组或可以由true和false索引的任何内容中,然后将条件的结果用作索引,查找分支并调用函数.如果您的语言支持宏,则可能会将传统条件转换为这种格式.

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.

这篇关于语言如果不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆