2024-11-19 07:17:00
xahlee.info
By Xah Lee. Date: . Last updated: .
OCaml’s let … = … in …
is very confusing. When nested, you can’t tell how it’s nested.
let wafin fd ?pos ?len buf = let loc = "wafin" in let pos = gop ~loc pos in let len = gol buf ~pos len in csa ~loc buf ~pos ~len; uw fd buf ~pos ~len ;;
another problem is that you have global and local declaration with the same syntax, but no bracket to indicate scope. Example:
let … = …
let … = … in …
another problem is that function definition also uses the same syntax.
Example:
let a = …
→ ( constant, or function of no arg )let a b = …
→ ( function with 1 arg )let a b c = …
→ ( function with 2 args )
though the last point is consistent, because a 0 arity function without side-effect is just constant.
but the effect of all these is that in a ocaml source code you’ll have nested let of the above. Quite confusing to read.
MetaLanguage has an “end”
In OCaml’s parent language ML, it has a “end”, like this:
let … = … in … end
This is better in the sense that it more clearly indicates the begin and end points of the expression.
Not really better in practice because now it’s more typing.
The advantage of clearer indication of nesting doesn’t really apply, because in practice, the code is broken down to lines, with proper indentation.
see also
Nested Syntax: Brackets vs Begin/End vs Indentation
History of OCaml Syntax, and The Next 700 Programming Languages
Here is the lineage of OCaml’s syntax:
- ISWIM (1966)
- ML (1973)
- Caml (1985)
- OCaml (1996)
Note that OCaml syntax came from ML, and ML syntax came from a article describing an ideal language called ISWIM, described in the paper:
History of OCaml and Haskell Syntax, and The Next 700 Programming Languages
What Could be Better?
So, if we stick with OCaml/ML’s style of syntax, what could be better for the “let” syntax?
That’s a hard question, because ML syntax is really a odd ball. It somewhat wanted to be like English.
Then, we trace its history, indeed, in ISWIM, it seems to be one man’s
whim where it espouses a notation that’s half English, half math notation,
and intended to be the syntax for the next seven hundred languages.
Facebook Fix to OCaml Syntax
If we do not stick to OCaml style of syntax, then, there’s 2 obvious possibilities.
One is going with lisp. Another, is going with C-syntax.
Facebook created Reason, which is OCaml in JavaScript syntax.
https://reasonml.github.io/
See also:
OCaml Tutorial
Keep your files stored safely and securely with the SanDisk 2TB Extreme Portable SSD. With over 69,505 ratings and an impressive 4.6 out of 5 stars, this product has been purchased over 8K+ times in the past month. At only $129.99, this Amazon’s Choice product is a must-have for secure file storage.
Help keep private content private with the included password protection featuring 256-bit AES hardware encryption. Order now for just $129.99 on Amazon!
Support Techcratic
If you find value in Techcratic’s insights and articles, consider supporting us with Bitcoin. Your support helps me, as a solo operator, continue delivering high-quality content while managing all the technical aspects, from server maintenance to blog writing, future updates, and improvements. Support Innovation! Thank you.
Bitcoin Address:
bc1qlszw7elx2qahjwvaryh0tkgg8y68enw30gpvge
Please verify this address before sending funds.
Bitcoin QR Code
Simply scan the QR code below to support Techcratic.
Please read the Privacy and Security Disclaimer on how Techcratic handles your support.
Disclaimer: As an Amazon Associate, Techcratic may earn from qualifying purchases.