灯下 登录
计算机科学 / SICP / 3.3.2 Representing Queues

Exercise 3.22 · 习题

Exercise 3.22: Instead of representing a queue
as a pair of pointers, we can build a queue as a procedure with local state.
The local state will consist of pointers to the beginning and the end of an
ordinary list. Thus, the make-queue procedure will have the form

(define (make-queue)
(let ((front-ptr … )
(rear-ptr … ))
⟨definitions of internal procedures⟩
(define (dispatch m) …)
dispatch))

Complete the definition of make-queue and provide implementations of the

queue operations using this representation.

练习 3.22:与其将队列表示为一对指针,不如将队列构建为一个带有局部状态的过程。局部状态由指向普通表的首端和末端的指针组成。这样,make-queue 过程将具有如下形式:

(define (make-queue)
(let ((front-ptr … )
(rear-ptr … ))
⟨definitions of internal procedures⟩
(define (dispatch m) …)
dispatch))

请完成 make-queue 的定义,并用此表示方式给出各队列操作的实现。

Racket #lang sicp
(define (make-queue)
 (let ((front-ptr … )
 (rear-ptr … ))
 ⟨definitions of internal procedures⟩
 (define (dispatch m) …)
 dispatch))