I have seen some implementation as follows:
let rec fact =
fun n ->
if n <= 0 then 1 else n * fact (n - 1)
Another implementation is:
let rec fact n =
if n <= 0 then 1 else n * fact (n - 1)
Could anyone tell me if there is any difference between these 2 styles?
These definitions are equivalent. The notation
let rec f a b c = <expr>
is a handy way of writing (syntactic sugar for):
let rec f = fun a b c -> <expr>
You can find this described in section 6.7.1 of the OCaml manual, under the heading Local definitions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With