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

后端 未结 6 428
日久生厌
日久生厌 2020-11-29 18:36

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

6条回答
  •  执笔经年
    2020-11-29 19:35

    There are two key reasons this is a good idea:

    First, if you enable recursive definitions then you can't refer to a previous binding of a value of the same name. This is often a useful idiom when you are doing something like extending an existing module.

    Second, recursive values, and especially sets of mutually recursive values, are much harder to reason about then are definitions that proceed in order, each new definition building on top of what has been already defined. It is nice when reading such code to have the guarantee that, except for definitions explicitly marked as recursive, new definitions can only refer to previous definitions.

提交回复
热议问题