SICP Exercise 3.21 make-queue

Exercise 3.21.  Ben Bitdiddle decides to test the queue implementation described above. He types in the procedures to the Lisp interpreter and proceeds to try them out:

(define q1 (make-queue))
(insert-queue! q1 'a)
((a) a)
(insert-queue! q1 'b)
((a b) b)
(delete-queue! q1)
((b) b)
(delete-queue! q1)
(() b)


``It's all wrong!'' he complains. ``The interpreter's response shows that the last item is inserted into the queue twice. And when I delete both items, the second b is still there, so the queue isn't empty, even though it's supposed to be.'' Eva Lu Ator suggests that Ben has misunderstood what is happening. ``It's not that the items are going into the queue twice,'' she explains. ``It's just that the standard Lisp printer doesn't know how to make sense of the queue representation. If you want to see the queue printed correctly, you'll have to define your own print procedure for queues.'' Explain what Eva Lu is talking about. In particular, show why Ben's examples produce the printed results that they do. Define a procedure print-queue that takes a queue as input and prints the sequence of items in the queue.

S O L U T I O N

The code is here:

SICP Exercise 3.21 make-queue

Explanation: 

Eva Lu is right. The printer doesn't know how to make sense of the queue representation. In our queue representation, we have a standard list which is a chain of pairs with a pointer to the beginning of the list and an additional pointer that indicates the final pair in the list. The pointer to the beginning of the list and the pointer to the final pair are combined with a 'cons' and the resulting pair is the queue object. When the interpreter tries to print the queue object, it follows the standard way of first printing the 'car' of the pair and then the 'cdr' of the pair. The 'car' points to the list that contains the items of the queue, so the entire queue is printed in the standard list format. After that the cdr of the queue
object is printed. This being the last pair in the queue, the last item is printed again. That is why every time the queue is printed, the last item appears twice: once as the last item in the 'car' of the queue and once as the 'cdr' of the queue.

Note that delete-queue! merely moves the front pointer to the second item in the queue. So even when the last item is "deleted", the rear pointer continues to point to it. That is why Ben sees the output: (() b)

I have written the procedure print-queue to print the items of the queue without any repetition. See the test results above.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions