Title here
Summary here
OCaml supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
This function int_seq
returns another function, which we define anonymously in the body of int_seq
. The returned function closes over the variable i
to form a closure.
In OCaml, we use a reference (ref
) to create a mutable variable i
. The inner function updates this reference and returns its value.
To run the program, save it as closures.ml
and use the OCaml compiler:
The last feature of functions we’ll look at for now is recursion.