Live data from Hacker News

If-then-else had to be invented

github.com

101–110 of 255 posts

Re: If-then-else had to be invented

#101
> 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.

Sure you can!

Shakespeare used 'else' in the same sense as 'otherwise': https://www.shakespeareswords.com/Public/GlossaryHeadword.as...

"else used as a conjunction" https://wordtype.org/of/else

"Or else!" was common speech before computers, and a common vague cartoon/cowboy/sign threat.

Re: If-then-else had to be invented

#102

I remember there was a paper that tried to compare natural language usage of common programming language keywords to see if they may come up with other constructs to create a programming language that is easier to learn as your first one. There were a few interesting observations. The one that stuck with me the most is that 'and' is most commonly used with almost the opposite sense in natural language vs programming:…

Great observations. In the same idea, "or" is inclusive in all of programming and CS, i.e. A and B => A or B, but in natural languages (as least the ones I know) "or" is almost always exclusive, i.e "do you want to eat pizza or pasta?" does not imply that eating both is an option. This is of course related to why the famous joke "Is it a boy or a girl? Yes." is so hilarious.

If you say it out loud, you will realize that the question "Do you want to eat pizza or pasta?" (exclusive-or case with the answer "pizza" or "pasta," i.e. in the sense "these are your two choices, which would you prefer?") is an entirely different utterance from "Do you want to eat pizza or pasta?" (inclusive-or case with the answer "yes" or "no," i.e. in the sense "would you like to go to an Italian restaurant?"). The first case is pronounced with primary stress on the first syllables of both "pizza" and "pasta," and with a high tone on "pizza" and a low tone on "pasta," indicating a contrast between the two alternatives. The second case is pronounced with primary stress on the first syllable of "pizza," secondary stress on the first syllable of "pasta," and a uniform tone over the phrase "pizza or pasta" on which is superimposed the rising tone on the last syllable of the sentence indicating a yes/no question. (To see that the latter is true, add "tonight" to the yes/no sentence - "would you like to eat pizza or pasta tonight?" The tone does not rise until the syllable "night.")

You have to be very careful in reasoning about natural language based solely on the written record, since spoken language regularly includes features, such as the suprasegmentals (stress and intonation) in the above examples, that aren't recorded in the orthography.

Re: If-then-else had to be invented

#103

I remember there was a paper that tried to compare natural language usage of common programming language keywords to see if they may come up with other constructs to create a programming language that is easier to learn as your first one. There were a few interesting observations. The one that stuck with me the most is that 'and' is most commonly used with almost the opposite sense in natural language vs programming:…

>I remember there was a paper that tried to compare natural language usage of common programming language keywords to see if they may come up with other constructs to create a programming language that is easier to learn as your first one.

Wasn't that the goal of Perl? It was inspired from natural language first, not inspired from another programming language first. This is why it's so quick and easy to prototype in it, arguably far easier than any other language. I believe it is the only programming language to be inspired from natural language first.

Re: If-then-else had to be invented

#104

Earlier quoted context omitted.

Having taught programming to novices a little, I believe “if not” is clearer than “else”. Imagining “not” being prepended to the original conditional expression would be natural, especially in languages with “not X” construct (like Python), and it’s easy to forget that “else” is in fact archaic to non-programmer ears, slightly and possibly needlessly raising the barrier. if (X) { doY(); } if not /* X is implied */ {…

I wonder whether anyone has used "otherwise" for this. It'd be a simple alias for "else", but it'd certainly sound natural: if (X) { doY(); } otherwise { doZ(); }

Towards the bottom of the article there is a example of MAD[0] that uses something similar, and there is also the John McCarthy proposal which also uses otherwise. I could imagine it being used in a language that had a tight connection to maths as otherwise is often used there.

[0]: https://en.wikipedia.org/wiki/MAD_(programming_language)

Re: If-then-else had to be invented

#105

I remember there was a paper that tried to compare natural language usage of common programming language keywords to see if they may come up with other constructs to create a programming language that is easier to learn as your first one. There were a few interesting observations. The one that stuck with me the most is that 'and' is most commonly used with almost the opposite sense in natural language vs programming:…

The word “and” in English is simply there to join multiple clauses together. Whether it is a set union or set intersection, (or some other relations) depends on the sentence. Set union: they can read and write’ Set intersection: my jeans were creased and dirty Sequential: he turned round and walked out Causal: there was a flash flood and by the next morning the town was under water’ And, there are many more usages.

To join like constituents, not just clauses, as per your examples.

Re: If-then-else had to be invented

#106
post #12

It begs the question: did we search and find the answer of if-then-else, or did we find an answer that was sufficiently likeable and usable- but one of many possibilities? How do we know whether this choice was the global optimum vs a local one? Is if-then-else the electron- a deep truth waiting to be discovered- or the gasoline powered car- an option we explored for decades among others options.

In regards to global vs local optimum it is useful to look at the alternatives like pattern matching.

For languages that don't support pattern matching or passing in functions as values, "if" is probably the best you can do with. You might be able to do something with boolean short-circuit operators but it's debatable if that that is better than the if statement.

In React, if you are using JSX, they don't have "if" statements within the markup they just use the boolean short-circuit (&&) operator.

For more ML style languages that support pattern matching, it is often easier to express solutions that way than with traditional if statements. In those situations I would say that "if" is less desirable in terms of expressive power and code readability.

Also, "if / else" only supports 2 cases, with pattern matching you can have an arbitrary amount of cases.

Without pattern matching, expressing more than 2 cases becomes a combination of nested if statements combined with "&&" and "||" expressions. This makes it much harder to read and write.

Re: If-then-else had to be invented

#107

The story of development is interesting, but I doubt that the concept itself needed invention. Even in machine language using nothing but conditional branching the pattern would have appeared enough to be an idiom perhaps without a name: ... b? addr1 ...else stuff jmp addr2 addr1: ...then stuff addr2: ...always stuff What's more interesting is the generalization to any boolean expression, not the naming/placement of…

Yep. I remember naturally writing assembly like that when I was a kid. When I picked up a high level language if statements were not mind blowing, it was already what I had been doing the entire time.

What did blow me away was seeing a function for the first time.

Re: If-then-else had to be invented

#108
post #12

It begs the question: did we search and find the answer of if-then-else, or did we find an answer that was sufficiently likeable and usable- but one of many possibilities? How do we know whether this choice was the global optimum vs a local one? Is if-then-else the electron- a deep truth waiting to be discovered- or the gasoline powered car- an option we explored for decades among others options.

Is pattern matching a generalization of if/then/else, a reframing of it, or something else entirely?

If-then-else is pattern matching on a value of type Bool. I think of general pattern matching on algebraic data types as a generalization.

Re: If-then-else had to be invented

#109

Interesting timing. I just read https://arstechnica.com/?post_type=post&p=1728174 due to r/programming https://www.reddit.com/r/programming/comments/kbs11a/a_damn_... yesterday. Apparently Cambridge Programming Language (more or less a failed language that became proto-C) designer Christopher Strachey had some misgivings about this usage of "else".

[deleted]

Re: If-then-else had to be invented

#110
post #104

Earlier quoted context omitted.

I wonder whether anyone has used "otherwise" for this. It'd be a simple alias for "else", but it'd certainly sound natural: if (X) { doY(); } otherwise { doZ(); }

Towards the bottom of the article there is a example of MAD[0] that uses something similar, and there is also the John McCarthy proposal which also uses otherwise. I could imagine it being used in a language that had a tight connection to maths as otherwise is often used there. [0]: https://en.wikipedia.org/wiki/MAD_(programming_language)

Oh, right, the article...
Post reply on HN