@@ -0,0 +1,2 @@ | |||||
*.swp | |||||
*~ |
@@ -0,0 +1,12 @@ | |||||
10 | |||||
12 | |||||
8 | |||||
3 | |||||
6 | |||||
19 | |||||
#f | |||||
4 | |||||
16 | |||||
6 | |||||
16 | |||||
@@ -0,0 +1,11 @@ | |||||
#lang sicp | |||||
(/ (+ 5 | |||||
4 | |||||
(- 2 | |||||
(- 3 | |||||
(+ 6 | |||||
(/ 4 5))))) | |||||
(* 3 | |||||
(- 6 2) | |||||
(- 2 7))) |
@@ -0,0 +1,23 @@ | |||||
#lang sicp | |||||
(define (square x) | |||||
(* x x)) | |||||
(define (sum-of-squares x y) | |||||
(+ (square x) (square y))) | |||||
(define (foo x y z) | |||||
(if (> x y) | |||||
(if (> y z) | |||||
(sum-of-squares x y) | |||||
(sum-of-squares x z)) | |||||
(if (> x z) | |||||
(sum-of-squares x y) | |||||
(sum-of-squares y z)))) | |||||
(foo 1 2 3) | |||||
(foo 1 3 2) | |||||
(foo 2 1 3) | |||||
(foo 2 3 1) | |||||
(foo 3 1 2) | |||||
(foo 3 2 1) |
@@ -0,0 +1 @@ | |||||
the application procedure will first evaluate the if statement, resulting in one of the symbols `+` or `-`. this will be the value of the first place within the outer set of brackets, which will determine which procedure is used during application of the rest of the procedure. |
@@ -0,0 +1,50 @@ | |||||
when evaluating the procedure: | |||||
``` | |||||
(define (p) (p)) | |||||
(define (test x y) | |||||
(if (= x 0) | |||||
0 | |||||
y)) | |||||
``` | |||||
by evaluating: | |||||
``` | |||||
(test 0 (p)) | |||||
``` | |||||
under both normal order and applicative order evaluation, you will see; | |||||
## applicative order | |||||
under applicative order evaluation, we would first fully evaluate any arguments to a procedure, then evaluate the procedure with those values. | |||||
to evaluate `(test 0 (p)` we would first need to evaluate `0` and `(p)` | |||||
`0` is a primitive value, and thus evaluates to the value `0`. | |||||
`(p)` is a procedure in the global environment, defined as `(p)`. we would recieve a value of `(p)`, which according to our evaluation strategy would need to be fully evaluated before we can continue evaluating `(test 0 (p))`. this would create an infinite loop as we would never reach a value of `(p)` that doesnt require further evaluation, and our program will hang. | |||||
## normal order | |||||
under normal order, we do not evaluate arguments until they are needed. thus, to evaluate `(test 0 (p))`, the following steps are taken: | |||||
``` | |||||
(test 0 (p)) | |||||
(if (= 0 0) 0 (p)) | |||||
(if #t 0 (p)) | |||||
0 | |||||
``` | |||||
the first line is our initial procedure application | |||||
the second is our definition of `test`, with the substitutions `x` -> `0` and `y` -> `(p)`. | |||||
the special form `if` will only evaluate the third argument, if the first (the predicate) is false. | |||||
the third line is the aftermath of evaluating `(= 0 0)` in order to check the value of our predicate, which is `true`. | |||||
the fourth line is the result of the `if` procedure, returning `0`. | |||||
notably, as we are following normal order application, we never needed to evaluate `(p)` (thus avoiding hanging our program). however had the predicate to the `if` procedure returned `false`, we would then need to evaluate `(p)` to find a value for the wider `if` application, and enter a loop. |
@@ -0,0 +1,5 @@ | |||||
when attempting to compute square roots using the new `new-if` procedure (which acts as `if` however is not a special form), all three of the arguments to `new-if` must be fully evaluated before the `new-if` can return a value. this is in contrast to the special form `if` which only ever evaluates one of the second and third arguments, conditional on the first argument. | |||||
in this case, even if the current guess is good enough (indicating that the second argument of the `new-if` should be returned) the procedure is still required to evaluate the third argument, which contains a recursive call. | |||||
as such, using the non-special form `new-if` will never return a value, as it has no way of stopping evaluation once a good enough answer has been found. |
@@ -0,0 +1,35 @@ | |||||
(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 (square x) | |||||
(* x x)) | |||||
(define (good-enough? guess x) | |||||
(< (abs (- (square guess) | |||||
x)) | |||||
0.001)) | |||||
(define (my-good-enough? guess prev-guess) | |||||
(< (abs (- guess prev-guess)) | |||||
(/ guess 100000))) | |||||
(define (my-sqrt-iter guess prev-guess x) | |||||
(if (my-good-enough? guess prev-guess) | |||||
guess | |||||
(my-sqrt-iter (improve guess x) | |||||
guess | |||||
x))) | |||||
(define (my-sqrt x) | |||||
(my-sqrt-iter 1.0 (improve 1.0 x) x)) | |||||
(define (sqrt x) | |||||
(sqrt-iter 1.0 x)) |
@@ -0,0 +1,27 @@ | |||||
(define (improve guess x) | |||||
(/ (+ (/ x (square guess)) | |||||
(* 2 guess)) | |||||
3)) | |||||
(define (average x y) | |||||
(/ (+ x y) 2)) | |||||
(define (square x) | |||||
(* x x)) | |||||
(define (cube x) | |||||
(* x x x)) | |||||
(define (my-good-enough? guess prev-guess) | |||||
(< (abs (- guess prev-guess)) | |||||
(/ guess 100000))) | |||||
(define (my-cube-root-iter guess prev-guess x) | |||||
(if (my-good-enough? guess prev-guess) | |||||
guess | |||||
(my-cube-root-iter (improve guess x) | |||||
guess | |||||
x))) | |||||
(define (my-cube-root x) | |||||
(my-cube-root-iter 1.0 (improve 1.0 x) x)) |
@@ -0,0 +1,7 @@ | |||||
# SICP | |||||
answers to SICP exercises. | |||||
answered in `#lang sicp` for racket. | |||||
can be installed with `raco pkg install sicp` |