Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This lab is also available in PDF.
Summary: In this laboratory, you will begin your experimnets with recursion by (1) considering some predefined recursive procedures and (2) designing your own recursive procedures.
Contents:
a. Reflect on the key aspects of recursion.
b. Start DrScheme.
Here are the two versions of sum
as given in
the reading on recursion.
;;; Procedures: ;;; sum ;;; newsum ;;; Parameters: ;;; numbers, a list of numbers. ;;; Purpose: ;;; Find the sum of the elements of a given list of numbers ;;; Produces: ;;; total, a number. ;;; Preconditions: ;;; All the elements of numbers must be numbers. ;;; Postcondition: ;;; total is the result of adding together all of the elements of ls. ;;; If all the values in numbers are exact, total is exact. ;;; If any values in numbers are inexact, total is inexact. (define sum (lambda (numbers) (if (null? numbers) 0 (+ (car numbers) (sum (cdr numbers)))))) (define newsum (lambda (numbers) (newsumhelper 0 numbers))) ;;; Procedure: ;;; newsumhelper ;;; Parameters: ;;; sumsofar, a number. ;;; remaining, a list of numbers. ;;; Purpose: ;;; Add sumsofar to the sum of the elements of a given list of numbers ;;; Produces: ;;; total, a number. ;;; Preconditions: ;;; All the elements of remaining must be numbers. ;;; sumsofar must be a number. ;;; Postcondition: ;;; total is the result of adding together sumsofar and all of the ;;; elements of remaining. ;;; If both sumsofar and all the values in remaining are exact, ;;; total is exact. ;;; If either sumsofar or any values in remaining are inexact, ;;; total is inexact. (define newsumhelper (lambda (sumsofar remaining) (if (null? remaining) sumsofar (newsumhelper (+ sumsofar (car remaining)) (cdr remaining)))))
a. Verify experimentally that they work.
b. Which version do you prefer? Why?
c. Here's an alternative definition of newsum
.
(define newersum (lambda (numbers) (newsumhelper (car numbers) (cdr numbers))))
Is it superior or inferior to the previous definition? Why?
Here is a variant of the largestoflist
procedure given in
the reading on recursion.
;;; Procedures: ;;; largestoflist ;;; Parameters: ;;; numbers, a list of real numbers. ;;; Purpose: ;;; Find the largest element of a given list of real numbers ;;; Produces: ;;; largest, a real number. ;;; Preconditions: ;;; numbers is not empty. ;;; All the values in numbers are real numbers. That is, numbers ;;; contains only numbers, and none of those numbers are complex. ;;; Postconditions: ;;; largest is an element of numbers (and, by implication, is real). ;;; largest is greater than or equal to every element of numbers. (define largestoflist (lambda (numbers) ; If the list has only one element (if (null? (cdr numbers)) ; Use that one element (car numbers) ; Otherwise, take the greater of ; (a) the first element of the list ; (b) the largest remaining element (max (car numbers) (largestoflist (cdr numbers))))))
a. Experiment with this procedure to verify that it works as advertised.
b. There's at least one kind of list for which this procedure fails to work. Can you tell what kind? Why might we have made that decision?
Here's another version of largestoflist
, one that
uses a technique like newsum
(that is, it has a helper
that adds an extra parameter to keep track of the largest value so far).
(define newlargestoflist (lambda (numbers) (newlargestoflisthelper (car numbers) (cdr numbers)))) (define newlargestoflisthelper (lambda (largestsofar remainingnumbers) ; If no elements remain ... (if (null? remainingnumbers) ; Use the largest value seen so far largestsofar ; Otherwise, update the guess using the next element ; and continue (newlargestoflisthelper (max largestsofar (car remainingnumbers)) (cdr remainingnumbers)))))
a. Verify experimentally that this procedure works correctly.
b. You may find it helpful to watch
newlargestoflisthelp
.
Add the following two lines to newlargestoflisthelper
(right before the outermost if
).
(display (list 'newlargestoflisthelper remainingnumbers largestsofar)) (newline)
What effect does this change have?
c. How is newlargestoflist
procedure similar to largestoflist
? How is it different?
d. Which of the two versions do you prefer? Why?
As some students note, there is no need for us to define any of the
variants of largestoflist
, since Scheme gives us
an equivalent procedure in max
. While that complaint has
a glimmer of validity, it is, in fact, useful to figure out how to write
many of the builtin Scheme procedures. One reason is that learning
the structure of those procedures makes it easier to write other, similar,
procedures.
Consider the problem of finding one of the longest strings in a list
of strings, a string whose length is at least as large as the length
of each of the other strings. (We say one of the longest strings
because more than one string may have the same length.) For example,
> (longeststringinlist (list "zebras" "have" "stripes") "stripes" > (longeststringinlist (list "stripes" "often" "appear" "on" "zebras") "stripes" > (longeststringinlist (list "this" "is" "a" "silly" "list")) "silly" > (longeststringinlist (list "this" "is" "a" "long" "list")) "this" ; or "list", or although it seems unlikely, "long"
We might document this procedure as follows:
;;; Procedure: ;;; longeststringinlist ;;; Parameters: ;;; los, a nonempty list of strings ;;; Purpose: ;;; Finds a longest string in los. ;;; Produces: ;;; longest, a string ;;; Preconditions: ;;; (none) ;;; Postconditions: ;;; For any string in los, (stringlength longest) >= (stringlength str) . ;;; longest is an element of los. ;;; Problems: ;;; los may contain many strings that meet the postconditions. We do ;;; not specify which of those strings is returned.
Using either of the patterns of largestoflist
, write
longeststringinlist
.
Note that you might find it helpful to
write a (longeroftwo str1 str2)
procedure that finds the
larger of two strings.
Define and test a Scheme procedure, (product values)
,
that takes a list of numbers as its argument and returns the result of
multiplying them all together. For example,
> (product (list 3 5 8)) 120 > (product (list 1 2 3 4 5 0)) 0
Warning: (product null)
should not be 0. It should be the identity for multiplication,
just as (sum null)
is the identity for addition.
Explain why.
Write a Scheme procedure, (closesttozero values)
,
that, given a list of numbers (including both negative and positive
numbers), returns the value closest to zero in the list.
Hint: Think about how, given two numbers, you determine which is closer to zero.
Hint: Think about how this problem is similar to a problem or problems we've solved before.
Define and test a Scheme procedure, (squareeachelement
values)
, that takes a list of numbers as its argument and
returns a list of their squares.
> (squareeachelement (list 7 3 12 0 4/5))
(49 9 144 0 16/25)
Hint: For the base case, consider what the procedure should return when given a null list; for the other case, separate the car and the cdr of the given list and consider how to operate on them so as to construct the desired result.
If you find that you not only finish this laboratory early, but also finish the two extra exercises early, you can start your next homework assignment.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/recursion.html
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
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:28 2007.
The source to the document was last modified on Thu Feb 8 22:08:31 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/recursion.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.