-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
76 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
(define (A x y) | ||
(cond ((= y 0) 0) | ||
((= x 0) (* 2 y)) | ||
((= y 1) 2) | ||
(else (A (- x 1) | ||
(A x (- y 1)))))) | ||
|
||
(define (pow base exponent) | ||
(cond ((= base 0) 0) | ||
((= exponent 0) 1) | ||
(else (* base (pow base (- exponent 1)))))) | ||
|
||
|
||
(pow 2 10) | ||
|
||
;Value: 1024 | ||
|
||
(pow 2 16) | ||
|
||
;Value: 65536 | ||
|
||
(A 1 10) | ||
|
||
;Value: 1024 | ||
|
||
(A 2 4) | ||
|
||
;Value: 65536 | ||
|
||
(A 3 3) | ||
|
||
;Value: 65536 | ||
|
||
(define (f n) (A 0 n)) | ||
|
||
; 2*n | ||
|
||
(define (g n) (A 1 n)) | ||
|
||
; 2^n | ||
|
||
(define (h n) (A 2 n)) | ||
|
||
; 2^2^2^2...^2, with (- n 1) 2's in total |
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,11 +1,18 @@ | ||
;; Exercise 1.3 | ||
|
||
(define (sum-of-squares x y) (+ (square x) (square y))) | ||
(define (sum-of-squares x y) | ||
(+ (square x) (square y))) | ||
|
||
(define (sum-squares-of-two-largest a b c) | ||
(cond ((and (> a c) (> b c)) (sum-of-squares a b)) | ||
((and (> a b) (> c b)) (sum-of-squares a c)) | ||
(else (sum-of-squares b c)))) | ||
|
||
(= (sum-squares-of-two-largest 3 4 5) (sum-of-squares 4 5)) | ||
(= (sum-squares-of-two-largest 5 4 3) (sum-of-squares 4 5)) | ||
(= (sum-squares-of-two-largest 3 5 4) (sum-of-squares 4 5)) | ||
(= (sum-squares-of-two-largest 3 4 5) | ||
(sum-of-squares 4 5)) | ||
|
||
(= (sum-squares-of-two-largest 5 4 3) | ||
(sum-of-squares 4 5)) | ||
|
||
(= (sum-squares-of-two-largest 3 5 4) | ||
(sum-of-squares 4 5)) |
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(define (inc a) (+ a 1)) | ||
(define (dec a) (- a 1)) | ||
|
||
;; The below procedure generates a recursive process because the process cannot | ||
;; be summarized at every step by a set of state variable values -- there is | ||
;; additional "hidden" information maintained by the interpreter that indicates | ||
;; "where the process is" in negotiating a chain of deferred operations. | ||
|
||
(define (plus-recur a b) | ||
(if (= a 0) | ||
b | ||
(inc (plus-recur (dec a) b)))) | ||
|
||
;; The below procedure generates an iterative process because the state at any | ||
;; stage in the evolution of the process is captured completely by two state | ||
;; variables, a and b. | ||
|
||
(define (plus-iter a b) | ||
(if (= a 0) | ||
b | ||
(plus-iter (dec a) (inc b)))) |