灯下 登录
计算机科学 / SICP / 3.1.1 Local State Variables

Exercise 3.3 · 习题

Exercise 3.3: Modify the make-account
procedure so that it creates password-protected accounts. That is,
make-account should take a symbol as an additional argument, as in

(define acc
(make-account 100 'secret-password))

The resulting account object should process a request only if it is accompanied
by the password with which the account was created, and should otherwise return
a complaint:

((acc 'secret-password 'withdraw) 40)
60

((acc 'some-other-password 'deposit) 50)

"Incorrect password"

练习 3.3:修改 make-account 过程,使其创建受密码保护的账户。也就是说,make-account 应接受一个符号作为额外参数,如:

(define acc
(make-account 100 'secret-password))

所得到的账户对象只有在请求附带账户创建时所用密码的情况下才予以处理,否则返回一条提示信息:

((acc 'secret-password 'withdraw) 40)
60

((acc 'some-other-password 'deposit) 50)
“Incorrect password”

Racket #lang sicp
(define acc
 (make-account 100 'secret-password))
Racket #lang sicp
((acc 'secret-password 'withdraw) 40)
60

((acc 'some-other-password 'deposit) 50)
"Incorrect password"