CSC 153  Grinnell College  Spring, 2005 
Computer Science Fundamentals  
Laboratory Exercise  
This laboratory provides additional examples and experience with local procedures and introduces named Let expressions. The laboratory also provides practices rewriting procedures to become tail recursive.
The lab proceeds by considering several solutions to various problems, building on the previous lab on tail recursion
The first solution seems to follow a nowfamiliar huskandkernel form for tail recursion:
(define sum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the sum of the numbers in ls (sumkernel ls 0) ) ) (define sumkernel (lambda (ls runningsum) ;Precondition: ls is a list of numbers ;Postcondition: returns the sum of the numbers in ls (if (null? ls) runningsum (sumkernel (cdr ls) (+ runningsum (car ls))) ) ) )
In this approach, recursion proceeds from (1 2 3 4) toward the null list (), carrying along a running sum of values already encountered.
While Solution 1 was quite efficient, it requires a separate procedure sumkernel. 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 sumkernel procedure within sum. This may be done with a letrec statement:
(define sum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the sum of the numbers in ls (letrec ((sumkernel (lambda (ls runningsum) (if (null? ls) runningsum (sumkernel (cdr ls) (+ runningsum (car ls))) ) ) )) (sumkernel ls 0) ) ) )
Solution 2 is tail recursive and contains sumkernel as a local procedure. Thus, it runs efficiently and avoids possible name conflicts with procedures defined elsewhere. Solution 2, 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 (sumkernel 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) ;Precondition: ls is a list of numbers ;Postcondition: returns the sum of the numbers in ls (let sumkernel ((ls lst) (runningsum 0)) (if (null? ls) runningsum (sumkernel (cdr ls) (+ runningsum (car ls))) ) ) ) )
Here, the local procedure name (sumkernel) follows immediately after let, and its parameters (ls and runningsum) are identified and initialized next. The body of the procedure follows. In such named let expressions, it is understood that the local procedure will be called with the given initial parameters, so there is no need to write out the call separately.
Run this procedure with several lists of numbers to check that it works correctly.
In this code, sum has a formal parameter lst, and this in turn is used to initialize the formal parameter ls of sumkernel.
In the next example, tail recursion is particularly helpful.
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.
Four solutions follow:
(define maximum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the maximum value on ls (cond ((null? (cdr ls)) (car ls)) ((< (car ls) (maximum (cdr ls))) (maximum (cdr ls))) (else (car ls)) ) ) )
(define maximum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the maximum value on ls (if (null? (cdr ls)) (car ls) (let ((maxinrest (maximum (cdr ls))) (firstitem (car ls))) (if (< firstitem maxinrest) maxinrest firstitem ) ) ) ) )
(define maximum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the maximum value on ls (maxkernel (car ls) (cdr ls)) ) ) (define maxkernel (lambda (maxsofar lst) ;Precondition: lst is a list of numbers ;Postcondition: returns the maximum value on lst (cond ((null? lst) maxsofar) ((< (car lst) maxsofar) (maxkernel maxsofar (cdr lst))) (else (maxkernel (car lst) (cdr lst))) ) ) )
(define maximum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns the maximum value on ls (letrec ((maxkernel (lambda (maxsofar lst) (cond ((null? lst) maxsofar) ((< (car lst) maxsofar) (maxkernel maxsofar (cdr lst))) (else (maxkernel (car lst) (cdr lst))) ) ) )) (maxkernel (car ls) (cdr ls)) ) ) )
Which of these solutions are tail recursive? Briefly explain your conclusions.
Modify solution 4, so that maxkernel is defined locally with a named let expression.
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.
Solution: Our procedure includes a named let expression to allow recursive processing of the list elements. As the problem asks us to keep track of three different results, we use a parameter for each in addition to the list parameter.
(define oddnegsum (lambda (ls) ;Precondition: ls is a list of numbers ;Postcondition: returns a triple: (# odds, #negatives, sum) for ls (let loop ((rest ls) ;; 4 parameters: numbers to process (numberodd 0) ;; running counts of odd integers (numberneg 0) ;; and number of negatives (sum 0)) ;; running sum (if (null? rest) (list numberodd numberneg sum) ;; base case returns result (loop (cdr rest) ;; for recursion, update list (if (odd? (car rest));; determine correct count (+ 1 numberodd) ;; of odd numbers numberodd) (if (< (car rest) 0) ;; provide correct count (+ 1 numberneg) ;; of negative numbers numberneg) (+ (car rest) sum) ;; update running sum ) ) ) ) )
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.
Write a tail recursive solution to this problem, using a single local kernel procedure defined within a named let expression. The local procedure may have as many parameters as desired.
Hint: In binding local variables within a let expression, one variable may be bound to the result of an if expression.
Define a procedure index that has two arguments, an item a and a list of items ls, and returns the index of a in ls, that is, the zerobased location of a in ls. If the item is not in the list, the procedure returns 1. Test your procedure on:
(index 3 '(1 2 3 4 5 6)) ===> 2 (index 'so '(do re mi fa so la ti do)) ===> 4 (index 'a '(b c d e)) ===> 1 (index 'cat '()) ===> 1
Any loop in this procedure should be coded using a named let expression.
The reading on strings contained huskandkernel procedures numbervowels and countvowelsbyposition, which count the number of vowels in a string. Rewrite number, so that countvowelsbyposition is defined locally inside:
The reading on input and output included procedures to compute a table of numbers and their square roots. Rewrite sqrttable so that all helper procedures are declared locally:
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/153.sp07/labs/labnamedlet.shtml
created 7 March 1997 last revised 24 February 2007 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 