calc - ((eq? fn '-) (cond ((null? args) (error...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
;; Scheme calculator -- evaluate simple expressions ; The read-eval-print loop: (define (calc) (display "calc: ") (flush) (print (calc-eval (read))) (calc)) ; Evaluate an expression: (define (calc-eval exp) (cond ((number? exp) exp) ((list? exp) (calc-apply (car exp) (map calc-eval (cdr exp)))) (else (error "Calc: bad expression:" exp)))) ; Apply a function to arguments: (define (calc-apply fn args) (cond ((eq? fn '+) (accumulate + 0 args))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ((eq? fn '-) (cond ((null? args) (error "Calc: no args to -")) ((= (length args) 1) (- (car args))) (else (- (car args) (accumulate + 0 (cdr args)))))) ((eq? fn '*) (accumulate * 1 args)) ((eq? fn '/) (cond ((null? args) (error "Calc: no args to /")) ((= (length args) 1) (/ (car args))) (else (/ (car args) (accumulate * 1 (cdr args)))))) (else (error "Calc: bad operator:" fn))))...
View Full Document

Ask a homework question - tutors are online