然而,我写了一个比较简单的银行账户功能,当我尝试运行它,我得到一个类型错误,我不知道为什么? 它是直出SICP,这样的解决方案是现成的,我只是想知道为什么我的回答产生了错误。 有什么想法吗? 这里是我的代码:
(define (make-password-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 users-guess-at-password m)
(if (eq? users-guess-at-password password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown Request --make-account" m)))
"INVALID PASSWORD"))
dispatch)
下面是从解释器的相关解释器的输入和输出:
..loaded function...
>(define acc (make-password-account 100 'secret))
acc
>((acc 's 'withdraw) 2)
;Stack Trace:
0 TypeError: The object "Invalid Password" is not applicable.
1 ((acc (quote s) (quote withdraw) 2)