Surikator
Surikator

Reputation: 1463

An OCaml odd thing

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.

Upvotes: 12

Views: 535

Answers (3)

Fabrice Le Fessant
Fabrice Le Fessant

Reputation: 4274

That's a very useful feature to avoid bugs and ease the reading of big integers:

1_000_000_000 is easier to read than 100000000 (did you notice I forgot a zero ?).

Upvotes: 6

Benoît Fraikin
Benoît Fraikin

Reputation: 582

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.

Upvotes: 4

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

Underscores are allowed in numbers (and ignored) in OCaml. From http://www.cs.ru.nl/~tews/htmlman-3.10/lex.html#xhtoc5:

For convenience and readability, underscore characters (_) are accepted (and ignored) within integer literals.

Upvotes: 19

Related Questions