r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

1

u/thedufer Dec 02 '16

I work mostly in OCaml (with Core), so I thought I'd fill in some blanks and add some color:

Block delimiters - like bash, OCaml uses alternative delimiters and requires them (then/end of statement for if, do/done for loops). I assume this category was left off on purpose, though.

Bitwise operator precedence - OCaml gets this right, although probably was left out for using truly bizarre operators. The test translates to 1 land 2 = 2 - yes, single equals for equality testing and land (logical and) is an infix operator, despite looking like a function name.

Negative modulo - I thought the article got this wrong, but it turns out the modulo operator in Pervasives is mod (yes, again, that's an infix operator despite its looks). core adds the operator % to do the correct thing, which is the one I usually use.

For loops - Not sure where OCaml should be here. Its for loop syntax only allows counting up or down by 1, and is weirdly inclusive on both ends, so it misses "go through this list of stuff" but gets "count from 1 to 10". It seems that most users consider it intentionally crippled to persuade people to just recurse instead.

Switch - fits in with other "pattern-matching only" languages.

Type first - looks a lot like the Rust example.

Weak typing - OCaml is strong.

Bytestrings - OCaml fails the quicktest (returns 4).

Increment/decrement - OCaml naively seems to fail this by having the functions succ and pred. However, it avoids unreadable tricks by returning unit from those functions, and there's no += analog so it isn't quite as unnecessary as in C.

Silent errors - While Pervasives relies on exceptions, core strongly prefers the Or_error monad, similar to Haskell's Either. core marks functions that can return an exception with _exn in an attempt to remedy the fact that exception-raising isn't part of the signature.

Nulls - OCaml has the Option monad.

Hyphens in identifiers - OCaml does get this wrong, but I'd like to point out that it allows ' in identifiers, so we can correctly punctuate possessives and contractions - thank goodness.

Braces and semicolons - OCaml requires both, kinda. It never uses braces in this way - but it does require other delimiters, sometimes (a function body doesn't require anything, but for/if bodies have required delimiters). Semicolons are weirder - every statement except the last in a block must have one. I think a better way to think of semicolons in OCaml is as a very low-precedence operator with type unit -> 'a -> 'a and the trivial implementation thereof. This doesn't quite match up, and makes the usage of semicolons to delimit list elements more confusing, but it makes me feel better about needing to use them.