"else" 关键字可能是从德语字面翻译而来的(2020)
The 'else' keyword is probably a literal translation from German (2020)

原始链接: https://github.com/e-n-f/if-then-else/blob/master/if-then-else.md

视频追溯了编程构造 if-then-else 如何从硬件层面的条件跳转演变而来。早期系统如 ENIAC 通过布线做决策;后来的计算机使用可根据结果跳转的数值指令。Fortran 的三向 “if” 和 Flow-Matic 的 “otherwise” 使条件表达更丰富。1958 年,德国和美国关于机器无关语言的提案引入了布尔控制块和短路思想。Algol 58 有 “if” 和 “if either”,但没有独立的 “else”。LISP 的条件表达式要求 “otherwise” 子句,启发 Klaus Samelson 将 Algol 的条件统一为如今标准的 Algol 60 “if-then-else”。单词 “else” 本身首次出现在德国提案的 “case” 形式中,可能来自翻译。后来的语言如 CPL 和 MAD 避免使用 “else”,但保留了其结构。到 1959 年,条件形式已基本确定,尽管其词汇仍显古怪。

一篇 Hacker News 帖子链接到一篇 2020 年文章,认为编程关键字“else”可能是从德语直译而来的。讨论串里有几条评论:一条提供了 if/then/else 式表述,一条认为“else”可在日常语言中使用,一条建议用“otherwise”替代,还有一条开玩笑称英语源自编程 if/then/else。总体来说,这是一段关于该词可能的德语起源及其在代码与日常语言中用法的简短轻松讨论。
相关文章

原文

Video


01

Hi, I'm Eric. I'm here to talk about what seems like an absurd idea: that if-then-else had to be invented.


02

If-then-else is how we talk about conditions in programming languages: if something is true, then do a thing, else do a different thing.

That's just English, right? Except that it isn't. I can't use "else" as a conjunction in normal speech, only in computer programs.

Where did this else come from? It's a mystery. It's too microscopic a detail to have made it into books on the history of programming languages.


03

It doesn't seem to have come from surveys or legal proceedings or algorithms as they were written before there were computers. You find "if yes" and "if no" and "if however", but not "else".


05

The first computer to be able to perform different instructions depending on the result of a previous calculation seems to have been the Eniac. In 1946, Haskell Curry and Willa Wyatt wrote this report describing a program to invert a function. They used the name "discrimination" for the facility of making a decision based on which of two numbers was larger.


06

The Eniac did not have an instruction called "discriminate." It was programmed by wires and dials on plugboards, and the control panel for the instruction that made a calculation for a decision was connected by physical wires to the instructions that could follow it.

Soon computers began to have enough memory that programs could be stored in memory instead of wired together. Instead of a physical sequence of instructions, there was a numerical sequence. A few special instructions could cause the computer to jump to a different point in the sequence.


07

Here, for example, is the conditional jump instruction from one of the first commercially produced computers. It checked whether the last number calculated was negative. If so, it diverted the flow of control to some specified location in memory; otherwise, it let control continue to the next instruction.

It was meant for a specific usage pattern: if you wanted to do a task, say, 10 times, your program counted how many times it had done it so far and then subtracted 10. If the result was negative, the task wasn't done, so it jumped back to do another round.


08

The idea was carried forward into the first higher-level programming languages, like Halcombe Laning and Neal Zierler's language for Whirlwind. Their conditional jump worked the same way, except that the numbered steps of the program were algebraic expressions, not single machine instructions.


09

The first popular programming language, Fortran, generalized the idea by specifying jumps to three locations at once, depending on whether a calculation was negative, zero, or positive, and gave it the name "if."

The three-way "if" was more powerful than just checking for a negative, but was probably more confusing, because every decision meant a discontinuity in the control flow instead of programmers only having to think about the normal case that continued on and the unusual case that jumped.


10

Flow-Matic, Grace Murray Hopper's predecessor to COBOL, made the three-way if a little easier to think about by talking about comparing two numbers instead of about the signs of numbers. It introduced the name "otherwise" for the case where the comparison wasn't what you were looking for.


algol

All these programming languages were associated with one particular computer from one particular manufacturer. In 1958, two American and German computing organizations began a joint project to develop a standard machine-independent language, one that would be natural for people to talk about rather than natural to implement on some particular machine. Each group brought a draft proposal to the joint conference.


11

The German authors made two big conceptual leaps in their formulation of the "if" statement.

The first was to let the conditional be controlled by any boolean expression rather than giving special priority to the less-than/equal/greater-than form.


11

The second big leap was that instead of causing an abrupt jump in the flow of control, their "if" statement only caused the flow to be temporarily diverted. At the end of the condition, whether the condition had been true or false, the program would resume at the "always" statement at the end of the block. The only difference would be whether the subsidiary statements had been performed.

The example on screen shows two "ifs" in a single block, and you might think that the second one was intended to work as an "else if" does now. But they expected all the conditions to be evaluated, even if one had already been found to be true. So if the statements controlled by the first changed a value that the second comparison depended upon, both blocks of statements might execute.


12

Where the German proposal did have something like "else" was in a second, entirely different, conditional form called "case." Unlike "case" as we know it now, their "case" was another way of writing boolean conditions, with the conditions set apart in a separate block from the statements they controlled. It sounds like they thought this form would be easier for more complicated comparisons.

And here is the first appearance of the world "else," for the case where none of the other cases apply.


13

Why did they call it "else?" They don't say.

What they do say is that this document was originally written in German and hastily translated into English. I think a carefully-chosen German word was probably translated as an archaic English word and then never revisited. Unfortunately we do not have the original German text to consult.


14

The American proposal also had the idea of controlling statements by boolean expressions. It didn't even have an "if" keyword. Instead, if an expression was followed by an arrow, the expression controlled the statements that followed the arrow.

The notation doesn't make it obvious, but unlike the German block of ifs, in the American form, if one expression in a block evaluated to true, it did short-circuit and skip the following expressions, like "else if" does now.


15

The two organizations held a joint meeting and merged their proposals into a single document that they called the International Algebraic Language. Before long the language would be renamed to Algol.

The "if" statement in the combined document looks a lot like the German proposal, but eliminates the "always" statement to end the block. Instead, each "if" stands alone, and if you want multiple statements to be controlled by the same expression, you can use "begin" and "end" to group them together.

There is nothing like "else" in this formulation of "if."


16

However, Algol 58 also had a second conditional form, called "if either." In this form, you could say "or if" to do the same thing as we now do with "else if." But there is nothing equivalent to "else" by itself, without another "if."


17

The story gets muddy the next year, when several papers about Algol were presented at a conference. One of them was the paper where John Backus, who had previously led the Fortran project, introduced the idea of formal grammars for programming languages.

Algol as he describes it doesn't have the "if either" conditional form. Instead, it has a keyword called "converge," which makes the top-level "ifs" inside its block behave like "else ifs." It is not clear whether this was meant as an idea for a better "if either," or whether it was an earlier idea that had already been rejected in favor of "if either."


18

Either way, "if either" would soon be replaced. In 1957 John McCarthy at MIT had written a proposal for the project that became LISP, another extremely old programming language that still survives today. His proposal introduced the idea of the conditional expression, as opposed to the conditional statement.

The important distinction is that an expression must always have a value, while a statement can simply not be performed if the condition that controls it is not true.

So his "if function" always ends with a clause called "otherwise" that gives the value of the expression when none of the other conditions have evaluated to true.


20

McCarthy's conditional expressions inspired Klaus Samelson to clean up and unify Algol's two separate conditional models at the end of 1959. He eliminated the entire "if either" form, leaving only the plain "if," but added a clause called "else" that would be performed when the expression controlling the corresponding "if" had been false.

You could chain conditions together with "else if," just like the previous "if either" had allowed with "or if," but you could also use "else" by itself for a final set of statements that would run if none of the conditions had been true.


20

"If-then-else" was the single conditional form that appeared in the Algol 60 report the next year, and is the form that almost all subsequent programming languages have followed.

Although it seems clear to us now, it was hard for people in 1960 to think about, and the report spends a page and a half explaining how it works, including this arrow diagram.


21

As I mentioned, the use of "else" as a conjuction sounds strange. Christopher Strachey's CPL programming language is the grandparent of C and therefore the ancestor of most current programming languages, and he refused to use "else," calling it "ignorantly incorrect English." He thought we should write "test … then … or" instead, which also doesn't sound very natural, and didn't catch on.


22

The MAD programming language also went its own way. It was known for its extremely long keywords, and in addition to using "whenever" instead of "if," it used "otherwise" instead of "else" and "or whenever" instead of "else if."

It is worth noting the indentation in the MAD manual's example. It took years before most other programming languages adopted this style of indenting conditions, even though it now seems unimaginable to do it any other way.


24

But even while CPL and MAD shunned the word "else," they kept the form and only changed the vocabulary. Language designers still search for the perfect way to write a for loop, but the quest for the perfect conditional form seems to have ended in 1959. Thank you Klaus Samelson, for giving us a tool to think with and a word to puzzle over.

联系我们 contact @ memedata.com