必承其重 | 欲带皇冠 2011-03-23 13:37 采纳率: 0%
浏览 286
已采纳

差异之间的定义,让和设置!

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) 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)

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

(define a (1+ a))

I get an error, while

(set! a (1+ a))

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

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)))

with

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

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

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?

转载于:https://stackoverflow.com/questions/5406064/difference-between-define-let-and-set

  • 写回答

4条回答 默认 最新

  • 7*4 2011-03-23 14:03
    关注

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

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

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

    is syntactically possible, while

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

    is not.

    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! 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))
    

    Valid use for set! is as follows:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办