Can anyone explain this OCaml toplevel behaviour?
# 1________________________________1;;
- : int = 11
(The big line is a sequence of underscores: '_')
Out of curiosity, this program compiles under ocamlc, too.
The ' is simply part of the variable name. And yes foo :: bar , where foo is an element of type a and bar is a list of type a, means "the list that has foo as its first element, followed by the elements of bar". So the meaning of the match statement is: If xs is the empty list, the value is 0.
Similarly, OCaml functions do not have to have names; they may be anonymous. For example, here is an anonymous function that increments its input: fun x -> x + 1 . Here, fun is a keyword indicating an anonymous function, x is the argument, and -> separates the argument from the body.
OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.
There is several programming language that accepts the underscore character as a non significant character in an integer. Ada, Perl, OCaml and probably some other language use it to separate thousand, millions and billions... but you can use _
anywhere inside the integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With