OCaml 中的 let 绑定和引用有什么区别? [英] What is the difference between let-bindings and references in OCaml?

查看:30
本文介绍了OCaml 中的 let 绑定和引用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OCaml 教程说引用是真正的变量",您可以在整个程序中对其进行分配和更改.让绑定不用于相同的目的.在这个 link 中,它说当一个名称由 let 绑定设置时,你不能"...重新分配它以指向不同的小部件."我知道引用实际上存储在内存中,而 let-bindings 不是,但我不明白他们对分配的看法.

The OCaml tutorials say that references are "real variables" which you can assign to and change throughout a program. Let-bindings do not serve the same purpose. In this link it says that when a name is set by a let-binding you cannot "...reassign it to point to a different widget." I understand that the references are actually stored in memory and let-bindings are not but i don't understand what they are saying about the assignment.

在 Ocaml 交互式会话中玩弄,似乎 let-bindings 和引用做同样的事情,但在语法上有一些不同.如果我使用 let-binding 将变量名称设置为某个整数值,该名称将返回该值,直到我取消它或将名称重置为不同的整数,let-binding 将允许.对于 ints、floats 和 strings 都是如此,但还没有尝试过其他类型.我错过了什么?

Playing around in an Ocaml interactive session, it seems like let-bindings and references do the same thing with some differences in the syntax. If I set a variable name to some integer value using a let-binding that name will return that value until I disestablish it or reset the name to a different integer, which the let-binding will allow. This is true for ints, floats and strings but have not tried other types. What am i missing?

# let let_var = 2;;
val let_var : int = 2
# let_var;;
- : int = 2
# let let_var = 3;;
val let_var : int = 3
# let_var;;
- : int = 3

推荐答案

看待事物的最佳方式是 let 绑定是一种为事物赋予永久名称的方式.由于 OCaml 的范围规则允许您重用名称,因此可以为内部上下文中的其他内容赋予相同的名称.但是名字在某种意义上仍然存在,并且仍然与其永久价值相关联.

The best way to look at things is that a let binding is a way of giving a permanent name to something. Since OCaml's scoping rules allow you to re-use names, it's possible to give the same name to something else in an inner context. But the first name is still there in some sense, and is still associated with its permanent value.

这是一个可能有助于说明这一点的会议:

Here's a session that might help illustrate this:

$ ocaml
        OCaml version 4.02.1

# let v = 12;;
val v : int = 12
# let g x = v + x;;
val g : int -> int = <fun>
# g 10;;
- : int = 22
# let v = 200;;
val v : int = 200
# g 10;;
- : int = 22
#

在这个会话中有两个不同具有相同名称v的let绑定.函数 g 使用第一个.如果您创建一个名为 v 的新绑定,这不会改变 g 的任何内容.

In this session there are two different let bindings with the same name v. The function g uses the first one. If you create a new binding named v, this doesn't change anything about g.

另一方面,引用是一个值.它根本不是一个名字,它是一种可变的值;即,它拥有另一个可以更改的值.以下会话可能有助于说明这一点:

On the other hand, a reference is a value. It's not a name at all, it's a kind of value that is mutable; i.e., it holds another value that can be changed. The following session might help illustrate this:

# let myref = ref 12;;
val myref : int ref = {contents = 12}
# let h x = !myref + x;;
val h : int -> int = <fun>
# h 10;;
- : int = 22
# myref := 200;;
- : unit = ()
# h 10;;
- : int = 210

这里的 myref 是一个 let 绑定变量,引用(永久)到一个引用值.最初引用保持值 12.但该值可以更改.函数 h 使用存储在引用中的任何当前值.但是请注意(再次) myref 没有改变.它仍然(永久)与同一个引用相关联.

Here myref is a let-bound variable referring (permanently) to a value that is a reference. Initially the reference holds the value 12. But the value can be changed. The function h uses whatever is the current value stored in the reference. But note (again) that myref hasn't changed. It still is (permanently) associated with the same reference.

这篇关于OCaml 中的 let 绑定和引用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