我写了一个相对简单的银行账户函数,但是当我尝试运行它时,我得到一个 TypeError,我不确定为什么?它直接来自 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)