Question:

produce a deep-reverse procedure that takes a list as argument 

and returns as its value the list with its elements reversed 

and with all sublists deep-reversed as well.



For example:

(define x (list (list 1 2) (list 3 4)))

x

((1 2) (3 4))

(reverse x)

((3 4) (1 2))

(deep-reverse x)

((4 3) (2 1))



Code:

( define tree ( list 1 ( list 2 ( list 3 4 ) 5 ) ( list 6 7 ) ) )

( define nil '() )



( define ( my-reverse items )

   ( define ( rev-imp items result )

      ( if ( null?

items ) 

           result 

           ( rev-imp ( cdr items )

                     ( cons ( car items ) result ) ) ) )

   ( rev-imp items nil ) )

( my-reverse items )



Output:

> ( my-reverse tree )

((6 7) (2 (3 4) 5) 1)



Code:

( define ( deep-reverse items )

   ( define ( deep-rev-if-required item )

      ( if ( not ( pair? item ) ) 

           item 

           ( deep-reverse item ) ) )

   ( define ( deep-rev-imp items result )

      ( if ( null? items ) 

           result 

           ( deep-rev-imp ( cdr items )

                          ( cons ( deep-rev-if-required( car items ) ) 

                                 result ) ) ) )

   ( deep-rev-imp items nil ) ) 



Output:

> ( deep-reverse tree )

((7 6) (5 (4 3) 2) 1)



or Code as:

( define ( deep-reverse items )

   ( if ( pair?

items )

        ( my-reverse ( map deep-reverse items ) )

        items ) )



Output:

> ( deep-reverse tree )

((7 6) (5 (4 3) 2) 1)







Question:

Write a procedure fringe that takes as argument a tree  (represented as a list) and 

returns a list whose elements are all the leaves of the tree arranged in left-to-right order. 



For example:

(define x (list (list 1 2) (list 3 4)))

(fringe x)

(1 2 3 4)

(fringe (list x x))

(1 2 3 4 1 2 3 4)



Code:

( define ( fringe tree )

   ( define ( search items res )

      ( cond ( ( null? items ) 

               res )

             ( ( not ( pair? items ) ) 

               ( cons items res ) )

             ( else ( search ( car items )

                             ( search ( cdr items ) res ) ) ) ) )

   ( search tree nil ) )



or Code as:

( define ( fringe tree )

   ( cond ( ( null? tree ) 

            nil )

          ( ( not ( pair? tree ) )

                ( list tree ) )

          ( else ( append ( fringe ( car tree ) )

                          ( fringe ( cdr tree ) ) ) ) ) )



Output:

> ( fringe tree )  

(1 2 3 4 5 6 7)







Question:

We can represent a set as a list of distinct elements, 

and we can represent the set of all subsets of the set as a list of lists. 

For example, if the set is (1 2 3), then the set of all subsets is 

(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). 

Complete the following definition of a procedure that

generates the set of subsets of a set and give a clear explanation of why it works:



For example:

( define ( subsets s )

  ( if ( null? s )

      ( list nil )

      ( let ( ( rest ( subsets ( cdr s ) ) ) )

        ( append rest ( map <??> rest ) ) ) ) )



Code:

( define nil '() )

( define ( subsets s )

   ( if ( null? s )

        ( list nil )

        ( let ( ( rest ( subsets ( cdr s ) ) ) )

           ( append rest ( map ( lambda ( x )

                                ( cons ( car s ) x ) )

                               rest ) ) ) ) )





Output:

> ( subsets ( list 1 2 3 ) )

(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))

Scheme -- Hierarchical Structures的更多相关文章

  1. 翻译 Improved Word Representation Learning with Sememes

    翻译 Improved Word Representation Learning with Sememes 题目 Improved Word Representation Learning with ...

  2. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  3. Extended symmetrical multiprocessor architecture

    An architecture for an extended multiprocessor (XMP) computer system is provided. The XMP computer s ...

  4. Smart internet of things services

    A method and apparatus enable Internet of Things (IoT) services based on a SMART IoT architecture by ...

  5. [转]在SqlServer 中解析JSON数据

      在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...

  6. R 网页数据爬虫1

    For collecting and analyzing data. [启示]本处所分享的内容均是笔者从一些专业书籍中学习所得,也许会有一些自己使用过程中的技巧.心得.小经验一类的,但远比不上书中所讲 ...

  7. 【SICP感应】3 级数据和符号数据

    在本书的第二章学习时,有一个问题我一直很困扰,那是2.2.4举例节.因为没有华丽的输出模式书,它只能有一个对的英文字母.两三个月的这浅浅的学校前Common Lisp同样是真实的,当.了非常赞的线条, ...

  8. [Python] logging.logger

    <1>. mylogger = logging.getLogger("abc") logging.debug()/logging.info()/logging.warn ...

  9. Python标准模块logging

    http://blog.csdn.net/fxjtoday/article/details/6307285 开发Python, 一直以来都是使用自己编写的logging模块. 比较土...... 今天 ...

随机推荐

  1. RobotFramework自动化测试框架-移动手机自动化测试Get Element Location关键字的使用

    Get Element Location关键字用来获取一个Element的Location位置,该关键字接收一个参数[ locator ] 示例1:使用Get Element Location来获取一 ...

  2. Android打开/data/目录以及导出文件

    打开logcat和和file Explorer Tools-->Android-->Android Device Monitor 如果右侧没有出现,Windows-->Show Vi ...

  3. Cache 和 Buffer 都是缓存,主要区别是什么?

    存储器的高速缓冲存储器存储了频繁访问的RAM位置的内容及这些数据项的存储地址.当处理器引用存储器中的某地址时,高速缓冲存储器便检查是否存有该地址.如果存有该地址,则将数据返回处理器;如果没有保存该地址 ...

  4. Lucene全文检索学习笔记

    全文索引 介绍Lucene的作者:Lucene的贡献者Doug Cutting是 一位资深全文索引/检索专家,曾经是V-Twin搜索引擎(Apple的Copland操作系统的成就之一)的主要开发者,后 ...

  5. java继承系列之添加一个LinkLable类

    import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import java.awt.event.WindowList ...

  6. Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors...java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are

    先将异常信息贴出: 该问题是tomcat进行http request解析的时候报的错,网上的解决办法主要是修改Tomcat的server.xml,在<Connector port="8 ...

  7. springMVC+spring+MyBatis(SSM)的简单配置

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中: Spring是一 ...

  8. Spring集成Quartz完成定时任务

    在JavaEE系统中,我们经常会用到定时任务,比如每天晚上凌晨之后跑批处理或者是每天某个时刻群发消息等等. 我们可以使用java.util.Timer结合java.util.TimerTask来去完成 ...

  9. [转] 深刻理解Python中的元类(metaclass)

    非常详细的一篇深入讲解Python中metaclass的文章,感谢伯乐在线-bigship翻译及作者,转载收藏. 本文由 伯乐在线 - bigship 翻译.未经许可,禁止转载!英文出处:stacko ...

  10. RSA非对称加密简析-java

    1 非对称加密算法 1.1 概述 1976年,美国学者Dime和Henman为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这 ...