Fundamentals of Computer Science I (CS151.02 2007S)

Homework 7: Recursive Procedures

This homework assignment is also available in PDF.

Assigned: Friday, February 9, 2007
Due: Friday, February 16, 2007
No extensions!

Summary: In this assignment, you will practice writing recursive procedures for a variety of tasks.

Purposes: To give you experience writing recursive procedures. To provide you with some feedback on the expected style of writing procedures.

Expected Time: One hour.

Collaboration: You may work in a group of any size between one and four, inclusive. You may consult others outside your group, provided you cite those others. You need only submit one assignment per group.

Submitting: Email me your work, using a subject of CSC151 Homework 7.

Warning: So that this exercise is a learning assignment for everyone, I may spend class time publicly critiquing your work.

Assignment

The focus of this assignment is a series of small problems, rather than a larger problem. Write procedures to solve each of the problems below. Since this is an exercise in writing procedures, and not in documenting procedures, you are not required to write documentation (although you may earn extra credit for writing good documentation).

Problem 1: A Better Sum

Write a procedure, (sum values), that, given a list of values as a parameter, computes the same of all numeric values in the list. Your sum procedure should ignore all non-numeric values.

For example,

> (sum (list 1 2 3))
6
> (sum (list 3 'a 'b 5))
8
> (sum (list 'a 'b))
0

Problem 2: Reversing Lists

Suppose the reverse procedure were not included in Scheme. Could you write it yourself? Certainly! It should be possible to implement reverse recursively.

One strategy is to use the standard recursive formulation of

(define my-reverse
  (lambda (lst)
     (if (null? lst)
         base-case
         (combine (car lst) (my-reverse (cdr lst))))))

Finish this implementation.

When using this strategy, you'll need to think about the question Suppose I've reversed the cdr of a list. What do I do with the car to get the reversal of the complete list?

Problem 3: Reversing Lists, Revisited

Of course, you've seen more than one strategy for writing recursive procedures. Another possibility is to use a helper that includes a what I've done so far parameter. For example,

(define my-other-reverse
  (lambda (lst)
     (my-other-reverse-helper null lst)))

(define my-other-reverse-helper
  (lambda (reversed-so-far remaining-elements)
    (if (null? remaining-elements)
        base-case
        (my-other-reverse-helper (modify reversed-so-far)
                                 (cdr remaining-elements)))))

Finish this implementation.

Problem 4: Finding Skips

Write a procedure, (find-first-skip lst) that takes a list of symbols as a parameter and returns the index of the first instance of skip in lst, if skip appears in lst. If skip does not appear in lst, find-first-skip should return #f.

> (find-first-skip (list 'hop 'skip 'and 'jump))
1
> (find-first-skip (list 'skip 'hop 'jump 'skip 'and 'skip 'again))
0
> (find-first-skip (list 'hop 'to 'work 'jump 'to 'school 'but 'never 'skip 'class))
8
> (find-first-skip (list 'hop 'and 'jump))
#f

Problem 5: Finding Arbitrary Values

Write a procedure, (index-of sym lst) that takes a symbol and a list of symbols as a parameter and returns the index of the first instance of sym in lst, if the symbol appears in lst. If the symbol does not appear in lst, index-of should return #f.

> (index-of 'skip (list 'hop 'skip 'and 'jump))
1
> (index-of 'skippy (list 'hop 'skip 'and 'jump))
#f
> (index-of 'hop (list 'hop 'skip 'and 'jump))
0
> (index-of 'jump (list 'hop 'skip 'and 'jump))
3
> (index-of 'jump (list))
#f

Important Evaluation Criteria

Students who provide correct procedures for each question 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 grade penalty.

Students who provide particularly elegant formatting or strategies will earn a higher grade.

 

History

Thursday, 14 September 2006 [Samuel A. Rebelsky]

Thursday, 8 February 2007 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Thu Sep 13 20:54:09 2007.
The source to the document was last modified on Tue Feb 13 07:33:07 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Homework/hw.07.html.

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

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