- 論壇徽章:
- 0
|
sicp 3.3 的答案,我 輸入錯(cuò)誤password ,程序出錯(cuò),后來看了win_hate 的答案
(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 pass m)
(cond ((not (eq? password pass)) (lambda (x) "Incorrect password"))
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unkonwn request -- MAKE-ACCOUNT" m))))
dispatch)
---------------------
上面是 win_hate 的答案,我 的答案也差不 多,就 是在 dispatch 的 錯(cuò)誤答案處理上直接(display "Incorrect password") , 沒 (lambda(x) "Incorrect password")
這樣的話 如果我輸入錯(cuò)誤的password , 我的 程序就會(huì)出錯(cuò),
比如 ((acc 'love 'withdraw) 400)
Incorrect pasword. . procedure application: expected procedure, given: #<void>; arguments were: 400
誰(shuí)解釋一下,為社么一定要加個(gè) lambda(x) 呢 。。 |
|