Structure and Interpretation of Computer Programs

Free Structure and Interpretation of Computer Programs by Harold Abelson and Gerald Jay Sussman with Julie Sussman

Book: Structure and Interpretation of Computer Programs by Harold Abelson and Gerald Jay Sussman with Julie Sussman Read Free Book Online
Authors: Harold Abelson and Gerald Jay Sussman with Julie Sussman
1.6180
    is the
golden ratio
, which satisfies the equation
    φ
2 =
φ
+ 1
    Thus, the process uses a number of steps that grows exponentially
with the input. On the other hand, the space required grows only
linearly with the input, because we need keep track only of which
nodes are above us in the tree at any point in the computation. In
general, the number of steps required by a tree-recursive process will be
proportional to the number of nodes in the tree, while the space
required will be proportional to the maximum depth of the tree.
    We can also formulate an iterative process for computing the
Fibonacci numbers. The idea is to use a pair of integers
a
and
b
, initialized to
F
i
b
(1) = 1 and
F
i
b
(0) = 0,
and to repeatedly apply the simultaneous
transformations
    a

a
+
b

b

a
It is not hard to show that, after applying this transformation
n
times,
a
and
b
will be equal, respectively, to
F
i
b
(
n
+ 1) and
F
i
b
(
n
). Thus, we can compute Fibonacci numbers iteratively using
the procedure

    (define (fib n)
  (fib-iter 1 0 n))

(define (fib-iter a b count)
  (if (= count 0)
      b
      (fib-iter (+ a b) a (- count 1))))

    This second method for computing
F
i
b
(
n
) is a linear iteration. The
difference in number of steps required by the two methods – one linear in
n
,
one growing as fast as
F
i
b
(
n
) itself – is enormous, even for
small inputs.
    One should not conclude from this that tree-recursive processes are
useless. When we consider processes that operate on hierarchically
structured data rather than numbers, we will find that tree recursion
is a natural and powerful tool. 32 But even in numerical operations,
tree-recursive processes can be useful in helping us to understand and
design programs. For instance, although the first
fib
procedure
is much less efficient than the second one, it is more
straightforward, being little more than a translation into Lisp of the
definition of the Fibonacci sequence. To formulate the iterative
algorithm required noticing that the computation could be recast as an
iteration with three state variables.

Example: Counting change
    It takes only a bit of cleverness to come up with the iterative
Fibonacci algorithm. In contrast, consider the
following problem: How many different ways can we make change of $ 1.00,
given half-dollars, quarters, dimes, nickels, and pennies? More
generally, can we write a procedure to compute the number of ways to
change any given amount of money?
    This problem has a simple solution as a recursive procedure. Suppose
we think of the types of coins available as arranged in some order.
Then the following relation holds:

    The number of ways to change amount
a
using
n
kinds of coins equals

the number of ways to change amount
a
using all but the first
kind of coin, plus
the number of ways to change amount
a
-
d
using all
n
kinds of
coins, where
d
is the denomination of the first kind of coin.

    To see why this is true, observe that the ways to make change can be
divided into two groups: those that do not use any of the first kind
of coin, and those that do. Therefore, the total number of ways to
make change for some amount is equal to the number of ways to make
change for the amount without using any of the first kind of coin,
plus the number of ways to make change assuming that we do use the
first kind of coin. But the latter number is equal to the number of
ways to make change for the amount that remains after using a coin of
the first kind.
    Thus, we can recursively reduce the problem of changing a given amount
to the problem of changing smaller amounts using fewer kinds of coins.
Consider this reduction rule carefully, and convince yourself that we
can use it to describe an algorithm if we specify the following
degenerate cases: 33

If
a
is exactly 0, we should count that as 1 way to make change.
If
a
is less than 0, we should count that as 0 ways to make change.
If
n
is 0, we should count that as 0 ways to

Similar Books

A Baby in His Stocking

Laura marie Altom

The Other Hollywood

Legs McNeil, Jennifer Osborne, Peter Pavia

Children of the Source

Geoffrey Condit

The Broken God

David Zindell

Passionate Investigations

Elizabeth Lapthorne

Holy Enchilada

Henry Winkler