&list-recursion-prefix; List Recursion
Due: &list-recursion-due;
Summary: You will apply the basic and
helper recursion patterns to a short series of problems.
Purposes: To practice writing a variety of procedures that
perform recursion over lists.
Expected Time:
Two to three hours.
Collaboration:
We encourage you to work in groups of size three. You may, however,
work alone or work in a group of size two or size four. You may discuss
this assignment with anyone, provided you credit such discussions when
you submit the assignment.
Submitting:
Email your answer to &grader-email;. The title of your email
should have the form &list-recursion-subject; and
should contain your answers to all parts of the assignment. Scheme code
should be in the body of the message.
Warning:
So that this assignment is a learning experience for everyone, we may
spend class time publicly critiquing your work.
Preliminaries
You may wish to review the summary
of recursion
patterns.
Assignment
Problem 1: Closest to Zero
a. Write a procedure, (closest-to-zero
values), that, given a list of real
numbers (including both positive and negative numbers), returns the
value closest to zero in the list. Your solution should use basic recursion.
Hint: Think about how, given two numbers, you
determine which is closer to zero.
b. Write a second version of closest-to-zero
that uses helper recursion. That is, you should have an additional
helper procedure that takes closest-so-far
and remaining as parameters.
c. Explain which version of closest-to-zero you
prefer and why.
Problem 2: A Safer Sum
Write and document a procedure
(safe-sum values)
that, given a list of values as a parameter, computes the sum of the
numeric values in the list. That is, safe-sum
should ignore all non-numeric values.
> (safe-sum (list 1 2 3))
6
> (safe-sum (list 3 'a 'b 5))
8
> (safe-sum (list 'a 'b))
0
Problem 3: Averaging Colors
We've seen how to average two colors using
rgb-blend from the assignment on Exploring Colors
and a list of colors in the lab on Recursion Basics. But
what if we want to do something different: Given a list of colors,
we want averages, but only of neighboring elements in the list.
Write a procedure, (rgb-averages
colors), that, given a list of colors,
computes a new list of colors, by averaging subsequent pairs of
colors. For example, if the input list is the standard seven
rainbow colors (red, orange, yellow, green, blue, indigo, and violet),
the output list will consist of
a red-orange average, an orange-yellow average, a yellow-green
average, a green-blue average, a blue-indigo average, and an
indigo-violet average.
The length of the resulting list will be one less than the length of the
input list.
Important Evaluation Criteria
Students who provide correct procedures for each question will earn a
&hw-grade-check;.
Students who provide oddly formatted or inelegant solutions to the
problems may be publicly critiqued for their odd formatting and
inelegance, and will also receive a &hw-grade-minus;.
Students who provide particularly elegant formatting or strategies
will earn a &hw-grade-plus;.