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
SICP 1.1 | |
10 | |
=> 10 | |
(+ 5 3 4) | |
=> 12 | |
(- 9 1) | |
=> 8 | |
(/ 6 2) | |
=> 3 | |
(+ (* 2 4) (- 4 6)) |
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
SICP 1.2 | |
(/ (+ 5 4 | |
(- 2 | |
(- 3 | |
(+ 6 | |
(/ 5 4) | |
) | |
) | |
) | |
) |
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
(define (square x) (* x x)) | |
(define (sum-of-squares x y) (+ (square x) (square y))) | |
(define (larger x y) (if (> x y) x y)) | |
(define (sum-squares-of-largest-two x y z) | |
(cond ((and (> x y) (> x z)) (sum-of-squares x (larger y z))) | |
((and (> y x) (> y z)) (sum-of-squares y (larger x z))) | |
(else (sum-of-squares z (larger x y))) |
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
(define (a-plus-abs-b a b) | |
((if (> b 0) + -) a b) | |
b is evaluated to be greater than zero or not | |
based on that evaluation the + or - operator is selected | |
and used to add a and b if b is positive or subtract b from a if b is negative |
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
(define (p) (p)) | |
(define (test x y) | |
(if (= x 0) | |
0 | |
y)) | |
applicative-order evaluation | |
evaluate arguments and then apply | |
normal-order evaluation |
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
;; new-if.... | |
With the new-if, things look good when the case is simple and it doesn't matter if the then-clause and else-clause get evaluated. Using the special form if, first the predicate will be evaluated and then the then-clause OR the else-clause. With the new-if procedure, all arguments will be evaluated first. This sqrt-iter will not progress to any result. Infinite loop. |
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
;; good-enough? | |
The good-enough? test used in computing square roots will not be very effective for | |
finding the square roots of very small numbers. Also, in real computers, arithmetic operations are | |
almost always performed with limited precision. This makes our test inadequate for very large | |
numbers. Explain these statements, with examples showing how the test fails for small and large | |
numbers. An alternative strategy for implementing good-enough? is to watch how guess changes | |
from one iteration to the next and to stop when the change is a very small fraction of the guess. Design | |
a square-root procedure that uses this kind of end test. Does this work better for small and large | |
numbers? |