定义方案函数make-checking

时间:2017-11-25 20:55:02

标签: scheme racket

(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 个答案:

答案 0 :(得分:0)

您的print-statement已关闭,但不会循环tlist。这是一种方法。请注意,我已将所有append替换为tlist cons es(这是一种很好的做法,而且速度更快),因此在print-statement我需要{{} 1}}列表:

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)))))