At the moment I’m working through Concrete Abstractions which is so far pretty great. I enjoy the writing style and the difficulty of the exercises. You can buy a used copy for about 10 bucks or read it for free online.

I’m going to present solutions to some exercises. However, I recommend working through this book by yourself. You’ll learn a lot!

**Exercise 2.9** Write a procedure that computes the number of 6s in the decimal representation of an integer. Generalize this to a procedure that computes the number of d’s, where d is another argument.

**Solution:** Idea behind this algorithm is to find the last digit, evaluate it and shorten the number by one digit. You can achieve the first one by taking the reminder of the number n and 10. For example: Reminder of , because .

The last thing to do is to shorten the number. It’s easy done by subtracting the reminder of the number and dividing it by ten: $latex 151 – 1 = 150 \implies 150 / 10 = 15

1 2 3 4 5 6 7 8 9 |
(define (num-of-d n d) (if (< n 10) (if (= n d) 1 0) (+ (if (= d (remainder n 10)) 1 0) (num-of-d (/ (- n (remainder n 10)) 10) d)))) |

**Exercise 2.12** Any positive integer can be expressed as , where is odd, that is, as a power of 2 times an odd number. We call the exponent of 2 in . For example, the exponent of 2 in 40 is 3 (because ) whereas the exponent of 2 in 42 is 1. If i itself is odd, then n is zero. If, on the other hand, i is even, that means it can be divided by 2. Write a procedure for finding the exponent of 2 in its argument.

**Solution:** The trick here is quite easy. We know that needs to be an integer. I just test this by comparing the results of . Furthermore, I calculated a upper limit for n which equals . If you choose the smallest positive , i.e. 1 then . This makes the implementation in Scheme a bit more convenient.

1 2 3 4 5 6 7 |
(define (find-n i) (find-n-iter i (round (/ (log i) (log 2))))) (define (find-n-iter i n) (if (= (/ i (expt 2 n)) (round (/ i (expt 2 n)))) n (find-n-iter i (- n 1)))) |

**Exercise 2.16** Consider the following procedure foo:

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

Use induction to prove that (foo x n) terminates with the value

for all values of and for all integers . You may assume that expt works correctly, (i.e., (expt b m) returns ).

*Info: I show a inductive proof a bit more verbose in the following exercise.*

**Solution:** The base case is .

We assume that .

Therefore which is

**Exercise 2.18** Prove by induction that for every nonnegative integer n the following procedure computes 2n:

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

**Solution:** At first look at the definition. and .

Now we test the base case: which is true.

The inductive step is where we assume that . Therefore

**Exercise 2.20** Prove that the following procedure computes for any nonnegative integer . That is, (f n) computes for any integer $latex n \geq 0.

1 2 3 4 5 |
(define f (lambda (n) (if (= n 0) 0 (+ (f (- n 1)) (/ 1 (* n (+ n 1))))))) |

**Solution:** The function is defined as and . First we test the base case. . Afterwards the inductive step:

where we assume that . Therefore, You can see that the numerator is the first binomial formula:

The one-layer thinking maxim:Don’t try to think recursively about a recursive process.