【问题标题】:How to understand the "body of the let"?如何理解“让的主体”?
【发布时间】: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.

这是什么意思?所以identifierthe 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; 被翻译成 ML let x = 1 in y = 2 in x + y。请记住,ML 中没有语句,一切都是表达式。
  • 逻辑很简单——把let x = y in z想象成(fun x -> z) y的语法糖。
  • lets 只是在正文范围内定义值的方法。

标签: functional-programming ocaml


【解决方案1】:

这是什么意思?那么identifier就是expression1的值,但只在body expression2中?是不是说identifier只在表达式2有效,却有表达式1的值?

是的。当我执行let x = 42 in x+x 时,x 在表达式x+x 中具有值42,但xlet 表达式之外没有任何值。因此,如果您在解释器中输入这样的内容:

let x = 42 in x+x;;
x*2;;

x+x 的结果将是 84,但 x*2 的结果将是一个错误,因为 x 未在该范围内定义。

那么定义标识符是否有意义,因为它只在表达式2中?

当然,为什么不呢?我的意思是在这个特定示例中,x 被定义为单个数字,它可能没有那么大的意义,但在现实世界的场景中,它可能是一个更大的表达式,不必重复多次变为一个很大的优势。

另外,expression1 可能具有您只想执行一次的副作用(例如,如果您想将用户输入分配给变量)。

所以我看不出这个 let 声明的意义。 x = 1 是肯定的,在 x+y;; 中给出 let y=2 的主体有什么意义?

你的意思是:为什么不直接写1+2?同样,在现实世界的场景中,您将拥有更复杂的表达式,并且您不想将它们全部放在一行上。还给值命名通常会增加可读性(尽管在本示例中并非如此)。

let z = let x = 1 in let y = 2 in x + y;;

上面 let 语句中的表达式 1 是什么?是让 x = 1 吗?

这里的let z = ... 是一个let 语句,即它在全局范围内定义了名称z,并且没有正文。所以第一个=右边的整个表达式就是z的值。对于let x = ... in ... 位,1expression1let y = 2 in x+yexpression2。对于y2expression1x+yexpression2

【讨论】:

  • 所以如果我说let x = 12;;,那么x 就被定义了,我以后可以在任何地方使用它。如果我说let x = 12 in x+x', then x` 被定义并且只能在x+x 中使用,我说的对吗?
  • 谢谢。好吧,关于示例的z 部分,真正重要的部分是the later x+y。对于let z,没有body of let,整个let x=1 in let y =2 in x+y 是一个表达式。我说的对吗?
  • 感谢 sepp2k。那么我该如何告诉a let is a expressiona let is a binding (by binding, I mean defining an identifier)
  • @JacksonTale let 表达式始终具有 in,而全局 let 绑定则没有。全局 let 绑定也只允许在顶层,而不是在表达式内。所以如果lets 比ins 多,那么第一个let 必须是全局绑定。
  • Also global let bindings are only allowed at the top-leveltop-level 这里是什么意思?是不是OCaml的命令行交互的东西?
【解决方案2】:

这是什么意思?所以 identifier 是 expression1 的值,但是 仅在正文表达式2中?这是否意味着标识符是 仅在表达式 2 中有效但具有表达式 1 的值?然后 定义标识符有意义吗,因为它只在表达式2中?

这就是它的意思,是的,但更简单地说,它意味着您不能“以其自身的价值”使用标识符。将表达式 1 的值绑定到标识符后,您可以在表达式 2 中使用标识符而不是整个表达式 1。

一般来说,使用绑定有两个原因; 1 是为了防止对给定表达式进行多次评估,而 2 是为了清楚起见:为相对复杂的表达式的值赋予人类可读的名称通常很有用。

在 Java 中,一个 let 绑定大致对应一个局部变量。

【讨论】:

    猜你喜欢
    • 2012-10-29
    • 2019-09-03
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多