## [Section 1.2 - Procedures and the Processes They Generate](https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2)

## Exercises
```scheme
;; ======================================================
;; Load definitions and functions from earlier chapters
;; ======================================================
(load "c01s01.scm")
;; ======================================================
```

**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.
```scheme
(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))
```
Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

**Exercise 1.10** The following procedure computes a mathematical function called Ackermann's function.
```scheme
(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?
```scheme
(A 1 10)
(A 2 4)
(A 3 3)
```
Consider the following procedures, where A is the procedure defined above:
```scheme
(define (f n) (A 0 n))
(define (g n) (A 1 n))
(define (h n) (A 2 n))
(define (k n) (* 5 n n))
```
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 5n^2.

```scheme
(A 1 10)
; 2^10 because the final argument, (A x (- y 1))
;       will evaluate 9 times before y = 1 and the
;       function stops at 2
(A 2 4)
; 2^16 because the function first evaluates to (A 1 (A 2 3))
;       = (A 1 (A 1 (A 2 2)))
;       = (A 1 (A 1 (A 1 (A 2 1))))
;       = (A 1 (A 1 (A 1 2)))
;       = (A 1 (A 1 (A 0 (A 1 1))))
;       = (A 1 (A 1 (A 0 2)))
;       = (A 1 (A 1 (2 * 2)))
;       = (A 1 (A 1 4))
;       = (A 1 (A 0 (A 1 3)))
;       = (A 1 (A 0 (A 0 (A 1 2))))
;       = (A 1 (A 0 (A 0 (A 1 2))))
;       = (A 1 (A 0 (A 0 (A 0 (A 1 1)))))
;       = (A 1 (A 0 (A 0 (A 0 2))))
;       = (A 1 (A 0 (A 0 (2 * 2))))
;       = (A 1 (A 0 (A 0 4)))
;       = (A 1 (A 0 (2 * 4)))
;       = (A 1 (A 0 8))
;       = (A 1 (2 * 8))
;       = (A 1 16)
;       which as we saw above will evaluate to 2^16
(A 3 3)
; 2^16 because the function first evaluates to (A 2 (A 3 2))
;       = (A 2 (A 2 (A 3 1)))
;       = (A 2 (A 2 2))
;       = (A 2 (A 1 (A 2 1)))
;       = (A 2 (A 1 2))
;       = (A 2 (A 0 (A 1 1)))
;       = (A 2 (A 0 2))
;       = (A 2 (2 * 2))
;       = (A 2 4)
;       which as we saw above will evaluate to 2^16

