|
|
|
|
|
by tonyg
2495 days ago
|
|
Conditionals allow a program to react to the data it is presented with. This can be data from the outside world, or data from some other module internal to a program composed out of modules (i.e. subroutines, for example). Without conditionals - or their moral equivalent - programs would not be able to react to their inputs or to changes in their internal state. -- There are Turing-complete languages without conditionals, though - even without data, as such! An example is the lambda calculus. If you want to learn more about this, read up on "Church encoding" and "Scott encoding". The basic idea is to use functions and the visitor pattern to encode a kind of conditional behaviour that varies with the input supplied to it. True = (lambda (t f) (t))
False = (lambda (t f) (f))
If = (lambda (b tr fa) (b tr fa))
(lambda (x)
(If x
(lambda () (display "It was true!"))
(lambda () (display "It was false!"))))
Desk check the reductions of applying that function to True and to False. The "t" and "f" arguments to the boolean values act as a kind of "menu" (the visitor pattern), from which the datum selects in order to communicate what variant of data it is. The pattern extends readily to natural numbers, lists, and so on, giving a straightforward encoding of not only conditionals, but also (one-layer-deep) pattern matching. |
|
> ... giving a straightforward encoding of not only conditionals, but also (one-layer-deep) pattern matching.
So you wrote a conditional out of something that didn't have one? But for that to work, something in the "If" has to decide whether to evaluate the first one or the second one. That is, there is something that is equivalent to a conditional somewhere in the parts that you use to build the "If".
This may come down to what the definition of "conditional" is, though...