
Donald Knuth used an Erlang-like notation - old_sound
http://videlalvaro.github.io/2016/10/knuth-first-erlang-programmer.html
======
fusiongyro
Knuth is simply defining a piecewise function here in ordinary math notation.
Functional programming languages borrow heavily from math, Erlang is a
functional language. The resemblance would probably be even more striking with
Haskell... which only serves to undermine the message.

Erlang's great though. Glad to see some irrational exuberance for it.

~~~
nayuki
Knuth's function would be written in Haskell as thus:

f [n] = [n]

f [m, n] = [m, n, 0, 1]

f [m, n, r, 1] = [m, n, mod m n, 2]

f [m, n, r, 2] = if r == 0 then [n] else [m, n, r, 3]

f [m, n, p, 3] = [n, p, p, 1]

It needs to be implemented with variable-length lists instead of fixed-length
tuples, in order to satisfy the type system.

~~~
old_sound
For me what's striking about Erlang syntax is the meaning of the semi-colon
and the period, as function clause separator. Most of the rest is just vanilla
FP lang syntax.

~~~
DanielMcLaury
To be fair that's just vanilla _English_ orthography.

~~~
old_sound
Yup, and still people get confused at Erlang's use of semi-colons and periods.

Do you think it is because other programming languages don't uses the semi-
colon that way? Or that periods are usually used to access class members, but
not to finish function declarations?

~~~
klibertp
I think it's because most people have no idea how to use semicolons in natural
languages too.

~~~
kbenson
I think this is true. Anecdotally, I've looked up the usage of semicolons a
few times now, and I _think_ I know how to use them, but every time I'm about
to I just rephrase what I'm saying so I don't have to. They are used so rarely
in English it's hard to feel confident through exposure.

~~~
adrianratnapala
I use semicolons all the time; they are easy and useful.

The two clauses separated by a semicolon are sentences; thus the semicolon is
logically and grammatically equivalent to a full stop. (As long as you
normalise the capitalisation). The difference is in nuance, flow, and other
soft things.

So when do you use a semicolon instead of a full stop? Whenever you want to!

The really difficult punctuation mark in English is the _full_ colon.

~~~
petertodd
What's a full colon? Hard/NSFW to google for that...

~~~
dragonwriter
Just a plain colon, as an emphatic distinction from a semicolon.

------
pmontra
To the eye of a developer used to more modern languages that also shows some
bad choices in the design of the Erlang syntax. Basically everything that
makes the source code less readable than the original: uppercased variables,
the minuses that prefix the module and export statements, the wierd =:=
operator. I add the also wierd <> binary and string concatenation operator,
not used in this example.

The semicolon-period statement separators/terminators are in the original and
in natural languages, but newer languages proved them to be useless. Probably
compilers in the 80s needed some help by the programmer to be fast.

Elixir fixed some of the worst offenders, kept others and added something.
Examples: the <> is still there but at least we can interpolate strings Ruby
like, the useless do at the end of almost every defsomething declarations (the
compiler should get it by itself.)

But every languages has its wierdnesses, the contest is to have the least of
them.

~~~
OskarS
It's worth noting that Erlang gets a lot of that weirdness from its progenitor
language, Prolog, where arguably some of that stuff makes more sense. For
instance, the =:= operator is an equality operator that forces arithmetic
evaluation, unlike = which is just used for unification. In Prolog, this is a
fairly crucial difference ("1 + 1 = 2" is false in prolog, while "1 + 1 =:= 2"
is true) and there needs to be language constructs that make the distinction
clear. Given that, having different operators is a sensible solution to the
problem.

On the other hand, it's arguably true that Prolog has an over-reliance on a
massive swath of operators. All of these operators represent some variation of
the concept of equality: =, ==, =:=, =@=, #=, and "is". The differences make
sense if you're deep into Prolog, and it's rich capacity for designing new
operators is arguably a strength of the language, but it's clearly an obstacle
to beginners.

I absolutely adore Prolog, but it's a real shame that development of logical
programming basically stopped with it. It would be as if functional
programming never properly progressed beyond early Lisps. Its a shame that
Erlang borrowed so much of its syntax, when the syntax is clearly not its
strongest suit (though it fits Prolog better than Erlang).

~~~
debacle
> The differences make sense if you're deep into Prolog

They make sense academically, but they only slow down language adoption.

