exercise 1.1
10 ;10
(+ 5 3 4) ;12
(- 9 1) ;8
(/ 6 2) ;3
(+ ( 2 4) (- 4 6)) ;6
(define a 3)
(define b (+ a 1))
(+ a b ( a b)) ;7+12=19
(= a b) ;#f
(if (and (> b a) ( b a) b a)) ;6
( (cond ((> a b) a) ;44=16
(( x y) (if (> x z) x (if (> y z) y z))
(if (> y z) y z)))
(define (middle x y z)
(- (+ x y z) (+ (largest x y z) (- 0 (largest (- 0 x) (- 0 y) (- 0 z))))))
(define (ex1p3 x y z) (+ (square (largest x y z)) (square (middle x y z))))
exercise 1.4
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
;the (if (> b 0) + -) produces a function either + or - so that the correct operation will be done to combine a and b. If b is greater than 0, then a and b are added, otherwise b is subtracted from a which is equivalent to adding the abs of b.
Exercise 1.5.
What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)
Because evaluation of p is an infinite loop, under applicative order evaluation,
this will not terminate, because evaluation of test implies evaluating each argument. Under normal order evaluation p will never be evaluated on (test 0 (p)). Under normal order evaluation, evaluation occurs inside the function definition on each occurrence of the argument. Because of the assumption about if, the else clause is not reached.
Exercise 1.6.
What happens when Alyssa attempts to use this to compute square roots? Explain.
Both the then clause and if clause get evaluated, so the iteration will actually recurse indefinitely. (infinite loop)
Exercise 1.7.
The problem with good-enough is that the tolerance is fixed at 0.001. So we can't get an answer to accuracy greater than 0.001 so if x is small, then there is a large relative tolerance for error on the answer. At very large numbers there will be rounding.
so (nsqrt 0.0000001) is 0.03125 and (nsqrt 0.0001) is 0.03231
(I said nsqrt because scheme didn't seem to let me redefine sqrt.)
(nsqrt (expt 10.0 50)) gets into an infinite loop because when you get to
a guess of 10^25 and the numerical squaring doesn't give you exactly 10^50,
you don't have more significant figures to add, and there isn't enough
precision to meet the stopping tolerance.
Following the directions to track the changes in the guess, here is an
improved version of sqrt-iter that does solve both problems, (at least
the examples I mentioned earlier):
(This implementation does have a small performance penalty, in that the
guess is computed twice. My excuse is that we haven't covered let yet, and
the corresponding lambda implementation of let would make this pretty
difficult to read at this point.)
(define (sqrt-iter guess x)
(if (<= (abs (- guess (improve guess x))) (* 0.0001 guess))
guess
(sqrt-iter (improve guess x)
x)))
Exercise 1.8.
since square root used:
(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 (nsqrt x)
(sqrt-iter 1.0 x))
I now define for cube root:
(define (cube-good-enough? guess x)
(< (abs (- (* guess (square guess)) x)) 0.001))
(define (cube-improve guess x)
(/ (+ (* 2 guess) (/ x (square guess))) 3))
(define (cube-iter guess x)
(if (cube-good-enough? guess x)
guess
(cube-iter (cube-improve guess x)
x)))
(define (ncubert x)
(cube-iter 1.0 x))