Functional Problem Solving (CSC 151 2013F) : Assignments
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
Due: 10:30 p.m, Wednesday, 30 October 2013
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.
Collaboration: You may work alone or with 1-2 partners of your choice. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.
Wrapper (Prologue): Individually read through this assignment and make sure that you understand what is required. Then use the form available at http://bit.ly/151hw7pro to indicate (a) how long you think this assignment will take and (b) what you think will be the most challenging aspect of this assignment.
Wrapper (Epilogue): When you are done with the assignment, fill out the form available at http://bit.ly/151hw7epi to indicate (a) how long the assignment took, (b) what the most challenging part of the assignment was, and (c) something important you learned from doing the assignment. If you find that the assignment took much less or much more time than you expected, also include (d) a note as to what might have led to that difference.
Submitting:
Email your answer to <grader-151-02@cs.grinnell.edu>. The subject of your email
should have the form CSC 151.02 Assignment 7: List Recursion 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.
You may wish to review the summary of recursion patterns.
Your procedures need not verify their preconditions, except as directed in your solution to Problem 4, part c.
Write and document a procedure
(
that, given a list of values as a parameter, computes the sum of the
numeric values in the list. That is, safe-sum values)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
a. Write a procedure, (, 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.
closest-to-zero
values)
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.
Averaging two colors is a fairly simple task: simply average each of the respective red, green, and blue components, as in the following procedure.
;;; Procedure:
;;; rgb-average
;;; Parameters:
;;; rgb1, an RGB color
;;; rgb2, an RGB color
;;; Purpose:
;;; Compute the "average" of two RGB colors.
;;; Produces:
;;; rgb-avg, an RGB color
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; (rgb-red rgb-avg) is the average of (rgb-red rgb1) and (rgb-red rgb2)
;;; (rgb-green rgb-avg) is the average of (rgb-green rgb1) and (rgb-green rgb2)
;;; (rgb-blue rgb-avg) is the average of (rgb-blue rgb1) and (rgb-blue rgb2)
(define rgb-average
(lambda (rgb1 rgb2)
(rgb-new (quotient (+ (rgb-red rgb1) (rgb-red rgb2)) 2)
(quotient (+ (rgb-green rgb1) (rgb-green rgb2)) 2)
(quotient (+ (rgb-blue rgb1) (rgb-blue rgb2)) 2))))
We also saw how to average 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, (, 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.
rgb-averages
colors)
The length of the resulting list will be one less than the length of the input list.
a. Using the 6 P's, document the reverse procedure.
b. Suppose the reverse procedure were not included in Scheme. Could you write it yourself? Certainly! It should be possible to implement reverse recursively.
Using the helper recursion pattern, implement your own version of
reverse called my-reverse.
c. Modify my-reverse so that it verifies its
preconditions.
Write and document a function (
that produces a new list containing alternating elements from the lists
riffle
first second)first and second.
If one list runs out before the other, then the remaining elements
should appear at the end of the new list.
>(riffle (list 'a 'b 'c) (list 'x 'y 'z))(a x b y c z)>(riffle (list 'a 'b 'c) (iota 10))(a 0 b 1 c 2 3 4 5 6 7 8 9)
Define and test a Scheme procedure, (, that takes a list as
argument and returns a list of two lists, one comprising the elements in
even-numbered positions in the given list, the other comprising the
elements in odd-numbered positions.
unriffle
lst)
>(unriffle (list 'a 'b 'c 'd 'e 'f 'g 'h 'i))((a c e g i) (b d f h))>(unriffle (list))(() ())>(unriffle (list 'a))((a) ())>(unriffle (list 'b))((b) ())>(unriffle (list 'a 'b))((a) (b))
Hint: There are many ways to solve this problem. Before writing code, try solving it by hand to develop your algorithm.
Students who provide correct procedures for each required problem will earn a 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 check-minus or minus.
Students who provide particularly elegant formatting or strategies will earn a check-plus or plus.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
Samuel A. Rebelsky, rebelsky@grinnell.edu
Copyright (c) 2007-2013 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.