定义,让和设置之间的区别! [英] Difference between define, let and set!

查看:43
本文介绍了定义,让和设置之间的区别!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是一个相当基本的问题:我正在关注 SICP 视频,我对 definelet 之间的区别有点困惑设置!.

Ok, this is a fairly basic question: I am following the SICP videos, and I am a bit confused about the differences between define, let and set!.

1) 根据 Sussman 在视频中的说法,define 只允许为变量附加一次值(REPL 中除外),特别是不允许在行中定义两个.然而 Guile 很高兴地运行了这段代码

1) According to Sussman in the video, define is allowed to attach a value to avariable only once (except when in the REPL), in particular two defines in line are not allowed. Yet Guile happily runs this code

(define a 1)
(define a 2)
(write a)

并按预期输出 2.事情有点复杂,因为如果我尝试这样做(在上述定义之后)

and outputs 2, as expected. Things are a little bit more complicated because if I try to do this ( after the above definitions)

(define a (1+ a))

我收到一个错误,同时

(set! a (1+ a))

是允许的.我仍然不认为这是 set!define 之间的唯一区别:我缺少什么?

is allowed. Still I don't think that this the only difference between set! and define: what is that I am missing?

2) definelet 之间的区别让我更加困惑.我知道理论上 let 用于绑定局部范围内的变量.不过,在我看来,这与 define 的作用相同,例如我可以替换

2) The difference between define and let puzzles me even more. I know in theory let is used to bind variables in local scope. Still, it seems to me that this works the same with define, for instance I can replace

(define (f x)
    (let ((a 1))
        (+ a x)))

(define (g x)
    (define a 1)
    (+ a x))

fg 工作相同:特别是变量 ag 之外也是未绑定的.

and f and g work the same: in particular the variable a is unbound outside g as well.

我认为这很有用的唯一方法是 let 可能具有比整个函数定义更短的范围.在我看来,人们总是可以添加一个匿名函数来创建必要的范围,并立即调用它,就像在 javascript 中所做的那样.那么,let 的真正优势是什么?

The only way I can see this useful is that let may have a shorter scope that the whole function definition. Still it seems to me that one can always add an anonymous function to create the necessary scope, and invoke it right away, much like one does in javascript. So, what is the real advantage of let?

推荐答案

你的意思是 (+ 1 a) 而不是 (1+ a) ?后者在语法上无效.

Do you mean (+ 1 a) instead of (1+ a) ? The latter is not syntactically valid.

let 定义的变量范围绑定到后者,因此

Scope of variables defined by let are bound to the latter, thus

(define (f x)
  (let ((a 1))
    (+ a x)))

在语法上是可能的,而

(define (f x)
  (let ((a 1)))
  (+ a x))

不是.

所有变量都必须在函数的开头defined,因此可以使用以下代码:

All variables have to be defined in the beginning of the function, thus the following code is possible:

(define (g x)
  (define a 1)
  (+ a x))

虽然这段代码会产生一个错误:

while this code will generate an error:

(define (g x)
  (define a 1)
  (display (+ a x))
  (define b 2)
  (+ a x))

因为定义后的第一个表达式暗示没有其他定义.

because the first expression after the definition implies that there are no other definitions.

set! 不定义变量,而是用于为变量分配一个新值.因此,这些定义毫无意义:

set! doesn't define the variable, rather it is used to assign the variable a new value. Therefore these definitions are meaningless:

(define (f x)
  (set! ((a 1))
    (+ a x)))

(define (g x)
  (set! a 1)
  (+ a x))

set! 的有效用法如下:

(define x 12)
> (set! x (add1 x))
> x
13

尽管不鼓励这样做,因为 Scheme 是一种函数式语言.

Though it's discouraged, as Scheme is a functional language.

这篇关于定义,让和设置之间的区别!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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