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: Although most of our prior experiments with recursion have emphasized recursion over lists, it is also possible to use other values as the basis of recursion. In this laboratory, you will explore the use of natural numbers (nonnegative integers) as the basis of recursion.
Contents:
a. Make sure you understand the initial reading on recursion and the followup reading on numeric recursion.
b. Start DrScheme.
Using recursion over natural numbers, define and test a recursive Scheme
procedure, (poweroftwo power)
that takes a natural
number (an integer greater than or equal to 0)
as its argument and returns the result of raising 2 to the power of
that number. For example,
> (poweroftwo 3) 8 > (poweroftwo 10) 1024 > (poweroftwo 1) 2
It is possible to implement this procedure nonrecursively, using Scheme's
primitive expt
procedure, but the point of the exercise is to
use recursion.
Define and test a Scheme procedure, (countdown
val)
, that takes a natural number as argument and returns
a list of all the natural numbers less than or equal to that number, in
descending order:
> (countdown 5) (5 4 3 2 1 0) > (countdown 0) (0)
Note that you should use cons
to build up the list.
Note also that you are better off writing this with direct recursion, rather than using a helper procedure.
When you are finished writing this procedure, add countdown
to your Scheme library.
Define and test a Scheme procedure, (filllist value
count)
, that takes two arguments, the second of which is a
natural number, and returns a list consisting of the specified number of
repetitions of the first argument:
> (filllist 'sample 5) (sample sample sample sample sample) > (filllist (list 'left 'right) 3) ((left right) (left right) (left right)) > (filllist null 1) (()) > (filllist null 2) (() ()) > (filllist 'hello 0) ()
Again, even if you know a builtin procedure to do this task, please implement it recursively.
When you are finished writing this procedure, add filllist
to your Scheme library.
Define and test a recursive Scheme procedure that takes a natural number
as argument and returns a list of all the natural numbers that are
strictly less than the argument, in ascending order. (The traditional
name for this procedure is iota
, a Greek letter.)
For example,
> (iota 3) (0 1 2) > (iota 5) (0 1 2 3 4) > (iota 1) (0)
Note that you will probably need to use a helper of some sort to write
iota
. You might use the traditional form of helper, which
adds an extra parameter. You might also use a helper that simply
computes iota in the reverse order. (Most students write
a backwards iota in the first attempt; instead of throwing it away,
rename it and call it from iota.)
When you are done, add iota
to your library.
You may recall the countfrom
procedure from
the reading on recursion
over natural numbers. That procedure is also reproduced
at the end of this lab.
What is the value of the call (countfrom 10 10)
?
a. Write down what you think that it should be.
b. Copy the definition of
countfrom
into DrScheme and use it to find out what the call
actually returns.
When you are finished writing this procedure, add countfrom
to your Scheme library.
Using countfrom
as a helper, define and test a Scheme
procedure, (newiota n)
, that takes a natural
number as argument and returns a list of all the natural numbers that
are strictly less than the argument, in ascending order. Note that your
procedure must use countfrom
as a helper.
For example,
> (newiota 3) (0 1 2) > (newiota 5) (0 1 2 3 4) > (newiota 1) (0)
Here is the definition of a procedure that computes the number of digits in
the decimal representation of number
:
(define numberofdecimaldigits (lambda (number) (if (< number 10) 1 (+ (numberofdecimaldigits (quotient number 10)) 1))))
a. Test this procedure.
The definition of numberofdecimaldigits
uses direct
recursion.
b. Describe the base case of this recursion.
c. Identify and describe the way in which a simpler instance of the problem is created for the recursive call. That is, explain what problem is solved recursively and why you know that that problem is simpler.
d. Explain how the procedure correctly determines that the decimal numeral for the number 2000 contains four digits.
e. What preconditions does numberofdecimaldigits
impose
on its argument?
When you are finished writing this procedure, add numberofdecimaldigits
to your Scheme library.
Develop a Scheme procedure, (nest val depth)
that takes any value, val as its first argument and any natural number, depth, as its second argument, and nests
val in depth lists.
To nest
a value in a list, simply place it in a singleton list. To nest it in two lists, place that first list in a singleton list. To nest in three lists, place that depthtwo list in a singleton list. And so on and so forth.
For example,
> (nest 'contents 5) (((((contents))))) > (nest #t 1) (#t) > (nest (list 'alpha 'beta) 2) (((alpha beta))) > (nest 'alpha 2) ((alpha)) > (nest 'notnested 0) notnested
Rewrite poweroftwo
to permit negative exponents.
digitof?
Define and test a procedure,
(digitof? digit num)
, returns
#t
if digit is a digit of the natural number num,
and #f
otherwise. For example,
> (digitof? 5 7523) #t > (digitof? 5 999338) #f
Note that you may want to use numberofdecimaldigits as a pattern for writing this procedure.
Write a procedure, (sumofdigits num)
, that
sums the digits in natural number num.
Note that you may want to use numberofdecimaldigits as a pattern for writing this procedure.
countfrom
For those of you unable to find the reading on recursion over
natural numbers and for completeness, here is the countfrom
procedure.
;;; Procedure: ;;; countfrom ;;; Parameters: ;;; lower, a natural number ;;; upper, a natural number ;;; Purpose: ;;; Construct a list of the natural numbers from lower to upper, ;;; inclusive, in ascending order. ;;; Produces: ;;; ls, a list ;;; Preconditions: ;;; lower <= upper ;;; Both lower and upper are numbers, exact, integers, and nonnegative. ;;; Postconditions: ;;; The length of ls is upper  lower + 1. ;;; Every natural number between lower and upper, inclusive, appears ;;; in the list. ;;; Every value in the list with a successor is smaller than its ;;; successor. ;;; For every natural number k less than or equal to the length of ;;; ls, the element in position k of ls is lower + k. (define countfrom (lambda (lower upper) (if (= lower upper) (list upper) (cons lower (countfrom (+ lower 1) upper)))))
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/numericrecursion.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:25 2007.
The source to the document was last modified on Tue Feb 13 10:35:06 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/numericrecursion.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.