(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^(n^2)
```

**Exercise 1.11** A function f is defined by the rule that f(n) = n if n < 3 and f(n) = f(n-1) + f(n-2) + f(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.
```scheme
; Recursive solution
(define (f n)
  (cond ((< n 3) n)
	     (else (+ (+ (f (- n 1)) (* 2 (f (- n 2))))  
               (* 3 (f (- n 3)))))))
; Iterative solution
; a <- a + 2*b + 3*c
; b <- a
; c <- b
(define (f n)
  (f-iter 2 1 0 n))
(define (f-iter a b c count)
  (if (= count 0)
      c
      (f-iter (+ a (* b 2) (* c 3)) a b (- count 1))))
```

**Exercise 1.12** The following pattern of numbers is called Pascal's triangle.  
```Tex
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
```
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a procedure that computes elements of Pascal's triangle by means of a recursive process.
```scheme
(define (pascal row col)
  (cond ((= row 1) 1)
	    ((= row col) 1)
	    ((= col 1) 1)
        (else (+ (pascal (- row 1) (- col 1))
		         (pascal (- row 1) col)))))
(pascal 1 1)
;Value: 1
(pascal 2 1)
;Value: 1
(pascal 2 2)
;Value: 1
(pascal 3 1)
;Value: 1
(pascal 3 2)
;Value: 2
(pascal 3 3)
;Value: 1
(pascal 4 2)
;Value: 3
(pascal 4 3)
;Value: 3
(pascal 5 3)
;Value: 6
```

**Exercise 1.13** Prove that Fib(n) is the closest integer to (phi^n)/5, where phi = (1 + sqrt(5))/2. Hint: Let psi = (1 - sqrt(5))/2. Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that Fib(n) = (n - n)/5.

```Text
Base case: show for n = 0 and n = 1

n=0: Fib(0) = (phi^0 - rho^0)/sqrt(5)
          0 = (0 - 0)/sqrt(5)
          0 = 0
n=1: Fib(1) = (phi^1 - rho^1)/sqrt(5)
          1 = ((1 + sqrt(5))/2 - (1 - sqrt(5))/2)/sqrt(5)
          1 = ((2*(sqrt(5)))/2) / sqrt(5)
          1 = (sqrt(5)) / sqrt(5)
          1 = 1

Induction case: take k >= 1 as given and assume relation is true for all n >= 0

 Fib(k + 1) = Fib(k) + Fib(k - 1)

By the induction hypothesis, we have that
 Fib(k + 1) = ((phi^(k) - rho^(k))/sqrt(5)) + ((phi^(k - 1) - rho^(k - 1))/sqrt(5))
 Fib(k + 1) = ((phi)^(k) + (phi)^(k - 1))/sqrt(5) - ((rho)^(k) + (rho)^(k - 1))/sqrt(5)

Since phi^2 = phi + 1 and rho^2 = rho + 1, we observe that
 Fib(k + 1) = (((phi)^(k - 1)(phi + 1)) - ((rho)^(k - 1)(rho + 1)))/sqrt(5)
 Fib(k + 1) = (((phi)^(k - 1)(phi^2)) - ((rho)^(k - 1)(rho^2)))/sqrt(5)
 Fib(k + 1) = ((phi)^(k + 1) - (rho)^(k + 1))/sqrt(5)
```

**Exercise 1.14** Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

**Exercise 1.15** The sine of an angle (specified in radians) can be computed by making use of the approximation sin x ~ x if x is sufficiently small, and the trigonometric identity  

sin r = 3 sin r/3 - 4 sin^3 r/3  

to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered "sufficiently small" if its magnitude is not greater than 0.1 radians.) These ideas are incorporated in the following procedures:
```scheme
(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine angle)
   (if (not (> (abs angle) 0.1))
       angle
       (p (sine (/ angle 3.0)))))
```
a.  How many times is the procedure p applied when (sine 12.15) is evaluated?
> 6  

b. What is the order of growth in space and number of steps (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated?
> n  

**Exercise 1.16** Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that (b^(n/2))^2 = (b^2)^(n/2), keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product a bn is unchanged from state to state. At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.)  
```scheme
; define the iterative algorithm
(define (rapid-expt-iter b n product)
    (cond ((= n 0) product)
          ; use successive squaring for even powers greater than 2
          ((and (even? n) (> n 2)) (rapid-expt-iter b (/ n 2) (* product (* b b))))
          ; else use simple iteration
          (else (rapid-expt-iter b (- n 1) (* product b)))))
; call iterative algorithm in a shorter-form function which initializes a to be 1
(define (rapid-expt b n)
    (rapid-expt-iter b n 1))
; Test some outputs
(rapid-expt 2 4)
; Value: 16
(rapid-expt 3 5)
; Value: 243
```

**Exercise 1.17** The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure:

```scheme
(define (* a b)
  (if (= b 0)
      0
      (+ a (* a (- b 1)))))
```

This algorithm takes a number of steps that is linear in b. Now suppose we include, together with addition, operations double, which doubles an integer, and halve, which divides an (even) integer by 2. Using these, design a multiplication procedure analogous to fast-expt that uses a logarithmic number of steps.

```scheme
; define double
(define (double x) (* x 2))
; define halve
(define (halve x) (/ x 2))
; define the iterative algorithm
(define (rapid-mult-iter a b product)
    (cond ((= b 0) product)
          ; if b can be halved and is greater than 2, halve it and double the product of the multiplication
          ((and (even? b) (> b 2)) (+ product (double (rapid-mult-iter a (halve b) 0))))
          ; else use simple iteration
          (else (rapid-mult-iter a (- b 1) (+ product a)))))
; call iterative algorithm in a shorter-form function which initializes a to be 1
(define (rapid-mult a b)
    (rapid-mult-iter a b 0))
```
