SICP第3.1.1节 - 程序中的本地状态似乎不一致 [英] SICP Section 3.1.1 - Local state in procedures seems inconsistent

查看:145
本文介绍了SICP第3.1.1节 - 程序中的本地状态似乎不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过SICP工作。我在第3.1.1节和查看在本地状态。我在GNU Guile v2.0.11中评估这些练习。

I am working my way through SICP. I am on Section 3.1.1 and looking at local state. I am evaluating these exercises in GNU Guile v2.0.11.

我发现了一个关于这一部分的类似问题,但似乎没有解决我所困扰的一点,或者我过于晦涩。

I did find a similar question about this section, but it seems not to address the point I am struggling with, or I am overly obtuse.

我在看的两个例子是:

(define new-withdraw
  (let ((balance 100))
    (lambda (amount)
      (if (>= balance amount)
          (begin (set! balance (- balance amount))
                 balance)
          "Insufficient funds"))))


(define (make-withdraw balance)
  (lambda (amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        "Insufficient funds")))

当我将第一个赋值给一个变量时:

When I assign the first to a variable with:

(define a new-withdraw)
(define b new-withdraw)

我得到两个指向同一个过程对象的指针,它们之间的状态是共享的:

I get two pointers to the same procedure object and the state is shared between them:

scheme@(guile-user)> a 
$1 = #<procedure 1165880 at /path/to/file (amount)>
scheme@(guile-user)> b
$2 = #<procedure 1165880 at /path/to/file (amount)>
scheme@(guile-user)> (a 50)
$3 = 50
scheme@(guile-user)> (b 10)
$4 = 40

当我实现第二个过程时,获取指向具有不同状态的两个不同过程对象的指针:

When I implement the second procedure, though, I get pointers to two distinct procedure objects with distinct state:

scheme@(guile-user)> (define c (make-withdraw 100))
scheme@(guile-user)> (define d (make-withdraw 100))
scheme@(guile-user)> c
$5 = #<procedure 14fdac0 at /path/to/file (amount)>
scheme@(guile-user)> d
$6 = #<procedure 1508360 at /path/to/file (amount)>
scheme@(guile-user)> (c 50)
$7 = 50
scheme@(guile-user)> (d 10)
$8 = 90

没有清楚的解释,我在寻找这个部分时遇到麻烦找到任何东西。我明白一般情况下发生的状况,但我不知道这些程序之间的区别是,允许​​一个单一的通用状态,另一个维持本地状态。

I've read through the section and there is no clear explanation of this, and I'm having trouble finding anything when I search for this section. I understand what is happening in terms of state in general, but I don't get what the difference between these procedures is that allows one to have a single universal state, and the other to maintain local state.

为什么第一个定义new-withdraw无法在多个任务中保持本地状态?看来,lambda应该捕获平衡的分配到100每次我们做不同的分配(define new-withdraw)。

Why is the first definition, 'new-withdraw', unable to maintain local state across multiple assignments? It seems that the lambda should be capturing the assignment of balance to 100 every time we make a different assignment (define new-withdraw).

推荐答案

原因是在第二个例子中,每次都返回一个新的闭包,而在第一个例子中,

The reason is that in the second example you return a fresh closure every time, whereas in the first example there is only one closure, so only one state.

如果您希望在第一种情况下与第二种情况下的行为相同,请更改为:

If you want the same behaviour in the first case as you have in the second case, change to:

(define (new-withdraw) ; this is a procedure now
  (let ((balance 100))
    (lambda (amount)
      (if (>= balance amount)
          (begin (set! balance (- balance amount))
                 balance)
          "Insufficient funds"))))

(define a (new-withdraw)) ; called as a procedure
(define b (new-withdraw))

>

then

> (a 50)
50
> (b 10)
90

这篇关于SICP第3.1.1节 - 程序中的本地状态似乎不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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