blob: 7d667dde963ef749fef48d52d725480777f8a2db (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
;; 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))
((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))))
|