-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
2,108 additions
and
1,985 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))))) |
Oops, something went wrong.