Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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.


To be fair that's just vanilla English orthography.


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?


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


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.


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.


Sure, which is what the rules always say, and is fairly straightforward. The hard part is actually using it because it usually looks so out of place to me. It's a chicken and egg problem, where semicolons are hard to comfortably use because they are so rare they look out of place when I try, and I assume part of the reason for their rarity is other people feel the same way.


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


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


I feel like the semicolon-period thing of Erlang is only really frustrating for the first day you're using it. After that, I actually found it to be a fairly natural syntax, and had little trouble switching between it and JS.


Erlang's usage of commas, semi-colons and periods is based on Prolog's use of these punctuation-marks. (Erlang was developed in Prolog and retains many similarities to it).


Just a small nitpick: your code is not recursive, you are missing 'f' on the right hand side.


Neither is Knuth's definition; it's just mapping an input to an output.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: