CSC 153  Grinnell College  Spring, 2005 
Computer Science Fundamentals  
Laboratory Exercise  
This laboratory exercise provides experience using recursion to solve a variety of types of problem.
With the help of recursion, we can perform many common operation on lists: filtering. One might, for example, write a procedure that takes any list of real numbers and returns a similar list, but with all the negative numbers removed; such a procedure would ``filter out'' the negative numbers. Here's how it would look:
(define filteroutnegatives (lambda (ls) (cond ((null? ls) '()) ((negative? (car ls)) (filteroutnegatives (cdr ls))) (else (cons (car ls) (filteroutnegatives (cdr ls)))))))
In English: If the list ls
is empty, return the empty
list. Otherwise, if the first element on the list is negative, discard it
and proceed to apply the filter to the rest of the list. But if the first
element on the list is zero or positive, add it to the front of the list
obtained by applying the filter to the rest of the list.
Start Scheme and enter the definition of filteroutnegatives
.
Confirm that it correctly filters out negatives from any list of numbers.
What happens if all of the elements of the list are negative? What happens
if ls
contains symbols instead of numbers?
Write and test a Scheme procedure filteroutliers
that takes a
list of real numbers and filters out those that are not in the range from 0
to 100.
(filteroutliers '(93 86 92 3 100 81 77 84)) ===> (93 86 92 100 81 77 84) (filteroutliers '(50 0 50 100 150)) ===> (0 50 100) (filteroutliers '(1000000)) ===> () (filteroutliers '()) ===> ()
Recursion can be used simply to run through a series of numerical values, so that each of those values can be operated on by a fixed procedure. For instance, the following procedure computes the sum of the squares of the positive integers from 1 to n, for any nonnegative integer n:
(define sumofsquares (lambda (n) (if (zero? n) 0 (+ (* n n) (sumofsquares ( n 1))))))
Adapt this procedure so that squares of odd numbers are excluded from the sum.
In other cases, the numerical parameter is simply a counter, tallying the
number of repetitions of some operation that may not involve numbers at
all. For instance, we might want procedure dupl
that takes
two arguments, a string str
and a nonnegative integer
factor
, and returns a string consisting of factor
successive copies of str
:
(dupl "alf" 4) ===> "alfalfalfalf" (dupl "What? " 5) ===> "What? What? What? What? What? " (dupl "*" 10) ===> "**********" (dupl "" 153) ===> "" (dupl "invisible" 0) ===> ""
Here's how we'd write it:
(define dupl (lambda (str factor) (if (zero? factor) "" (stringappend str (dupl str ( factor 1))))))
The stringappend
operation itself deals only with strings,
not with numbers; the role of factor
is simply to count off
the number of repetitions remaining and to cut off the recursion once this
number decreases to zero.
Write and test a procedure dupltofit
, similar to
dupl
, except that the second parameter indicates the maximum
length permitted for the result string  str
should be
duplicated as many times as possible without exceeding this maximum length.
(In other words, the recursion should terminate as soon as the amount of
``free space'' left for the result string is less than the length of
str
.)
(dupltofit "alf" 11) ===> "alfalfalf" (dupltofit "alf" 12) ===> "alfalfalfalf" (dupltofit "*" 7) ===> "*******" (dupltofit "*" 7) ===> "***" (dupltofit "invisible" 3) ===> ""
Be careful not to use the null string as the first argument to this procedure!
Suppose we are given a continuous function f, and we want to approximate a value r where f(r)=0. While this can be a difficult problem in general, suppose that we can guess two points a and b (perhaps from a graph) where f(a) and f(b) have opposite signs. The four possible cases are shown below:
Given a and b, we know that a root r must lie between them, since f(a) and f(b) have opposite signs. Thus, we know that r must lie in the interval [a, b]. In one step, we can cut this interval in half as follows. If f(a) and f(m) have opposite signs, then r must lie in the interval [a, m]; otherwise, r must lie in the interval [m, b].
As with the discussion of the cuberoot program in Section 2.2, once one understands how to halve the possible interval for r in a step, then one can apply this process recursively to halve the interval as much as one wants from an initial interval [a, b].
To apply this approach in an actual program, a few additional details are
needed. First, one must decide how to specify the function f. For
now, this could be accomplished by writing a separate definition, such as
(define f ...)
. For example, one might use
(define f (lambda (x) ( (* x x x) 2)))
Third, one must decide what values to use for a and b. It may be best to let the user enter these values as parameters.
Fourth, one must consider how to decide if two real numbers (u and v) have the opposite sign. One approach is to use cases  just as in the above figure. A second approach is to note that the numbers have the opposite sign if their product is negative.
Apply your procedure to find the cube root of 2.
Change f to find the cube root of 8, which should be 2. Does your procedure root work in this case? If not, make any needed corrections.
Then, change f to sin(x) and find the root between 2 and 4. (Since the actual root is Pi, this will give you an approximation to that value.)
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/labrecursivedesign.shtml
created February 5, 1997 by John David Stone completely revised January 7, 2000 by Henry M. Walker last revised January 26, 2005 

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