Declaring order in let-bindings, Haskell vs OCaml

In Haskell, the declaration order in let / where clauses does not matter, for example:

fx = let g1 xy = if x>y then show x else g2 yx g2 pq = g1 qp in ... 

where g2 used in g1 before its declaration. But this does not apply to Ocaml:

 # let a = b in let b = 5 in a;; Warning 26: unused variable b. Error: Unbound value b 

Is there a reason OCaml doesn't behave like Haskell? In the absence of a direct declaration, this function seems to me useful.

Is it because of strict evaluation in OCaml, but lazy in Haskell?

+6
source share
4 answers

Not severity as such, but a symptom of the same problem.

Ocaml is not purely functional, i.e. arbitrary function calls can perform arbitrary I / O. This requires that they work in a predictable manner, which requires both rigor and an orderly order.

+7
source

OCaml uses "let rec" to indicate when a binding in a group can refer to each other. Without an additional "rec", the binding should be in order from top to bottom. See "Local Definitions" at http://caml.inria.fr/pub/docs/manual-ocaml/expr.html for details.

+18
source

At Haskell, this is not due to lazy appreciation. Figuring out what name refers to what happens at compile time, when nothing is done in any case (strictly or lazily). Remember that in Haskell you only write mathematical definitions, not commands that must be executed in order. The order in which you write these definitions does not matter. If you say

 a = expr1 b = expr2 

or

 b = expr2 a = expr1 

this means that a is defined as expr1 , and b is defined as expr2 .

I do not know OCaml, so I can not say anything about it.

+5
source

Haskell Ends the Archaic Difference let vs. let rec . A worthy compiler can see very well if it is a simple let or one that contains mutually recursive definitions and can act accordingly.

(I wonder what the ML compiler does if you start with let rec and then only have non-recursive definitions. Of course, this let / let rec thing is an unpleasant obstacle when you do some kind of refactoring.)

-1
source

Source: https://habr.com/ru/post/903183/


All Articles