Let’s call the number of people in the circle n and number the positions from 1 to n. We’ll assume that the killing of every third person starts with killing the person in position number 3. […]

As we saw above, if the person we care about is in position 3, that person is the one killed and hence definitely not a survivor:

1234567 (define survives?(lambda (position n)(if (< n 3)#t(if (= position 3)#f;we still need to write this part))))Suppose we aren’t interested in the person in position number 3 but rather in some other person—let’s say J. Doe. The person in position number 3 got killed, so now we only have n – 1 people left. Of that smaller group of n – 1, there will still be two survivors, and we still want to know if J. Doe is one of them.

**Exercise 3.9** How about the people who were in positions 1 and 2; what position numbers are they in after the renumbering?

**Solution:** In the example there were 8 persons at the start. After killing #3, the new #1 is #4, the new #2 is #5*, etc. #2 is #(n-1) and #1 is #(n-2).

*: clearly a reference to the Prisoner ;)

**Exercise 3.10** Write a procedure for doing the renumbering. It should take two arguments: the old position number and the old number of people (n). (It can assume that the old position number won’t ever be 3, because that person is killed and hence doesn’t get renumbered.) It should return the new position number.

**Solution:**

1 2 3 4 5 |
(define renumber (lambda (position n) (if (< position 3) (+ position (- n 3)) (- position 3)))) |

**Exercise 3.11** Finish writing the survives? procedure, and carefully test it with a number of cases that are small enough for you to check by hand but that still cover an interesting range of situations.

**Solution:**

1 2 3 4 5 6 7 |
(define survives? (lambda (position n) (if (< n 3) #t (if (= position 3) #f (survives? (renumber position n) (- n 1)))))) |

I made a table for testing if the program works correctly. Each line represents a new round, i.e. somebody got killed. The killed person is labeled with *X.*

1 2 3 4 5 6 7 |
1 2 3 4 5 6 7 8 6 7 X 1 2 3 4 5 3 4 5 6 X 1 2 X 1 2 3 4 5 3 4 X 1 2 X 1 2 3 X |

**Exercise 3.15** Consider the following two procedures:

1 2 3 4 5 6 7 8 9 10 11 |
(define f (lambda (n) (if (= n 0) 0 (g (- n 1))))) (define g (lambda (n) (if (= n 0) 1 (f (- n 1))))) |

**a.** Use the substitution model to evaluate each of (f 1), (f 2), and (f 3).

**b.** Can you predict (f 4)? (f 5)? In general, which arguments cause f to return 0 and which cause it to return 1? (You need only consider nonnegative integers.)

**c.** Is the process generated by f iterative or recursive? Explain.

**Solution:** **a.** I started to define each function mathematically. and . Now we can see what’s happening.

**b.** You see that odd numbers lead to and even numbers lead to .

**c.** I would say that f’s process iterative because you could stop at any time and just continue later with the saved parameters.

**Exercise 3.16** Consider the following two procedures:

1 2 3 4 5 6 7 8 9 |
(define f (lambda (n) (if (= n 0) 0 (+ 1 (g (- n 1)))))) (define g (lambda (n) (if (= n 0) 1 (+ 1 (f (- n 1)))))) |

**a.** Use the substitution model to illustrate the evaluation of (f 2), (f 3), and (f 4).

**b.** Is the process generated by f iterative or recursive? Explain. c. Predict the values of (f 5) and (f 6).

**Solution:** **a.** Same trick at last time.

and

Therefore:

**b.** The process is recursive because you can’t just stop and continue later. This happens because of the (+ 1 f) which have to be saved on the stack.

**c.** The values of (f 5) and (f 6) are both 6.

**Exercise 3.18** We’ve already seen how to raise a number to an integer power, provided that the exponent isn’t negative. We could extend this to allow negative exponents as well by using the following definition:

**a.** Using this idea, write a procedure power such that (power b n) raises b to the n power for any integer n.

**b.** Use the substitution model to show how (power 2 -3) would be evaluated. (You can leave out steps that just determine which branch of a cond or if should be taken.) Does your procedure generate a recursive process or an iterative one?

**Solution** **a.**

1 2 3 4 5 |
(define power (lambda (b n) (cond ((= n 0) 1) ((> n 0) (* b (power b (- n 1)))) ((< n 0) (* (/ 1 b) (power b (+ n 1))))))) |

**b.**

1 2 3 4 5 6 7 |
(power 2 -3) (* (/ 1 2) (power 2 (+ -3 1))) (* 0.5 (power 2 -2)) (* 0.5 0.5 (power 2 -1)) (* 0.25 0.5 (power 2 0)) (* 0.125 1) 0.125 |

It’s an recursive one because you can’t determine the final solution without knowing the beginning parameters.

**Exercise 3.19** Prove that, for all nonnegative integers n and numbers a, the following procedure computes the value :

1 2 3 4 |
(define foo (lambda (n a) (if (= n 0) a (foo (- n 1) (+ a a))))) |

**Solution** Let’s define the function again mathematically. . The base case is correct:

.

We assume that which can be written as .

With our assumption it should follow that

If we look at the definition, we see that which implies

Good to find someone who started reading the book and doing the exercises before me to compare my solutions… For ex. 3.16 (a), is your mathematical equivalent for g correct? I thought it would be: g(n) = f(n – 1) + 1 for n > 0 .

So f(5) = 5, f(6) = 6, … f(n) = n

Hey, yeah you are right. Fixed it. Thanks!

“*: clearly a reference of the Prisoner ;)”

Up the irons

Great song and great TV series!