Skip to content

Commit

Permalink
refactor chapter 2
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8b committed Jul 18, 2020
1 parent 25735f3 commit da23073
Show file tree
Hide file tree
Showing 89 changed files with 2,108 additions and 1,985 deletions.
62 changes: 31 additions & 31 deletions 1.01.scm
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
10 ; 10

(+ 5 3 4) ; 12

(- 9 1) ; 8

(/ 6 2) ; 3

(+ (* 2 4) (- 4 6)) ; 6

(define a 3) ; a

(define b (+ a 1)) ; b

(+ a b (* a b)) ; 19

(= a b) ; #f

(if (and (> b a) (< b (* a b)))
b
a) ; 4

(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25)) ; 16

(+ 2 (if (> b a) b a)) ; 6

(* (cond ((> a b) a)
((< a b) b)
(else -1))
10 ; 10

(+ 5 3 4) ; 12

(- 9 1) ; 8

(/ 6 2) ; 3

(+ (* 2 4) (- 4 6)) ; 6

(define a 3) ; a

(define b (+ a 1)) ; b

(+ a b (* a b)) ; 19

(= a b) ; #f

(if (and (> b a) (< b (* a b)))
b
a) ; 4

(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25)) ; 16

(+ 2 (if (> b a) b a)) ; 6

(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1)) ; 16
14 changes: 7 additions & 7 deletions 1.03.scm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(define (>= a b)
(or (> a b) (= a b)))

(define (sum-larger-numbers a b c)
(cond
((and (>= a c) (>= b c)) (+ a b))
((and (>= b a) (>= c a)) (+ b c))
(define (>= a b)
(or (> a b) (= a b)))

(define (sum-larger-numbers a b c)
(cond
((and (>= a c) (>= b c)) (+ a b))
((and (>= b a) (>= c a)) (+ b c))
((and (>= a b) (>= c b)) (+ a c))))
2 changes: 1 addition & 1 deletion 1.04.scm
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(define (a-plus-abs-b a b)
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
14 changes: 7 additions & 7 deletions 1.05.scm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(define (p) (p))

(define (test x y)
(if (= x 0)
0
y))

(define (p) (p))

(define (test x y)
(if (= x 0)
0
y))

(test 0 (p))
4 changes: 2 additions & 2 deletions 1.06.scm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
52 changes: 26 additions & 26 deletions 1.07.scm
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))

(define (improve guess x)
(average guess (/ x guess)))

(define (average x y)
(/ (+ x y) 2))

(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))

(define (square x)
(* x x))

(define (sqrt x)
(sqrt-iter 1.0 x))

(sqrt 2) ; 1.4142156862745097

(define (good-enough-alternative? guess x)
(< (abs (/ (- (improve guess x) guess)
guess))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))

(define (improve guess x)
(average guess (/ x guess)))

(define (average x y)
(/ (+ x y) 2))

(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))

(define (square x)
(* x x))

(define (sqrt x)
(sqrt-iter 1.0 x))

(sqrt 2) ; 1.4142156862745097

(define (good-enough-alternative? guess x)
(< (abs (/ (- (improve guess x) guess)
guess))
0.001))
38 changes: 19 additions & 19 deletions 1.08.scm
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
(define (cbrt-iter guess x)
(if (good-enough? guess x)
guess
(cbrt-iter (improve guess x)
x)))

(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.001))

(define (cube x)
(* x x x))

(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))

(define (square x)
(* x x))

(define (cbrt x)
(define (cbrt-iter guess x)
(if (good-enough? guess x)
guess
(cbrt-iter (improve guess x)
x)))

(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.001))

(define (cube x)
(* x x x))

(define (improve guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))

(define (square x)
(* x x))

(define (cbrt x)
(cbrt-iter 1.0 x))
52 changes: 26 additions & 26 deletions 1.09.scm
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))

; (+ 4 5)
; (inc (+ 3 5))
; (inc (inc (+ 2 5)))
; (inc (inc (inc (+ 1 5))))
; (inc (inc (inc (inc (+ 0 5)))))
; (inc (inc (inc (inc 5))))
; (inc (inc (inc 6)))
; (inc (inc 7))
; (inc 8)
; 9

