DEV Community

Andrey Frolov
Andrey Frolov

Posted on • Updated on

Variables

It's an expression and ;; says to ocaml treats this line as expression

3 + 4 ;;
Enter fullscreen mode Exit fullscreen mode

And heres variable declaration or let binding

let x = 3 + 4 ;;
(** val x : int = 7 *)
Enter fullscreen mode Exit fullscreen mode

After a new variable is created, the toplevel tells us the name of the variable (x), in addition to its type (int) and value (7).

Also with let you can define a function

let square x = x * x ;;

(** val square : int -> int = <fun> *)

square (square 2) ;;

(** - : int = 16 *)
Enter fullscreen mode Exit fullscreen mode

Function in ocaml is a first class citizen.

Note: ;; is required only in repl, not in your source code.

Top comments (2)

Collapse
 
yawaramin profile image
Yawar Amin

;; says to ocaml treats this line as expression

Note that ;; is only needed in the REPL to tell it that the line is finished, not in source code.

Collapse
 
frolovdev profile image
Andrey Frolov

Yeah, my bad. I'll fix it now. Thanks for your feedback