Przeglądaj źródła

cond has an else clause now

master
Felix Brendel 7 lat temu
rodzic
commit
311af423df
2 zmienionych plików z 16 dodań i 11 usunięć
  1. +9
    -4
      bin/pre.slime
  2. +7
    -7
      bin/tests/sicp.slime

+ 9
- 4
bin/pre.slime Wyświetl plik

@@ -46,10 +46,15 @@
(define (rec clauses)
(if (= nil clauses)
nil
(list 'if (first (first clauses))
(pair 'prog (rest (first clauses)))
(rec (rest clauses)))))
(rec clauses))
(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 clauses))

(define (nil? x)
"Checks if the argument is nil."


+ 7
- 7
bin/tests/sicp.slime Wyświetl plik

@@ -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))



Ładowanie…
Anuluj
Zapisz