-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.scm
28 lines (24 loc) · 1017 Bytes
/
calculator.scm
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
(define (calc)
(display "calc: ")
(print (calc-eval (read)))
(newline)
(calc))
(define (accumulate op initial items)
(if (null? items)
initial
(op (car items)
(accumulate op initial (cdr items)))))
(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))))
(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))))