zoukankan      html  css  js  c++  java
  • 【SICP练习】108 练习3.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


    版权声明:本文为 NoMasp柯于旺 原创文章,如需转载请联系本人。

  • 相关阅读:
    DRUPAL 关于 $user 用户权限
    Delphi中代替WebBrowser控件的第三方控件
    最新美行地图Z13升级攻略
    Drupal Form问题汇总
    美行四维图新导航地图可以升级了!!
    Redis 主从配置
    MariaDB 双主复制的配置
    MariaDB 主从复制的配置
    在CentOS下利用Python+selenium获取腾讯首页的今日话题。
    利用Simple-RTMP-Server(SRS)来进行直播
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4786097.html
Copyright © 2011-2022 走看看