Live data from Hacker News

If-then-else had to be invented

github.com

111–120 of 255 posts

Re: If-then-else had to be invented

#111
post #5

Ah, interesting, I wish the title had been prefixed with "What if ". There's a language where THEN basically means ENDIF... Forth, if memory serves me right?

Yes. It is used in the sense of the English statement: IF you go to the store get some butter THEN come right back. So THEN is where the program continues on a false condition. Typically Forth. It uses an alternative way of looking at things.

The GForth documentation addresses this. [0] Forth is simply adopting a different use of 'then' than the one more commonly seen in programming languages, but it's still faithful to English.

[0] https://gforth.org/manual/Selection.html

See also the StackOverflow thread on this topic: https://stackoverflow.com/q/12539634/

Re: If-then-else had to be invented

#112
Nice walk through history, interesting to think that at some point if-else was in need of diagrams to describe the concept clearly.

When I ran the "if-else" statement of my programming language* by a user-study with my non-technical brother-in-law he said it reminded him of the binary nature of legalise and inherently limiting the nature of what is possible. I agree.

My sister also formed the study-group and consistently they both prefer "otherwise" to anything else (pardon the pun).

* The language in case anyone wants to take a look: https://github.com/ducklang-community/ducklang

Re: If-then-else had to be invented

#113

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.

>> Set union: they can read and write’

>> Set intersection: my jeans were creased and dirty

IMHO those are both unions. One describes two capabilities people have while the other describes two conditions some clothing has.

They both become intersection when using the pair of conditions as selection or decision criteria.

It's the difference between an assertion and a question.

Re: If-then-else had to be invented

#114
Chinese also has an interesting sentence structure that doesn't quite fit with European languages. In addition to subject.verb(object) form, there is also a kind of topic-comment form that might be reasonable to use in some assertion-like contexts.

    ensure(foo), !empty()
    require(bar), open()
Chinese also has a word that can transform some kinds of statements into a question. `ma` is used to form yes-no questions. Imagine if languages with truthy values required you to use an explicit keyword/character in postfix position to take a non-boolean value and use it in boolean context.

    if predicate:
        pass

    if non_predicate ma:
        pass

To be clear, I'm not actually advocating for such constructs. The topic-comment form breaks down as soon as you want a predicate on multiple objects. The automatic truthy values concept is already falling out of favor in preference for explicit predicates like file_handle.is_open(). I don't actually want to program in "Cobol, but Chinese". But it is interesting to explore the space.

Re: If-then-else had to be invented

#115

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:…

> For example, 'women and children' means of course 'if X is a woman OR X is a child'.

Well, no, “women and children” alone doesn't mean that, “if X is a member of women and children” means “if X is in women OR X is in children”.

But that's consistent with AND and OR as boolean operators in programming (it's inconsistent with the way &-and-| symbols are used as set operators in some programming languages, though; ∪ and ∩ would be better, but aren't in ASCII.)

Re: If-then-else had to be invented

#116
post #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.

Yup, it grated on me to read that at the beginning.

Though: else as a conjunction isn't quite archaic today. But, it is one of those usages that when I encounter it, I begin to suspect that the document I'm reading is rather old.

Re: If-then-else had to be invented

#117

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:…

> For example, it's more likely you'll say something like 'give candy to these kids' or 'wash these cars'

That's one of the reasons I like functional programming. In ML-style languages you have something like `Set.map giveCandy kids`, which is both concise and close to natural language.

Comprehensions are also cool in my book, because they're very close to set-builder notation.

Re: If-then-else had to be invented

#118
The language REBOL used `either [] []` which I now miss regularly.

It was great in that by default it pushed to me consider and deal with `else` in every case.

There are allot of bugs left when stopping at 'if'.

Re: If-then-else had to be invented

#119
That's an interesting and well-researched article. I'd like to put if-then-else in the bigger historical context of "structured programming".

In the late 1960s and early 1970s, it was a big debate if programmers should use structured programming, building programs out of blocks with control structures such as iteration and conditionals. (Earlier programs were built from ad hoc control using goto.) This is the context of Dijkstra's famous "Go To Statement Considered Harmful.

Although structured programming seems obvious now, there was a lot of resistance. At first, people weren't even sure if structured programming could implement all the required control flows, so someone (Böhm–Jacopini) had to prove the "structured program theorem". Languages needed to be modified to support structured programming, e.g. adding block if-else to Fortran 77.

I have to wonder what modern programming debates will seem as obvious in retrospect as the victory of structured programming.

Re: If-then-else had to be invented

#120

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:…

> For example, it's more likely you'll say something like 'give candy to these kids' or 'wash these cars' than 'give a piece of candy to each kid' or 'wash each of these cars.

True but for more complex instructions, it’s common to say “do these things for that kid, then do that for all the kids”, which is pretty much a for loop. I can’t speak to how common this was in lay speech before programming was common though.

(There was an earlier thread about this but algolia’s search UI suddenly became a pain to use on mobile and it’s too tedious to find.)

Post reply on HN