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

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

    A big part of it is that it gives the programmer more control over the complexity of their local scopes. The spectrum of let, let* and let rec offer an increasing level of both power and cost. let* and let rec are in essence nested versions of the simple let, so using either one is more expensive. This grading allows you to micromanage the optimization of your program as you can choose which level of let you need for the task at hand. If you don't need recursion or the ability to refer to previous bindings, then you can fall back on a simple let to save a bit of performance.

    It's similar to the graded equality predicates in Scheme. (i.e. eq?, eqv? and equal?)

提交回复
热议问题