【SICP練習】108 練習3.21,sicp練習1083.21

來源:互聯網
上載者:User

【SICP練習】108 練習3.21,sicp練習1083.21
練習3-21原文

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.

分析

Ben的代碼本身沒有問題,問題在於Lisp的標準輸出函數和其所想要的輸出方式不同而已。題目要求的就是定義一個過程print-queue來輸出Ben所想的輸出結果。

代碼
(define (print-queue queue)   (car queue));Value: print-queue
測試
(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: ()

為使本文得到斧正和提問,轉載請註明出處:
http://blog.csdn.net/nomasp

郵箱及Skype:nomasp@outlook.com
Facebook:https://www.facebook.com/yuwang.ke
CSDN部落格:http://blog.csdn.net/nomasp
新浪微博:http://weibo.com/nomasp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.