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
+9 -4
View File
@@ -46,10 +46,15 @@
(define (rec clauses) (define (rec clauses)
(if (= nil clauses) (if (= nil clauses)
nil nil
(list 'if (first (first clauses)) (if (= (first (first clauses)) 'else)
(pair 'prog (rest (first clauses))) (prog
(rec (rest clauses))))) (if (not (= () (rest clauses)))
(rec 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 clauses))
(define (nil? x) (define (nil? x)
"Checks if the argument is nil." "Checks if the argument is nil."
+7 -7
View File
@@ -1,6 +1,6 @@
(define (abs x) (define (abs x)
(cond ((< x 0) (- x)) (cond ((< x 0) (- x))
(t x))) (else x)))
(assert (= (abs 1) 1)) (assert (= (abs 1) 1))
(assert (= (abs (- 2)) 2)) (assert (= (abs (- 2)) 2))
@@ -151,7 +151,7 @@
(define (A m n) (define (A m n)
(cond ((= m 0) (+ n 1)) (cond ((= m 0) (+ n 1))
((= n 0) (A (- m 1) 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 0 0) 1))
(assert (= (A 1 2) 4)) (assert (= (A 1 2) 4))
@@ -165,7 +165,7 @@
(define (fib n) (define (fib n)
(cond ((= n 0) 0) (cond ((= n 0) 0)
((= n 1) 1) ((= n 1) 1)
(t (+ (fib (- n 1)) (fib (- n 2)))))) (else (+ (fib (- n 1)) (fib (- n 2))))))
(define (fib2 n) (define (fib2 n)
(fib-iter 1 0 n)) (fib-iter 1 0 n))
@@ -195,7 +195,7 @@
;; (define (cc amount kinds-of-coins) ;; (define (cc amount kinds-of-coins)
;; (cond ((= amount 0) 1) ;; (cond ((= amount 0) 1)
;; ((or (< amount 0) (= kinds-of-coins 0)) 0) ;; ((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))))) ;; (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins)))))
;; (define (first-denomination kinds-of-coins) ;; (define (first-denomination kinds-of-coins)
@@ -232,7 +232,7 @@
(cond ((= n 0) 1) (cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2)))) ((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 1 2) 1))
(assert (= (expt 2 2) 4)) (assert (= (expt 2 2) 4))
@@ -269,7 +269,7 @@
(define (find-divisor n test-divisor) (define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n) (cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor) ((divides? test-divisor n) test-divisor)
(t (find-divisor n (+ test-divisor 1))))) (else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b) (define (divides? a b)
(= (% b a) 0)) (= (% b a) 0))
@@ -333,7 +333,7 @@
(let ((test-value (f midpoint))) (let ((test-value (f midpoint)))
(cond ((positive? test-value) (search f neg-point midpoint)) (cond ((positive? test-value) (search f neg-point midpoint))
((negative? test-value) (search f midpoint pos-point)) ((negative? test-value) (search f midpoint pos-point))
(t midpoint)))))) (else midpoint))))))
(define (close-enough? x y) (< (abs (- x y)) 0.001)) (define (close-enough? x y) (< (abs (- x y)) 0.001))