3 ms·
I guess for historical and stylistic reasons the OCaml people wanted to allow redefining variables while using the old values in the definition. So you can say
by sampo 12y ago
I guess for historical and stylistic reasons the OCaml people wanted to allow redefining variables while using the old values in the definition.
So you can say
let process data =
let data = sanitize data in
f(data)
instead of
let process data0 =
let data1 = sanitize data0 in
f(data1)
And because OCaml uses the same let syntax for variables and functions, this also lets you to
let f x = x * x
let f x = f (f x)
# now f(3) will be 81
So we can define new f using the definition of old f.
In some cases I can see the point of overwriting variables, instead of using new names like data0, data1, data2, but in the case of functions, redefining functions while utilizing the previous definition in the body of new definition will probably not catch on.
But anyway, because this is possible, OCaml needs another way to signify the that f used in the definition of f is not meant to refer to a previous definition, but the current one.
Hence, let rec.
Also, while marking a single function recursive maybe does not make much difference when reading source code, it's kind of nice to read code when a group of mutually recursive functions is explicitly marked as such.