Using natural-number recursion, develop a Scheme procedure named
power-of-two that takes a natural number as its argument and
returns the result of raising 2 to the power of that number. Here's one
sample call to get you started: the value of (power-of-two 3)
should be 23, or 8.
It's possible to define this procedure non-recursively, using Scheme's
primitive expt procedure, but the point of the exercise is to
use recursion.
Develop a Scheme procedure named list-fill 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.
Develop a Scheme procedure named count-down 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:
> (count-down 5) (5 4 3 2 1 0)
Using count-from, define and test a 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.)
Here is a procedure that computes the product of all of the odd natural
numbers up to and including number:
;;; odd-factorial: compute the product of odd natural
;;; numbers up to and including a given natural number
;;; John David Stone
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; stone@cs.grinnell.edu
;;; created February 3, 2000
;;; last revised August 8, 2001
;;; Given:
;;; NUMBER, an integer
;;; Result:
;;; PRODUCT, an integer
;;; Preconditions:
;;; (This is the exercise.)
;;; Postcondition:
;;; PRODUCT is the product of the odd natural numbers
;;; up to and including NUMBER.
(define odd-factorial
(lambda (number)
(if (= number 1)
1
(* number (odd-factorial (- number 2))))))
What preconditions does odd-factorial impose on its argument?
What will happen if these preconditions are not met?
Write a summary of what actually happens during the evaluation of
(odd-factorial 9). Use the summary of (termial
5) in the reading as a model.
Here is the definition of a procedure that computes the number of digits
in
the decimal representation of number:
;;; number-of-decimal-digits: compute the number of
;;; digits in the decimal representation of a given number
;;; John David Stone
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; stone@cs.grinnell.edu
;;; created February 3, 2000
;;; last revised August 8, 2001
;;; Given:
;;; NUMBER, an integer
;;; Result:
;;; LENGTH, an integer
;;; Preconditions:
;;; (This is part of the exercise.)
;;; Postcondition:
;;; LENGTH is the number of digits in the decimal
;;; representation of NUMBER.
(define number-of-decimal-digits
(lambda (number)
(if (< number 10)
1
(+ (number-of-decimal-digits (quotient number 10)) 1))))
Write out some sample calls to this procedure and test them to confirm
that
you get the expected answer. What preconditions does
number-of-decimal-digits impose on its argument?
The definition of number-of-decimal-digits uses direct
recursion. Describe the base case of this recursion. Identify and
describe the way in which a simpler instance of the problem is created for
the recursive call. Write a summary of what actually happens during the evaluation of
(number-of-decimal-digits 65536). Use the summary of
(termial 5) in the reading as a model.
Define and test a procedure digit-of? that takes two
arguments,
digit and num, and returns #t if
digit is a digit of the number num, and
#f otherwise. For example:
> (digit-of? 5 7523) #t > (digit-of? 5 999338) #f
Develop a Scheme procedure wrap that takes any value as
its first argument and any natural number n as its second argument,
and ``wraps'' the value as a singleton list n times. Here are some
sample calls:
> (wrap 'contents 5) (((((contents))))) > (wrap #t 1) (#t) > (wrap (list 'alpha 'beta) 2) (((alpha beta))) > (wrap 'unwrapped 0) unwrapped
A kid's game called buzz is played as follows: the kids agree on a digit and start counting, but say buzz instead of any number that either contains or is divisible by the preagreed upon digit. For example, suppose the digit is 3, then count would go like: 1, 2, buzz, 4, 5, buzz, 7, 8, buzz, 10, 11, buzz, buzz, ....
You are to write a function buzz, which take in a digit
digit,
a number start, and another number finish, and
then
returns a list of the numbers from start to
finish, replacing every number
which contains or is divisible by digit, by the word
buzz. For example:
> (buzz? 5 47 53) (47 48 49 buzz buzz buzz buzz) > (buzz? 3 25 16) (25 buzz buzz 22 buzz 20 19 buzz 17 16)
You may want to have your buzz procedure call
digit-of?.
Develop a procedure named runs that takes a natural
number n as argument and returns a list of Boolean values
consisting
of n runs, where a run consists of zero or more occurrences of
#f followed by one occurrence of #t. The first
of the n runs should be of length 1, the second of length 2, and so
on. Here is a sample call:
> (runs 5)
(#t #f #t #f #f #t #f #f #f #t #f #f #f #f
#t)
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~gum/courses/151/labs/recursion-with-integers.xhtml
created February 3, 2000
last revised June 20, 2003
John David Stone
(stone@cs.grinnell.edu)
Ben Gum
(gum@cs.grinnell.edu)