------
unboxed_type
Its interesting how you squeezed 'Erlang language' into some syntax choices.
Erlang is about actor model, not about pattern matching. Joe had his
inspiration from Prolog - it is a well-known fact from publicly available
sources.

Anyway, thanks for the article, it sparked some emotions in my brains :-)

~~~
old_sound
I know all that. Long time Erlang dev, former RabbitMQ core dev, so yeah, I've
heard already about the Prolog story. Interesting you talk about the Actor
Model, since even Robert Virding has said that when they created Erlang they
had no idea there was a thing called "Actor Model".

~~~
unboxed_type
Wow! Ok. Well yes, as far as I know both Joe and Rob agree on that it is
accident that Erlang have implemented 'actor model' thing. Also its
interesting to note that functional nature of Erlang is very different to ML-
family languages. Authors used all these functional stuff because of
pragmatics in the first place and not because of solid theory underpinnings.

~~~
old_sound
Yup. See this convo
[https://twitter.com/old_sound/status/788349221946662912](https://twitter.com/old_sound/status/788349221946662912)

------
Xophmeister
Joe Armstrong's comment on this post is gold just for this:

    
    
        But as Abraham Lincoln said - "don't believe everything you read on the Internet"

------
peterkelly
It's called pattern matching, and is present in most functional languages -
Haskell is a good example of something which uses this style extensively.

In a sense it's derived from the way that some mathematical functions are
expressed, e.g. i've seen the fibonacci sequence expressed in this manner a
few times.

The only thing this excerpt from the book has to do with Erlang is that they
both used the same (existing) idea.

~~~
old_sound
As mentioned already:

>For me what's striking about Erlang syntax is the meaning of the semi-colon
and the period, as function clause separator. Most of the rest is just vanilla
FP lang syntax.

It's strikes me that everybody notices the pattern matching part, but not the
way the clauses are separated, which AFAIK, it's something used by Erlang
Syntax only.

And yeah, after working in Erlang and other FP langs for the past 6 years I
kinda know this is called pattern matching ;-)

~~~
kqr
> _it 's something used by Erlang Syntax only._

Well... and English syntax. In English, you can specify a list of things by
separating each thing with a semicolon and then a sentence is terminated by a
period. Example:

> _Today I would like you to buy coffee; talk to Sarah about the party if you
> can, otherwise send her a text; drop off the kids; meet me at soccer
> practise._

You see how the sentence is terminated with a period, each individual item in
the list is separated by a semicolon, and the comma is free to use inside each
item to indicate related clauses. I've changed nothing about the structure of
that list, except replaced the items with function definitions.

> _Today I would like you to define f(m, n, r, 1) as f(m, n, remainder of m
> divided by n, 2); f(m, n, r, 2) as n if r is 0, otherwise as f (m, n, r, 3);
> f(m, n, p, 3) as f(n, p, p, 1)._

It's the same thing, only we've replaced the mundane everyday tasks with
function definitions. Now reformat the thing and we get

    
    
        Today I would like you to define
    
          f(m, n, r, 1) = f(m, n, remainder of m divided by n, 2);
          f(m, n, r, 2) = n              if r is 0, 
                          f (m, n, r, 3) otherwise;
          f(m, n, p, 3) = f(n, p, p, 1).
    

Sure, Erlang syntax happens to coincide with the syntax of English, and I'm
not sure whether this was intentional or not, but it certainly has nothing to
do with Knuth.

~~~
old_sound
Really you can use semi-colons on a sentence and then end the sentence with a
period? And I thought I had learned something during all my years studying
linguistics and grammar.

BTW, newsflash: many languages use semi-colons inside sentences. Spanish for
example, which could be argued to be older than English. So did Erlang copy
Knuth, or Erlang actually copied El Cid Campeador? I think Erlang comes from
El Cantar del Cid.

~~~
kqr
I just find it more likely that Knuth continued to use English for those
definitions, and not that he switched to Spanish, or for that matter Erlang.

~~~
old_sound
For me it's pretty clear he was trying to write the algorithms in Spanish,
therefore the semicolons.

------
sotojuan
Doesn't most of this syntax come from Prolog?

~~~
Turing_Machine
Likely that was its direct source (Erlang was originally developed in Prolog,
IIRC). However, the first volume of TAOCP came out in 1968, while Prolog dates
from 1972. It's possible that the Prolog team got the notation from Knuth, and
then Armstrong, et al carried it forward into Erlang.

