. writes:
> > > (define (f n)
> > > (define (iter a b c count)
> > > (if (= count 0)
> > > a
> > > (iter b c (+ c (* 2 b) (* 3 a)) (- count 1))))
> > > (iter 0 1 2 n))
> >
> > They are f(0), f(1), and f(2). They come from f(n) = n if n < 3.
>
> I see. Thank you.
>
> There is another issue.
> I don't know how to design a recursive procedure (which generates a
> recursive or an iterative process).
>
> For example:
> It's not clear (for me) from the text of the exercise that we should
> use the following to calculate the third operand.
> (+ c (* 2 b) (* 3 a))
That's the definition, f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. (That
should be "if n >= 3".) The a, b, c in iter are three consecutive
elements of the sequence of f(k) for k = 0, 1, 2, 3, ...
Let me show you the simple solution:
(define (f n)
(case n
((0 1 2) n) ; n < 3
(else (+ (f (- n 1)) ; n >= 3
(* 2 (f (- n 2)))
(* 3 (f (- n 3)))))))
I just wrote the given definition in Scheme. The main reason I
wouldn't do this in practice is not the deep recursion but the fact
that the recursive calls will do the same work many times. I think
SICP illustrates this with Fibonacci numbers.
There are naturally tree-shaped problems where this kind of recursion
is the right thing.
For the given definition, consider the following variant, because I
think the backwards counting is needlessly obscure.
(define (f n)
(define (iter a b c k)
(if (= k n)
a
(iter b c (+ c (* 2 b) (* 3 a)) (+ k 1))))
(iter 0 1 2 0))
This goes through the same calculations, but now at every step the
meaning of the parameter can be stated simply: the three numbers a, b,
c are f(k), f(k + 1), and f(k + 2). That's why a is f(n) if k = n. If
not, try recursively with f(k + 1), f(k + 2), f(k + 3).
Hope this helps.
> Another example:
> "Exercise 1.12. The following pattern of numbers is called Pascal's
> triangle.
> 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." [1]
It says "numbers at the edge" are all 1, and "each number is the sum
of the two numbers above it".
You are asked about c(r,k). If it's "at the edge", you know the
answer. The solution [2] you showed did not actually identify the
numbers at the edge, it identified the numbers _outside the whole
triangle_ and set them to 0. That's valid mathematics, but not
provided in the given definition. Try "k = 0 or r = k" as the
definition of "at the edge". (Avoid calling with k < 0 or k > r then.)
> How to understand from the text of the exercise that I should write
> the following.
>
> (+ (pascal-triangle (- row 1) (- col 1))
> (pascal-triangle (- row 1) col))
If c(r,k) is not "at the edge", you need the "two numbers above it",
c(r - 1, k - 1) and c(r - 1, k - 1). The answer is their sum. Row
numbers grow downwards, column numbers to the right, like in a matrix.
> But the design part of the code writing is not clear.
> Could you explain it?
Name the things. (pascal-triangle row col) is an ok name in a program,
c(r,k) is shorter. Look for base cases where the answer is known and
for the recursive cases where the answer is computed from simpler
instances of the same problem - which instances, how? - and then just
write in the following pattern:
(define (solve parameters)
(if (base-case? parameters)
(answer parameters)
(solve (simpler parameters))))
"simpler" means closer to some base case in some way, but that is
probably taken care of when you are given a definition like here.
The numbers in that triangle are very important, by the way, and there
are interesting other ways of computing them.
Keeping these for reference: