The lab proceeds by considering several solutions to various problems:
Problem -- Sum: Find the sum of a list of numbers.
Solution 1:The first solution seems to follow a now-familiar recursive format:
(define sum
(lambda (ls)
(if (null? ls)
0
(+ (car ls) (sum (cdr ls)))
)
)
)
While this procedure works correctly, it is not terribly efficient either
in terms of time or required memory within the machine. To understand why,
we trace the execution of this procedure on the list (1 2 3 4).
Here, when we type (sum '(1 2 3 4)) the machine checks for a null list, recognizes that '(1 2 3 4) is not null, and goes the the else clause of the if. This means that sum will be called recursively with parameter (2 3 4). However, once (sum '(2 3 4)) is computed, we still will have to add 1 to the result to get the final answer. Thus, the machine will need to store 1 until the recursive step is completely done. Similar comments apply at each stage. Thus, when the machine finally evaluates (sum '()) and obtains 0, the sum has been called 5 times, and intermediate values are stored at each stage.
While this solution is correct, after the base case is computed (at the right of the above diagram), the machine must come back one call at a time, using previous results and making further computations.
Solution 2: The next solution adds a running sum parameter.
(define sum
(lambda (ls)
(sum-kernel ls 0)
)
)
(define sum-kernel
(lambda (ls running-sum)
(if (null? ls)
running-sum
(sum-kernel (cdr ls) (+ running-sum (car ls)))
)
)
)
In this approach, recursion proceeds from (1 2 3 4) toward the
null list (). However, once this base case is reached, the result
(10) is returned directly by each preceding procedure call. In this case,
the machine does not need to combine the result at one stage with values at
a previous stage, so earlier values need not be stored during recursion.
This direct return of a result following recursion is called tail
recursion. The following diagram provides a graphical picture of this
processing.
Since Scheme is sophisticated enough to identify when tail recursion is present, tail recursion can run particularly efficiently within Scheme.
Solution 3: While Solution 2 was quite efficient, it requires a separate procedure sum-kernel. For simple exercises, such supplemental procedure definitions are unlikely to cause much trouble. When tackling complex problems, however, the number of such auxiliary procedure names can proliferate dramatically, and it can be difficult to keep track of just which names apply to what.
This problem can be solved easily by defining the sum-kernel procedure within sum. This may be done with a letrec statement:
(define sum
(lambda (ls)
(letrec ((sum-kernel
(lambda (ls running-sum)
(if (null? ls)
running-sum
(sum-kernel (cdr ls) (+ running-sum (car ls)))
)
)
))
(sum-kernel ls 0)
)
)
)
Solution 4:
Solution 3 is tail recursive and contains sum-kernel as a local
procedure. Thus, it runs efficiently and avoids possible name conflicts
with procedures defined elsewhere. Solution 3, however, seems somewhat
cumbersome, since much of the body of the letrec expression seems
rather formal. In particular, the lambda reference is used to
identify parameters in one stage and the (sum-kernel ls 0) gives
values to these parameters as part of a procedure call.The named let combines this formalism in a somewhat more compact form:
(define sum
(lambda (lst)
(let sum-kernel ((ls lst) (running-sum 0))
(if (null? ls)
running-sum
(sum-kernel (cdr ls) (+ running-sum (car ls)))
)
)
)
)
Here, the local procedure name (sum-kernel) follows immediately
after let, and its parameters (ls and
running-sum) are identified and initialized next. The body of the
procedure follows. In such named let, it is understood that the
local procedure will be called with the given initial parameters, so there
is not need to write out the call separately.
In the next example, tail recursion is particularly helpful.
Problem -- Maximum: Find the maximum value within a list of numbers.
To find a maximum, there must be at least one item on the list. Otherwise, a maximum will be undefined. Thus, the base case arises when a list contains just one element.
Solution 1: The first solution is particularly unsophisticated:
(define maximum
(lambda (ls)
(cond ((null? (cdr ls)) (car ls))
((< (car ls) (maximum (cdr ls))) (maximum (cdr ls)))
(else (car ls))
)
)
)
While this code produces the correct answer, it calls maximum
recursively twice in the case that the largest value occurs later in the
list.
(define maximum
(lambda (ls)
(if (null? (cdr ls))
(car ls)
(let ((max-in-rest (maximum (cdr ls)))
(first-item (car ls)))
(if (< first-item max-in-rest)
max-in-rest
first-item
)
)
)
)
)
Explain why this approach is more efficient that the previous solution.
Solution Outline: Since an average requires both a sum of the items and a count of the number of items present, any solution must do both tasks. A tail recursive approach to this problem uses parameters to keep a running sum and a running count of the number of items processed.
Comment: Since three results are desired, we must decide how these results should be returned. One natural approach would be to place all three results on a single list, with the maximum first, the minimum second, and the average third.
Hint: In binding local variables within a let expression, one variable may be bound to the result of an if expression.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153/lab-tail-recursion.html