OneCompiler

Brant Code SICP through exercise 1.11

492

#lang racket/base
;Exercise 1.1. Below is a sequence of expressions. What is the result printed b;y the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented.

;Exercise 1.1
;outputs 10
10
;outputs 12
(+ 5 3 4)
;outputs 8
(- 9 1)
;outputs 3
(/ 6 2)
;outputs 6
(+ (* 2 4) (- 4 6))
;outputs a=3
(define a 3)
;outputs b=4
(define b (+ a 1))
;outputs 19
(+ a b (* a b))
;outputs False
(= a b)
;outputs 4
(if (and (> b a) (< b (* a b))) b a)
;outputs 16
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
;outputs 6
(+ 2 (if (> b a) b a))
;outputs 16
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
;Exercise 1.2
;Translate the following expression into prefix form
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))

;Exercise 1.3
;Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
(define (square x) (* x x))
(define (TwoLargeSquares x y z)
(cond ((and (< x y) ( < x z)) (+ (square y) (square z)))
((and (< y x) (< y z)) (+ (square x) (square z)))
((and (< z x) (< z y)) (+ (square x) (square y)))))
(TwoLargeSquares 1 2 3)
;Exercise 1.4
;Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
;If b is greater than zero, then a adds with b, else, a subtract b resulting in a - - b (ie: a+b)

;Exercise 1.5
;Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:
(define (p) (p))

(define (test x y)
(if (= x 0)
0
y))
; Then he evaluates the expression (test 0 (p))
;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.)

;ANSWER Applicative-order evaluation
;0, Applicative-order evaluation will never substitute in p for p, because it runs the if statement and determines x=0, resulting in 0. Opposite: using applicative-order evaluation, the evaluation of (test 0 (p)) never terminates, because (p) is infinitely expanded to itself…
;Normal-order evaluation
;0, Normal-order evaluation will replace p with p before running the if statement and determining p irrelevant. Opposite: using normal-order evaluation, the expression evaluates, step by step, to 0

;**Normal order evaluates out->in (or left-> right)
;**Applicative order evaluates in->out (or right->left)

;Exercise 1.6
; Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
; Delighted, Alyssa uses new-if to rewrite the square-root program:

;(define (sqrtiter guess x)
; (new-if (good-enough? guess x)
; guess
; (sqrtiter (improve guess x)
; x)))
;ANSWER: Undefined. The "new-if" is cond, which cannot iterate, and will test each set until it finds a "true" value; if it does not find a true condition, it will return "undefined." Wrong. The function continues calling itself without a return, overflowing the stack and causing an out of memory error. IE: "sqrt-iter calls itself (recursive) but never evaluates, causing an infinite call and memory overflow.

;Exercise 1.7
;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?
(define (sqrt x)
(sqrtiter 1 x))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrtiter guess x)
(if (good-enough? guess x)
guess
(sqrtiter (improve guess x)
x)))
(sqrt 10000)
(define (bettersqrt x)
(bettersqrt-iter (* .1 x) x))
(define (bettersqrt-iter guess x)
(if (< (abs (- guess (newguess guess x))) 0.001)
guess
(bettersqrt-iter (newguess guess x) x)))
(define (newguess guess x)
(average guess (/ x guess)))
(bettersqrt 10000)

;Exercise 1.8. Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value
(define (cubert x)
(cubert-iter (* .1 x) x))
(define (cubert-iter guess x)
(if (< (abs (- guess (cubenewguess guess x))) 0.001)
guess
(cubert-iter (cubenewguess guess x) x)))
(define (cubenewguess guess x)
(/ (+ (/ x (square guess)) (* 2 guess)) 3))
(cubert 1000)

;Exercise 1.9. Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.
;PROCESS 1
; (define (+ a b)
; (if (= a 0)
; b
; (inc (+ (dec a) b))))
;Begin Expanding Process --- Recursive Process / Recursive Procedure
; inc (+ 3 5)
; inc inc (+ 2 5)
; inc inc inc (+ 1 5)
; inc inc inc inc (+ 0 5)
; returns 9

;Process 2
;(define (+ a b)
; (if (= a 0)
; b
; (+ (dec a) (inc b))))
;Begin Expanding Process --- Iterative Process / Recursive Procedure
; (+ 3 6)
; (+ 2 7)
; (+ 1 8)
; (+ 0 9) Returns value 9

; Exercise 1.10. The following procedure computes a mathematical function called Ackermann's function.
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))

;What are the values of the following expressions?
(A 1 10)
;1024
;(A 0 (A 1 9)
;(A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (A 0 (2))
(A 2 4)
;65536
(A 3 3)
;2^256 /// 65536 (16 bit interpreter IE: Max capability?)

;Consider the following procedures, where A is the procedure defined above:

;(define (f n) (A 0 n))
;(f n) = 2*n

;(define (g n) (A 1 n))
;(g n) = 2^n

;(define (h n) (A 2 n))
;(h n) = 2^(2^n)

;(define (k n) (* 5 n n))
;(k n) = 5(n^2)

;Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n2.

;Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.
;Recursive Function
(define (recursivefn n)
(cond ((< n 3) n)
(else (+ (recursivefn(- n 1)) (* 2 (recursivefn(- n 2))) (* 3 (recursivefn(- n 3)))))))
(recursivefn 10)
;Iterative Function
(define (iterativefn n)
(if (< n 3)
n
(iterativefniter 3 n 2 2 0 4)))
(define (iterativefniter count n firstoutput secondoutput thirdoutput answer)
(cond ((= n 3) 4)
((< count n) (iterativefniter (+ 1 count) n answer (* 2 firstoutput) (* 3 (/ secondoutput 2)) (+ answer (* 2 firstoutput) (* 3 (/ secondoutput 2)))))
(else answer)))
(iterativefn 10)
;(+ (- count 1) (* 2 (- count 2)) (* 3 (- count 3))))))))
; 3: firstoutput=2 secondoutput=2 thirdoutput=0 answer=4
; 4: firstoutput=4(answer) secondoutput=4(2lastfirstoutput) thirdoutput=3(3lastsecondoutput/2) answer=11
; 5: firstoutput=11(answer) secondoutput=24(2answer 2 ago) thirdoutput=32(3lastsecondoutput/2) answer=25
; 6: firstoutput=25(answer) secondoutput=211(2answer2ago=2lastfirstoutput) thirdoutput=34(3answer3ago=3lastsecondoutput/2) answer=36