Yes, he
? : , let's see why he is this and not the other. The only thing that pitons will not be so interested to read is because for
us it looks like this:
print True if 1 > 2 else False
It is clear and understandable "in the forehead", with the advent of it in python, I read a lot of reasoning in blogs why it is so, where is the standard
? : similar.
Let's see ...
In general, the essence is clear to all, reduces the routine conditions by 1 action, although write like this:
')
$var = $a > $n ? $b < $a ? 1 : 0 : null;
I would not, because it is not readable right away ...
Why am I talking about this?
Once, at the lab on the theory of algorithms, I first encountered Turing and Post machines, that when I came home, I began to absorb passionately about this topic (well, I really don’t know why), and I want to share it with everyone.
The recording itself
? : in the computer world appeared at the beginning of the 20th century, it was proposed by Emil Leon Post (Post machine, Post algebra are all his works) as a record of a conditional operation in his car.
Machine post
An abstract machine that can set and delete cell values ​​(variables), move to the desired cell, the most common goto, here you can organize a cycle (like while) and a conditional operation based on the movement. There is also a Turing machine, the analogy of the Post, but the version of the Post is so simple and ingenious that I want to tell you about it.
For those who do not understand, this is not some kind of program, not a real cabinet-sized machine, as many people think, this is an abstraction - and this is great.
Imagine some boxes built in a row (tape), and the boxes have their own address (array), for example, we look at the box with the index 0. So, on both sides of these boxes endlessly. We continue to look at this box (we are the carriage) and, in a kind of seek, we can move one iteration left or right (and there are infinity of these boxes).
There can be 0 or 1 in the box. By default, there is 0 everywhere, but we can put 1 in the boxes we need in advance. If you have enough imagination to imagine it all in your head, go ahead ...
Programming
Yes, this is quite a common programming, and without a computer, the syntax is simple:
icn
i - team number
c - team
n - the next command (you can specify any account)
For example:
The cutter is put on position 1 in the tape, all sections are at "0". In section 10 we demolish "1". We write the program:
1. V 2 // , 2 . → 3 // , 3 3. ? 1:4 // 1, 4 4. ! //
Roughly speaking it was
0000000001
We initially stood on the first zero ... now look again at the code, understand what happened?
At the end we get 1111111111
This is the simplest example, “Hello, world” on a post machine!
And here he is our ternary operator in action, and the very record that has been preserved until now.
Here is a list of commands
Y - put a label
X - remove label
transition arrows naturally
? x: y - if the cell is empty, go to x, otherwise y.
Try to write simple programs ... for example, put labels like this 10101010, by default we stand at 0.