Exercise 1.1

Exercise Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented. Student Response 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 10 ; 10 (+ 5 3 4) ; 12 (- 9 1) ; 8 (/ 6 2) ; 3 (+ (* 2 4) (- 4 6)) (define a 3) ; a = 3 (define b (+ a 1)) ; b = 4 (+ a b (* a b)) ; 19 (= a b) ; #f (if (and (> b a) (< b (* a b))) b a) ; b (cond ((= a 4) 6) ((= b 4) (+ 6 7 a)) ; 16 (else 25)) ; 25 (+ 2 (if (> b a) b a)) ; 6 (* (cond ((> a b) a) ; 4 ((< a b) b) ; 4 (else -1)) ; -1 (+ a 1)) ; 5 Evaluation is there anything special that i need to think through? Looking at this code, it’s a great introduction to Scheme (part of SICP - Structure and Interpretation of Computer Programs). Here are the key things to think through: ...

2 min

Exercise 1.2

Exercise Translate the following expression into prefix form $5 + 4 + (2 - ( 3- (6 + \frac{4}{5})))/ 3 \cdot (6-2)\cdot(2-7)$ Student Response 1 2 3 (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) ; -37/150 Evaluation explain the code Let me break down this Scheme expression: This is a complex mathematical expression that uses nested functions for arithmetic operations. Let’s solve it step by step from the inside out: ...

2 min

Exercise 1.3

Exercise Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Student Response 1 2 3 4 5 6 7 (define (sum-of-squares x y) (+ (* x x) (* y y))) (define (sum-of-squares-of-two-largest x y z) (cond ((and (<= x y) (<= x z)) (+ (* y y) (* z z))) ((and (<= y x) (<= y z)) (+ (* x x) (* z z))) (else (+ (* x x) (* y y))))) Teacher Evaluation Evaluation Explain this code Let me explain the Scheme code from the main file: ...

2 min

Exercise 1.4

Exercise 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: 1 2 (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)) Student Response Predicate: 1 (> b 0) Consequent: 1 + Alternative: 1 - So that means that if b is greater than 0, then we add a and b, otherwise we subtract b from a. We can use compound expressions as operators in procedures. ...

2 min

Exercise 1.5

Exercise 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: 1 2 3 4 5 6 (define (p) (p)) (define (test x y) (if (= x 0) 0 y)) Then he evaluates the expression 1 (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.) ...

3 min

Exercise 1.6

Exercise 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: 1 2 3 (define (new-if predicate then-clause else-clause) (cond (predicate then-clause) (else else-clause))) Eva demonstrates the program for Alyssa: 1 2 3 4 5 (new-if (= 2 3) 0 5) 5 (new-if (= 1 1) 0 5) 0 Delighted, Alyssa uses new-if to rewrite the square-root program: ...

4 min

Exercise 1.7

Exercise 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? ...

5 min