SICP sicp 108: 3.21 exercises 1083.21 and

Source: Internet
Author: User

SICP sicp 108: 3.21 exercises 1083.21 and
Exercise 3-21 original

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 particle, 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.

Analysis

Ben's code is fine. The problem is that the standard output function of Lisp is different from the desired output method. The question requires defining a process print-queue to output what Ben thinks.

Code
(define (print-queue queue)   (car queue));Value: print-queue
Test
(define q2 (make-queue));Value: q2(print-queue q2);Value: ()(insert-queue! q2 'a);Value 16: ((a) a)(print-queue q2);Value 17: (a)(insert-queue! q2 'b);Value 16: ((a b) b)(print-queue q2);Value 17: (a b)(delete-queue! q2);Value 16: ((b) b)(print-queue q2);Value 18: (b)(delete-queue! q2);Value 16: (() b)(print-queue q2);Value: ()

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

Related Article

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.