2017-11-25 109 views
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 

回答

1

print-statement是關閉的,它並沒有遍歷tlist。這是一種方法。請注意,我已經更換了你所有append s到tlistcons ES(這是一個很好的做法,和更快的),所以在print-statement我需要reverse名單:

(define (make-checking beg-bal) 
    (let ((balance beg-bal) (tlist '())) 
    (define (deposit f) 
     (set! balance (+ balance f)) 
     (set! tlist (cons f tlist))) 
    (define (withdraw f) 
     (cond ((> f balance) "Insufficient Funds") 
      (else 
      (set! balance (- balance f)) 
      (set! tlist (cons (- f) tlist))))) 
    (define (write-check f) 
     (cond ((< balance f) "Insufficient Funds") 
      ((<= f balance) 
      (set! balance (- balance f)) 
      (set! tlist (cons (- f) tlist))) 
      (else (display "Error") 'done))) 
    (define (print-statement) 
     (printf "Beginning Balance: ~a\n" beg-bal) 
     (for-each (lambda (f) 
        (printf "Transaction: ~a ~a\n" 
          (if (< f 0) "Check amount: " "Deposit amount: ") 
          f)) 
       (reverse tlist)) 
     (printf "Balance: ~a\n" balance)) 
    (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))))) 

輸出現在是:

Beginning Balance: 100 
Transaction: Check amount: -10 
Transaction: Check amount: -10 
Transaction: Deposit amount: 100 
Transaction: Check amount: -10 
Balance: 170 
170 
+0

哇這個作品非常感謝你的幫助! – dscarf