Fundamentals of Computer Science I (CS151 2003F)

Repetition Through Recursion

Summary: In this laboratory, you will begin your experimentations with recursion by (1) considering some pre-defined recursive procedures and (2) designing your own recursive procedures.

Contents:

Exercises

Exercise 0: Preparation

a. Reflect on the key aspects of recursion.

b. Start DrScheme.

Exercise 1: Sum

Here are the two versions of sum as given in the reading on recursion.

;;; Procedure:
;;;   sum
;;;   new-sum
;;; 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.
;;;   total is a number.
;;;   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 new-sum
  (lambda (numbers)
    (new-sum-helper 0 numbers)))

;;; Procedure:
;;;   new-sum-helper
;;; Parameters:
;;;   sum-so-far, a number.
;;;   remaining, a list of numbers.
;;; Purpose:
;;;   Add sum-so-far 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.
;;;   sum-so-far must be a number.
;;; Postcondition:
;;;   total is the result of adding together sum-so-far and all of the 
;;;     elements of remaining.
;;;   total is a number.
;;;   If both sum-so-far and all the values in remaining are exact, 
;;;     total is exact.
;;;   If either sum-so-far or any values in remaining are inexact, 
;;;     total is inexact.
(define new-sum-helper
  (lambda (sum-so-far remaining)
     (if (null? remaining)
         sum-so-far
         (new-sum-helper (+ sum-so-far (car remaining))
                         (cdr remaining)))))

a. Verify experimentally that they work.

b. Which version do you prefer? Why?

c. Here's an alternative definition of new-sum.

(define newer-sum
  (lambda (numbers)
    (new-sum-helper (car numbers) (cdr numbers))))

Is it superior or inferior to the previous definition? Why?

Exercise 2: The Largest Element in a List

Here is a variant of the greatest-of-list procedure givin in the reading on recursion.

;;; Procedures:
;;;   greatest-of-list
;;; Parameters:
;;;   numbers, a list of real numbers.
;;; Purpose:
;;;   Find the greatest element of a given list of real numbers
;;; Produces:
;;;   greatest, 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:
;;;   greatest is an element of numbers (and, by implication, is real).
;;;   greatest is greater than or equal to every element of numbers.
(define greatest-of-list 
  (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 greatest remaining element
        (max (car numbers)
             (greatest-of-list (cdr numbers))))))

Here's an alternative version, based on a design from some students in a Spring 2003 session of CSC 151. Many students in the class participated in the design, but Andy Cook and Brett McMillian provided the key ideas. Sam Rebelsky wrote the extra documentation.

(define new-greatest-of-list
  (lambda (numbers )
    (new-greatest-of-list-helper numbers (max (car numbers) (cadr numbers)) 2)))

;;; Procedure:
;;;   new-greatest-of-list-helper
;;; Parameters:
;;;   numbers, a list of numbers
;;;   greatest-so-far, a number
;;;   position, an integer
;;; Purpose:
;;;   Find the greatest value in a sublist of numbers.
;;; Produces:
;;;   greatest, a number
;;; Preconditions:
;;;   position >= 0 [Unverified]
;;;   position <= (length numbers) [Unverified]
;;;   numbers contains only real numbers (no complex numbers). [Unverified]
;;;   greatest-so-far is a real number (not complex).  [Unverified]
;;; Postconditions:
;;;   greatest is the greatest of (a) greatest-so-far and (b) elements
;;;     position .. last of numbers.  (See greatest-of-list for what
;;;     it means to be greatest.)
(define new-greatest-of-list-helper
  (lambda (numbers greatest-so-far position)
    ; If we run off the end of the list, use the greatest-so-far
    (if (>= position (length numbers))
        greatest-so-far
        ; Otherwise, update your guess and continue
        (new-greatest-of-list-helper numbers 
                                     (max greatest-so-far 
                                          (list-ref numbers position))
                                     (+ position 1)))))

a. Which version do you prefer? Why?

b. Experiment with the two versions to verify that they work as advertised.

c. There's at least one kind of list for which one of the procedures works and one of the procedures doesn't. Can you tell which one?

Exercise 3: Yet Another Greatest-of-List Procedure

Here's yet another version of greatest-of-list, one based on new-greatest-of-list.

(define newer-greatest-of-list
  (lambda (numbers)
    (newer-greatest-of-list-helper (car numbers) (cdr numbers))))

(define newer-greatest-of-list-helper
  (lambda (greatest-so-far remaining-numbers)
    ; If no elements remain ...
    (if (null? remaining-numbers)
        ; Use the greatest value seen so far
        greatest-so-far
        ; Otherwise, update the guess using the next element
        ; and continue
        (newer-greatest-of-list-helper (max greatest-so-far 
                                            (car remaining-numbers))
                                       (cdr remaining-numbers)))))

a. Verify experimentally that this procedure works correctly.

b. You may find it helpful to watch new-greatest-of-list-helper and newer-greatest-of-list-helper.

Add the following two lines to new-greatest-of-list-helper (right before the outermost if).

  (display (list 'new-greatest-of-list-helper numbers greatest-so-far position))
  (newline)

Add the following two lines to newer-greatest-of-list-helper (right before the outermost if).

  (display (list 'newer-greatest-of-list-helper greatest-so-far remaining-numbers))
  (newline)

What effect does this change have?

c. How is newer-greatest-of-list-helper procedure similar to new-greatest-of-list-helper? How is it different? What are the advantages and disadvantages of the changes?

c. Which of the three versions do you prefer? Why?

Exercise 4: Product

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.

Exercise 5: Closest to Zero

Write a Scheme procedure, (closest-to-zero 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.

Exercise 6: Squaring Lists

Define and test a Scheme procedure, (square-each-element values), that takes a list of numbers as its argument and returns a list of their squares.

> (square-each-element (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.

For Those With Extra Time

If you find that you finish this laboratory early, you can start the next laboratory on list recursion.

 

History

 

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 Wed Jan 28 20:14:33 2004.
The source to the document was last modified on Wed Jan 28 20:14:31 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/recursion.html.

Valid HTML 4.0 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu