Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do these two function definitions in OCaml differ?

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?

like image 335
SoftTimur Avatar asked Jun 03 '26 20:06

SoftTimur


1 Answers

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.

like image 196
Jeffrey Scofield Avatar answered Jun 06 '26 22:06

Jeffrey Scofield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!