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 ;;; 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?
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 ;;; 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. Explain how the procedure correctly determines that
the decimal numeral for the number 2000 contains four digits.
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
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 (including the final #t), 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)