CISC-280 Program Development Techniques

Homework 7: Mutators, Queues, and Concurrency

Due: Wednesday, May 12, 2004; May Hand in Friday, May 14 w/ no Penalty

Problem 1 - 50 points

The builtin procedure length works correctly on proper lists, but will go into an infinite loop on lists with a loop back. For instance, this sequence of Scheme commands will go into an infinite loop:

(define L '(1 2 3 4 5))
(define M (cddddr L))
(set-cdr! M (cddr L))
(length L)

The resulting L is a structure with 5 pairs. The car's are the numbers 1 through 5. Let us identify the pairs by their cars. The mutation done by the set-cdr! makes the cdr of pair 5 be pair 3. Thus when length "walks down" L it visits the pairs in the order

1, 2, 3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 4, ....

As a result, length never terminates. L consists of an initial segment 1, 2 followed by a circle 3, 4, 5. Write a procedure clength that takes an arbitrary list structure and returns its length if it is a list without loop back. If there is a loop back, clength returns a list of two numbers (m n) where m is the number of pairs in the initial segment of the input and n is the number of pairs in the circle caused by the loop back. For the list structure L above, (clength L) returns the list (2 3).

Hints

First write a definition for clength that just computes the length of an ordinary list using an auxiliary procedure that runs as an iterative process. Then modify your auxiliary procedure to maintain a list of the pairs that have been seen already as it walks down the original list. You may use memq to test for membership on a list or write your own function using eq?. Do not use equal? or member (which uses equal?). If two of the cars of pairs in the input list have similar list structures that are themselves circular lists, equal? will never terminate. You may use length if you wish but be certain that the list you apply it to is guaranteed not to contain a loop back. (The input list might; that is why we are writing clength in the first place.)

Problem 2 - 50 Points

Problem 3.23 from the text (page 266) - deque implementation.

Problem 3 - 15 Extra Credit Points

Problem 3.21 from the text (pages 265-266) - printing a queue.

Problem 4 - 15 Extra Credit Points

Problem 3.39 from the text (page 306) - serialize execution results.