; (define (+ a b)
; (if (= a 0)
; b
; (+ (dec a) (inc b))))

; (+ 4 5)
; (+ 3 6)
; (+ 2 7)
; (+ 1 8)
; (+ 0 9)
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))

; (+ 4 5)
; (inc (+ 3 5))
; (inc (inc (+ 2 5)))
; (inc (inc (inc (+ 1 5))))
; (inc (inc (inc (inc (+ 0 5)))))
; (inc (inc (inc (inc 5))))
; (inc (inc (inc 6)))
; (inc (inc 7))
; (inc 8)
; 9

; (define (+ a b)
; (if (= a 0)
; b
; (+ (dec a) (inc b))))

; (+ 4 5)
; (+ 3 6)
; (+ 2 7)
; (+ 1 8)
; (+ 0 9)
; 9
24 changes: 12 additions & 12 deletions 1.10.scm
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1) (A x (- y 1))))))

(A 1 10) ; 1024
(A 2 2) ; 4
(A 3 3) ; 65536

(define (f n) (A 0 n)) ; 2n
(define (g n) (A 1 n)) ; n=0: 0, n>0: 2^n
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1) (A x (- y 1))))))

(A 1 10) ; 1024
(A 2 2) ; 4
(A 3 3) ; 65536

(define (f n) (A 0 n)) ; 2n
(define (g n) (A 1 n)) ; n=0: 0, n>0: 2^n
(define (h n) (A 2 n)) ; n=0: 0; n=1: 2; n>1: 2^2^...(n-1) times
28 changes: 14 additions & 14 deletions 1.11.scm
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))))

(define (f.iter n)
(define (f-iter a b c count)
(if (= n count)
c
(f-iter b c (+ c (* 2 b) (* 3 a)) (+ count 1))))
(if (< n 3)
n
(define (f n)
(if (< n 3)
n
(+ (f (- n 1))
(* 2 (f (- n 2)))
(* 3 (f (- n 3))))))

(define (f.iter n)
(define (f-iter a b c count)
(if (= n count)
c
(f-iter b c (+ c (* 2 b) (* 3 a)) (+ count 1))))
(if (< n 3)
n
(f-iter 0 1 2 2)))
11 changes: 5 additions & 6 deletions 1.12.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(define (pascal c r)
(if (or (= r c) (= 0 c))
1
(+ (pascal c (- r 1))
(pascal (- c 1) (- r 1)))))

(define (pascal c r)
(if (or (= r c) (= 0 c))
1
(+ (pascal c (- r 1))
(pascal (- c 1) (- r 1)))))
29 changes: 14 additions & 15 deletions 1.16.scm
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
(define (square a)
(* a a))

(define (halve a)
(/ a 2))

(define (fast-expt b n)
(define (fast-expt-iter b n product)
(if (= n 0)
product
(if (even? n)
(fast-expt-iter (square b) (halve n) product)
(fast-expt-iter b (dec n) (* b product)))))

(fast-expt-iter b n 1))
(define (square x)
(* x x))

(define (halve x)
(/ x 2))

(define (fast-expt b n)
(define (fast-expt-iter b n a)
(cond
((= n 0) a)
((even? n) (fast-expt-iter (square b) (halve n) a))
(else (fast-expt-iter b (- n 1) (* b a)))))

(fast-expt-iter b n 1))
23 changes: 11 additions & 12 deletions 1.17.scm
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
(define (double x)
(+ x x))

(define (halve x)
(/ x 2))

(define (multiply a b)
(if (= b 0)
0
(if (even? b)
(multiply (double a) (halve b))
(+ a (multiply a (- b 1))))))
(define (double x)
(+ x x))

(define (halve x)
(/ x 2))

(define (* a b)
(cond
((= b 0) a)
((even? b) (* (double a) (halve b)))
(else (+ a (* a (- b 1))))))
Loading

0 comments on commit da23073

Please sign in to comment.