SICP sicp 109: 3.22 exercises 1093.22 and

Source: Internet
Author: User

SICP sicp 109: 3.22 exercises 1093.22 and
Exercise 3-22 Original

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.

Analysis

The local state in this question is composed of the start and end pointers pointing to a regular table. And insert-queue! And delete-queue! Nested in the entire process. Use dispatch to call these functions.

(define (make-queue)    (let ((front-ptr '())          (rear-ptr '()))   (define (empty-queue?)      (null? front-ptr))   (define (insert-queue! item)       (cond ((empty-queue?)              (let ((init-list (list item)))                  (set! front-ptr init-list)                  (set! rear-ptr init-list)                  front-ptr))             (else              (let ((new-item (list item)))                  (set-cdr! rear-ptr new-item)                  (set! rear-ptr new-item)                  front-ptr))))   (define (delete-queue!)       (cond ((empty-queue?)              (error "DELETE! called with an empty queue" queue))             (else              (set! front-ptr (cdr front-ptr))              front-ptr)))   (define (dispatch m)      (cond ((eq? m 'insert-queue!)              insert-queue!)            ((eq? m 'delete-queue!)              (delete-queue!))            ((eq? m 'empty-queue?)              (empty-queue?))            (else             (error "Unknown operation -- DISPATCH" m))))    dispatch))
Test
(define q3 (make-queue))        ;Value: q3((q3 'insert-queue!) 'a)                ;Value 15: (a)((q3 'insert-queue!) 'b);Value 16: (a b)(q3 'delete-queue!)                  ;Value 17: (b)(q3 'delete-queue!);Value: ()(q3 'empty-queue?)                   ;Value: #t
Supplement

Because insert-queue! There are parameters, so no parentheses need to be added in dispatch; otherwise, an error is reported.

For this article to get an axe and a question, please indicate the source:
Http://blog.csdn.net/nomasp

Email and Skype: nomasp@outlook.com
Facebook: https://www.facebook.com/yuwang.ke
CSDN blog: http://blog.csdn.net/nomasp
Sina Weibo: http://weibo.com/nomasp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.