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"(please use lisp)
时间: 2024-02-07 14:03:21 浏览: 83
南昌大学《数据库管理系统》期末试卷2009-2010-1(A)
下面是使用 Lisp 语言编写的修改后的 `make-account` 过程:
```lisp
(define (make-account balance password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch pw m)
(if (eq? pw password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
"Incorrect password"))
dispatch)
```
这个过程接受两个参数:账户余额和密码。返回一个带有两个参数的过程,其中第一个参数是密码,第二个参数是操作类型('withdraw 或 'deposit)。如果密码正确,过程将执行相应的操作。如果密码错误,过程将返回“Incorrect password”字符串。
阅读全文