~~~
4ad
Knuth didn't invent any notation here, it's just standard math.

There are a million languages out there that resemble math notation (mostly
functional languages), many of them resemble it much better than erlang does.

------
pwdisswordfish
This article reminds me of the "JavaScript is Scheme" meme from a while ago.
In that people wanted to increase the prestige of their favourite(?) language
by shoehorning it into a comparison with something high-status; which
comparison, when you think about it, doesn't make much sense.

> Interesting curiosity, which proves that the Erlang Syntax is some sort of
> Platonic Language Ideal that predates all programming languages.

Because it supports definition by cases and an if-then-else construct? And
because it uses the same punctuation marks that ordinary natural language
sentences do? Give me a break.

~~~
old_sound
Go get your break, while you are at it, get a laugh or two, because that's
what this article is supposed to be about. nobody is so silly to pretend Knuth
was describing Erlang on his book.

------
yellowapple
Are we sure it ain't just Prolog?

In particular, Erlang's syntax is heavily derived from Prolog's, so it's
natural to conclude that Prolog would be the closer relative to Knuth's
syntax.

In reality, though, it's probably the other way around; Prolog's syntax is in
turn more-or-less derived from the conventions in traditional math formulas
(especially with the uppercase variables and such).

------
4ad
Author confuses standard math notation with erlang, missing the fact that
other, more pure, functional programming languages even more closely resemble
math notation.

Nothing to see here.

~~~
rdtsc
> Author confuses standard math notation with erlang, missing the fact that
> other,

The article is silly whimsy, you know that? Knowing the author from online
presence, I can assure you the author doesn't really think Knuth was writing
Erlang at that time.

> that other, more pure, functional programming languages

Maybe the author doesn't care about other more pure functional programming
languages. They care about Erlang so they wrote about it.

------
rpcope1
Related:
[https://www.youtube.com/watch?v=rRbY3TMUcgQ](https://www.youtube.com/watch?v=rRbY3TMUcgQ)

------
KeytarHero
Uhh, TAOCP predates Erlang by quite a bit. Shouldn't the headline be, "Erlang
uses a Donald Knuth-like notation"?

~~~
rdtsc
> TAOCP predates Erlang by quite a bit.

Are you sure Knuth wasn't secretly programming in Erlang since the early 50's.
He is considered one of the original inventors, it was just too controversial
so it was covered up all this time.

I guess we need special whimsy and/or sarcasm tags so people don't take them
too seriously...

------
GnarfGnarf
Knuth also created in TeX the notation used by Microsoft's Rich Text Format
(RTF): {\b bold} etc.

------
ljbx
omg, wrong way round. It's more math or logic notation.

~~~
old_sound
More like satire logic notation, but who am I to tell at this point.

~~~
totallymike
Honestly I can't tell sometimes if HN is all just satirical internet comments.
I read the article, chuckled at the exchange with Joe Armstrong, then opened
the HN comments to look at all the grumbling.

Turns out there's plenty.

------
dang
This is a good case of where the HN guidelines call for changing a title of an
article: "Please use the original title, unless it is misleading or linkbait."
[https://news.ycombinator.com/newsguidelines.html](https://news.ycombinator.com/newsguidelines.html)

A good HN title is accurate, neutral, and close to the language of the
original article. If anyone suggests a better title, we're always happy to
change it again.

~~~
old_sound
It's not misleading or linkbait. It's just humor. Don't you have humor
guidelines at HN?

~~~
dang
Jokes certainly have their place but they're context-dependent, i.e. the same
title can work fine in one place but not another.

~~~
old_sound
Is the website title HackerNews not linkbait or misleading? I ask since I
usually don't see many "hacker News" around here…

~~~
dang
It's a category error to conflate article titles and website names, but I'll
play along.

Misleading? Arguably, but I don't think it's much of a stretch from "Hacker
News" to "news for hackers" to "anything that good hackers would find
interesting", which is the mandate of the site
([https://news.ycombinator.com/newsguidelines.html](https://news.ycombinator.com/newsguidelines.html)).

Linkbait? I doubt it, but I'm biased.

------
gohrt
Clickbait title is clickbait.

minimal-syntax languages tend to look similar.

