Alex Rivera | Logout

Why are functions in OCaml/F# not recursive by default?

Asked 2009-05-23T00:59:13.607
117

Why is it that functions in F# and OCaml (and possibly other languages) are not by default recursive?

In other words, why did the language designers decide it was a good idea to explicitly make you type rec in a declaration like:

let rec foo ... = ...

and not give the function recursive capability by default? Why the need for an explicit rec construct?

Edit
Report

1 Answer

4

Given this:

let f x = ... and g y = ...;;

Compare:

let f a = f (g a)

With this:

let rec f a = f (g a)

The former redefines f to apply the previously defined f to the result of applying g to a. The latter redefines f to loop forever applying g to a, which is usually not what you want in ML variants.

That said, it's a language designer style thing. Just go with it.

answered 2010-12-22T05:30:16.877

Your Answer