0
(define (make-checking beg-bal)
(let* ((balance beg-bal)
(tlist '()))
(define (writer s x)
(display s)
(display x)
(newline))
(define (deposit f)
(set! balance (+ balance f))
(set! tlist (append tlist (list f))))
(define (withdraw f)
(cond ((> funds balance)
"Insufficient Funds")
(else
(set! balance (- balance f))
(set! tlist f))))
(define (write-check f)
(cond ((< balance f) "Insufficient Funds")
((<= f balance)
(set! balance (- balance f))
(set! tlist (append tlist (list (* -1 f)))))
(else (display "Error") 'done)))
(define (print-statement)
(let ((t tlist) (z 0))
(display (string-append "Beginning Balance: " (number->string beg-bal)))
(newline)
(cond ((null? t) 'done)
((< (car t) 0) (string-append "Transaction: Check Amount: " (number->string (car t))))
((> (car t) 0) (string-append "Transaction: Check Amount: " (number->string (car t))))
(else print-statement))
(display (string-append "Balance: " (number->string balance)))
(newline)))
(define (current-balance)
balance)
(lambda (method)
(cond ((eq? method 'beg-bal) beg-bal)
((eq? method 'deposit) deposit)
((eq? method 'withdraw) withdraw)
((eq? method 'write-check) write-check)
((eq? method 'print-statement) print-statement)
((eq? method 'balance) current-balance)
(else 'undefined-operation)))))
"Tests"
(define checking (make-checking 100))
((checking 'write-check) 10)
((checking 'write-check) 10)
((checking 'deposit) 100)
((checking 'write-check) 10)
((checking 'print-statement))
((checking 'balance))
有人能告訴我爲什麼當我運行print-statement
函數輸出不是。帶字的輸出應該是字符串,並且歸功於display
函數,底部的170
是整個函數返回的結果。定義一個方案功能檢查
> beginning balance: 100
> transaction: check amount: -10
> transaction: check amount: -10
> transaction: deposit amount: 100
> transaction: check amount: -10
> balance: 170
> 170
哇這個作品非常感謝你的幫助! – dscarf