I am learning Jason Hickey's Introduction to Objective Caml. Just have a question about the expression thing.
So it says:
Definitions using let can also be nested using the in form.
let identifier = expression1 in expression2
The expression expression2 is called the body of the let. The variable named identifier
is defined as the value of expression1 within the body. The identifier is defined only in
the body expression2 and not expression1.
A let with a body is an expression; the value of a let expression is the value of
the body.
let x = 1 in
let y = 2 in
x + y;;
let z =
let x = 1 in
let y = 2 in
x + y;;
val z : int = 3
Ok. I don't understand much about the above statements.
First
The variable named identifier
is defined as the value of expression1 within the body. The identifier is defined only in
the body expression2 and not expression1.
What does this mean? So identifier
is the value of expression1
, but only in the body expression2
? Does it mean that identifier
is effective only in expression2
but has the value of expression1
? Then does defining identifier
make sense, as it is only in expression2
?
Second
Let's see the example:
let x = 1 in
let y = 2 in
x + y;;
So I don't see the point of this let
statement. x = 1
for sure, what's the point of giving a body of let y=2 in x+y;;
?
Third
let z = let x = 1 in let y = 2 in x + y;;
So how can I sort out the logic of this statement?
if take this definition form: let identifier = expression1 in expression2
What's the expression1
in the let
statement above? Is it let x = 1
?
Can anyone tell me the logic of nesting let
in a kind of Java
way? or more understandable way?