Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An OCaml odd thing

Tags:

ocaml

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.

like image 229
Surikator Avatar asked Feb 20 '11 02:02

Surikator


People also ask

What does :: mean in OCaml?

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.

What does fun do in OCaml?

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.

How do I return a value in OCaml?

OCaml doesn't have a return keyword — the last expression in a function becomes the result of the function automatically.


1 Answers

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.

like image 62
Benoît Fraikin Avatar answered Jan 20 '23 02:01

Benoît Fraikin