From: Ramakrishnan Muthukrishnan Date: Wed, 24 Mar 2010 04:32:37 +0000 (+0530) Subject: - Exercise 1.1 to 1.5. X-Git-Url: https://git.rkrishnan.org/pf/content/fr.html?a=commitdiff_plain;h=b01cd3c4a7d5a3409e584d85bde1e007b30ab4fb;p=sicp.git - Exercise 1.1 to 1.5. - Newton's method of square root finding. --- diff --git a/chapter1/ch1_1.clj b/chapter1/ch1_1.clj index 7361cd3..733f23c 100644 --- a/chapter1/ch1_1.clj +++ b/chapter1/ch1_1.clj @@ -6,4 +6,112 @@ (+ (square x) (square y))) (defn f [a] - (sum-of-squares (+ a 1) (* a 2))) \ No newline at end of file + (sum-of-squares (+ a 1) (* a 2))) + +(defn abs + "find absolute value of x" + [x] + (if (< x 0) (- x) x)) + +(defn abs1 [x] + (cond (< x 0) (- x) + :else x)) + +;; exercise 1.1: What is the result printed by the interpreter in response +;; to each expression (in order) ? + +(def a 3) +(def b (+ a 1)) + +(+ a b (* a b)) ; 19 +(= a b) ; false + +(if (and (> b a) (< b (* a b))) + b + a) ; 4 + +(cond (= a 4) 6 + (= b 4) (+ 6 7 a) + :else 25) ; 16 + +(+ 2 (if (> b a) b a)) ; 6 + +(* (cond (> a b) a + (< a b) b + :else -1) + (+ a 1)) ; 16 + +;; exercise 1.2: Translate the given expression into prefix form +(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 1 5))))) + (* 3 (- 6 2) (- 2 7))) ; -71/300 + +;; exercise 1.3: Define a procedure that takes three numbers as +;; arguments and returns the sum of the squares of +;; the two larger numbers. +(defn sort3 [a b c] + (cond (> b a) (sort3 b a c) + (< b c) (sort3 a c b) + :else [a b c])) + +(defn sum-of-sq-of-two-largest [a b c] + (apply sum-of-squares (take 2 (sort3 a b c)))) + +;; 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: +;; (defn a-plus-abs-b [a b] +;; ((if (> b 0) + -) a b)) +(comment + If b is positive, we do (+ a b) and if it is negative, we do (- a b). + This makes use of the fact that the first element in a list is an + operand. Here, the operand is chosen based on other operators. + ) + +;; 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: +;; (defn p [] (p)) +;; (defn 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? +(comment + In the case of applicative order evaluation, the test gets into + and infinite loop (eventually using all the stack), as the parameters + are evaluated before they are actualy used in the function. + ) +;; What behavior will he observe with an interpreter that uses +;; normal-order evaluation? Explain your answer. +(comment + It will print 0, as (p) is not evaluated in this case. + ) + +;; 1.1.7 Square root finding using Newton's method +(defn sqrt-iter [guess x] + (if (good-enough? guess x) + guess + (sqrt-iter (improve guess x) + x))) + +(defn improve [guess x] + (average guess (/ x guess))) + +(defn average [x y] + (/ (+ x y) 2)) + +(defn good-enough? [guess x] + (< (abs (- (square guess) x)) 0.001)) + +(defn sqrt [x] + (sqrt-iter 1.0 x)) + +;; exercise 1.6