【发布时间】:2012-11-30 17:14:19
【问题描述】:
我正在学习Jason Hickey's Introduction to Objective Caml。只是有一个关于表达的问题。
所以它说:
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
好的。我对上述说法不太了解。
首先
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.
这是什么意思?所以identifier 是the value of expression1,但只在正文中expression2?是不是说identifier只对expression2有效,但有expression1的值?那么定义identifier 是否有意义,因为它只存在于expression2 中?
第二
我们来看例子:
let x = 1 in
let y = 2 in
x + y;;
所以我看不出这个let 声明的意义。 x = 1 是肯定的,给let y=2 in x+y;; 的正文有什么意义?
第三
让 z = 让 x = 1 在 让 y = 2 在 x + y;;
那我该如何理清这句话的逻辑呢?
如果采用这种定义形式:let identifier = expression1 in expression2
上面let 语句中的expression1 是什么?是let x = 1吗?
谁能以Java 的方式告诉我nesting let 的逻辑?还是更容易理解的方式?
【问题讨论】:
-
A
C语句序列x = 1; y = 2; return x + y;被翻译成 MLlet x = 1 in y = 2 in x + y。请记住,ML 中没有语句,一切都是表达式。 -
逻辑很简单——把
let x = y in z想象成(fun x -> z) y的语法糖。 -
lets 只是在正文范围内定义值的方法。
标签: functional-programming ocaml