开发者

language without if's?

A colleague said he heard of a language that did not have the concept of "if". Is that possible? If 开发者_StackOverflow中文版so, what language is it?


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.

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

would become soemthing like

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)
}

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

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.

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

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, 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++ template programming does not have an 'if' construct but is Turing-complete via template specialization:

template <int N>
struct Factorial 
{
    enum { value = N * Factorial<N - 1>::value };
};

template <>
struct Factorial<0> 
{
    enum { value = 1 };
};

// Factorial<4>::value == 24
// Factorial<0>::value == 1

from Wikipedia's article on template metaprogramming


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