OCaml: related expressions v. Functions

Here we have a function definition:

let fx = x + 3;; 

Here is the expression:

 let g = 4;; 

Can g just be considered a constant function that takes no arguments? Is there any difference?

+4
source share
3 answers

Yes. From a fully functional point of view (for example, in Haskell) all functions ( Really all ).

And since a purely functional language prohibits any changes, this definition does not bear any contradictions.

Is there any difference?

Well, OCaml is not purely functional. This means that functions can perform side effects that are slightly different from determining a constant value.

This code (F # here - but very similar in Caml) would be absolutely right.

 let name = Console.Write("Enter your Name: ") Console.ReadLine() 
+5
source

Difference between

 let f() = expr 

and

 let f = expr 

in an unclean language means that the “effects” of the expr expression are executed on each “call site” in the first case, and only once on the definition site in the latter case. This is one of the few differences between them, but perhaps the most important.

+5
source

Technically, the definition of variables is pattern matching:

 let [x] = someList let y::zs = someList let (Some z) = someOption let _ = someIgnoredExpr 
+1
source

All Articles