OCaml Values and Functions
A value is the result of evaluating an expression: 42, "hi", [1; 2; 3], or a function. let gives a value a name. Names are bindings, not boxes: once bound, x always refers to the same value.
let width = 8
let height = 5
let area = width * height
let () = Printf.printf "area = %d\n" areaarea = 40
In the toplevel you would see the inferred type of each binding:
# let width = 8;;
val width : int = 8
# let area = width * height;;
val area : int = 40Defining functions
A function definition lists its parameters after the name, separated by spaces:
let square x = x * x
let add a b = a + b
let describe name age = name ^ " is " ^ string_of_int age
let () =
print_int (square 9);
print_newline ();
print_int (add 2 3);
print_newline ();
print_endline (describe "Grace" 85)81
5
Grace is 85
There is no return keyword: the body is an expression and its value is the result. The inferred types are:
val square : int -> int
val add : int -> int -> int
val describe : string -> int -> stringRead int -> int -> int as "takes an int, then another int, and returns an int". The compiler worked this out from the *, + and ^ operators, which only work on ints and strings.
Anonymous functions
fun creates a function without a name. let square x = x * x is just shorthand for let square = fun x -> x * x:
let cube = fun x -> x * x * x
let () =
print_int (cube 3);
print_newline ();
List.iter (fun word -> print_endline ("- " ^ word)) [ "map"; "filter"; "fold" ]27
- map
- filter
- fold
Anonymous functions are most useful as arguments to other functions, like List.iter above, which calls the function once for each list element.
Functions are values
Because functions are ordinary values, you can store them, pass them and return them:
let twice f x = f (f x)
let increment n = n + 1
let shout s = s ^ "!"
let () =
Printf.printf "%d\n" (twice increment 10);
Printf.printf "%s\n" (twice shout "hey")12
hey!!
twice works with any function whose input and output types are the same. Its inferred type is ('a -> 'a) -> 'a -> 'a, where 'a ("alpha") is a type variable meaning "any type". You will meet these in the polymorphism lesson.
Functions with no arguments
A function must take at least one argument. When there is nothing useful to pass, take (), the only value of type unit:
let counter_start () = 100
let say_hello () = print_endline "hello"
let () =
say_hello ();
print_int (counter_start ());
print_newline ()hello
100
Note:
let start = 100is a value computed once;let start () = 100is a function that computes it each time it is called. The difference matters when the body has side effects, such as printing or reading a clock.
Common mistakes
| Mistake | Fix |
|---|---|
add(2, 3) |
add 2 3 (parentheses with a comma build a tuple) |
square -4 |
square (-4) |
print_int add 2 3 |
print_int (add 2 3) |
Forgetting () when calling say_hello |
say_hello () |
Test yourself
What is the type of let add a b = a + b?
int * int -> intint -> int -> int'a -> 'a -> 'aint -> int
Which expression is equivalent to let double x = x * 2?
let double = fun x -> x * 2let double = x * 2let double () = x * 2fun double x = x * 2
How do you call let greet () = print_endline "hi"?
greetgreet ()greet()is invalidcall greet