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

后端 未结 6 453
日久生厌
日久生厌 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:34

    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.

提交回复
热议问题