2017-11-25 111 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