# Chapter 1 ¶

## Exercise 1.01 ¶

What is printed in response to each expression?

10

10

(+ 5 3 4)

12

(- 9 1)

8

(/ 6 2)

3.0

(+ (* 2 4) (- 4 6))

6

(setv a 3)
a

3

(setv b (+ a 1))
b

4

(+ 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.02 ¶

Translate the following expression into prefix form:

$\frac{5+4\left(2-\left(3-\left(6+\frac{4}{5}\right)\right)\right)}{3\left(6-2\right)\left(2-7\right)}$
(/
(+ 5 4
(- 2
(- 3
(+ 6
(/ 4 5)))))
(* 3
(- 6 2)
(- 2 7)))

-0.24666666666666667


## Exercise 1.03 ¶

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

(defn square [x] (* x x))

(defn sum-of-squares [x y]
(+ (square x) (square y)))

(defn sum-of-squares-of-two-largest [x y z]
(cond
[(and (>= x y) (>= y z)) (sum-of-squares x y)]
[(and (>= y x) (>= x z)) (sum-of-squares x y)]
[(and (>= x y) (>= z y)) (sum-of-squares x z)]
[True (sum-of-squares y z)]))

(sum-of-squares-of-two-largest 2 3 4)

25

(sum-of-squares-of-two-largest 3 4 2)

25

(sum-of-squares-of-two-largest 3 2 4)

25

(sum-of-squares-of-two-largest 4 3 2)

25

(sum-of-squares-of-two-largest 4 2 3)

25


## Exercise 1.04 ¶

### Question ¶

Describe the behavior of the following procedure:

(defn a-plus-abs-b [a b]
((if (> b 0) + -) a b))


### Solution ¶

The if expression checks whether the whether to apply the  -  or  +  operation to  a  and  b  .

If  b  is positive, we will add  a  to  b  , otherwise we will subtract  a  from  b  .

## Exercise 1.05 ¶

### Question ¶

Ben Bitdiddle has invented a test to determine whether the interpreter he is faced is 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))


and a test expression:

(test 0 (p))


What behavior will Ben observe with an applicative-order interpreter? With a normal-order interpreter?

Note

Assume that the evaluation rule for the special form if is the same regardless of the interpreter’s evaluation order:

the predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.

## Exercise 1.08 ¶

### Question ¶

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

$\frac{{x}/{y^2} + 2y}{3}$

Use this formula to implement a cube-root procedure analogous to the square-root procedure from section 1.1.7 of the text.

### Solution ¶

(defn square [x] (* x x))

(defn cube [x] (* (square x) x))

(defn cube-root-iter [guess x]
(if (good-enough? guess x)
guess
(cube-root-iter (improve guess x)
x)))

(defn improve [guess x]
(/
(+ (/ x (square guess))
(* 2 guess))
3))

(defn good-enough? [guess x]
(< (abs (- (cube guess) x)) 0.001))


### Test ¶

(cube-root-iter 1 8)

2.000004911675504

(cube-root-iter 1 27)

3.0000005410641766

(cube-root-iter 1 64)

4.000017449510739

(cube-root-iter 1 125)

5.000000000287929

(cube-root-iter 1 100)

4.641590111046459