cond has an else clause now

This commit is contained in:
Felix Brendel
2019-05-03 19:36:32 +02:00
parent a1fccd29de
commit 311af423df
2 changed files with 16 additions and 11 deletions
+6 -1
View File
@@ -46,9 +46,14 @@
(define (rec clauses)
(if (= nil clauses)
nil
(if (= (first (first clauses)) 'else)
(prog
(if (not (= () (rest clauses)))
(error "There are additional clauses after the else clause!")
(pair 'prog (rest (first clauses)))))
(list 'if (first (first clauses))
(pair 'prog (rest (first clauses)))
(rec (rest clauses)))))
(rec (rest clauses))))))
(rec clauses))
(define (nil? x)
+7 -7
View File
@@ -1,6 +1,6 @@
(define (abs x)
(cond ((< x 0) (- x))
(t x)))
(else x)))
(assert (= (abs 1) 1))
(assert (= (abs (- 2)) 2))
@@ -151,7 +151,7 @@
(define (A m n)
(cond ((= m 0) (+ n 1))
((= n 0) (A (- m 1) 1))
(t (A (- m 1) (A m (- n 1))))))
(else (A (- m 1) (A m (- n 1))))))
(assert (= (A 0 0) 1))
(assert (= (A 1 2) 4))
@@ -165,7 +165,7 @@
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(t (+ (fib (- n 1)) (fib (- n 2))))))
(else (+ (fib (- n 1)) (fib (- n 2))))))
(define (fib2 n)
(fib-iter 1 0 n))
@@ -195,7 +195,7 @@
;; (define (cc amount kinds-of-coins)
;; (cond ((= amount 0) 1)
;; ((or (< amount 0) (= kinds-of-coins 0)) 0)
;; (t (+ (cc amount (- kinds-of-coins 1))
;; (else (+ (cc amount (- kinds-of-coins 1))
;; (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins)))))
;; (define (first-denomination kinds-of-coins)
@@ -232,7 +232,7 @@
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(t (* b (fast-expt b (- n 1))))))
(else (* b (fast-expt b (- n 1))))))
(assert (= (expt 1 2) 1))
(assert (= (expt 2 2) 4))
@@ -269,7 +269,7 @@
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(t (find-divisor n (+ test-divisor 1)))))
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
(= (% b a) 0))
@@ -333,7 +333,7 @@
(let ((test-value (f midpoint)))
(cond ((positive? test-value) (search f neg-point midpoint))
((negative? test-value) (search f midpoint pos-point))
(t midpoint))))))
(else midpoint))))))
(define (close-enough? x y) (< (abs (- x y)) 0.001))