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. 机器翻译评测——BLEU改进后的NIST算法

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/7765345.html 上一节介绍了BLEU算的缺陷.NIS ...

  2. 【机器学习实战】第13章 利用 PCA 来简化数据

    第13章 利用 PCA 来简化数据 降维技术 场景 我们正通过电视观看体育比赛,在电视的显示器上有一个球. 显示器大概包含了100万像素点,而球则可能是由较少的像素点组成,例如说一千个像素点. 人们实 ...

  3. 三星R428 内存不兼容金士顿2G DDR3

    京东上买了个金士顿2G DDR3, 回家装上之后发现不兼容, 原机带的是三星DDR3 1066的2G条子,买的是 金士顿DDR3 2G 1333的条子,结果单独插任何一根都好使,两个插槽均无问题,但是 ...

  4. 【OpenCV】一种基于阈值的图片中的文字分割

    在今年泰迪杯A题电商中图片的文字识别这道题中,我们先用了一种很笨的办法来分割字符. 首先对图片进行灰度化,然后二值化,这里的二值化要选择一个合适的阈值.然后我们进行轮廓的提取,计算轮廓最小矩形的面积, ...

  5. javaScript函数提升及作用域

    代码片段: var a = 1; function foo() { console.log(a); //输出为undefined if (!a) { var a = 2; } alert(a); }; ...

  6. web前端vertical-align的作用及对象详解

    很多程序员知道web前端技术里的vertical-align是什么意思,但是对于vertical-align到底以什么为对齐标准却一知半解,今天我们就来说说web前端vertical-align. 1 ...

  7. matlab R2016a 中添加新的工具箱的方法

    matlab添加新的工具箱分三步: 1. 下载新的工具箱,并解压. 2. 将解压后的工具箱文件夹移到安装的matlab中的toolbox文件夹中 3. 添加新文件夹及其子文件夹到路径中. 接下来以添加 ...

  8. 入坑第二式 golang入坑系统

    史前必读: 这是入坑系列的第二式,如果错过了第一式,可以去gitbook( https://andy-zhangtao.gitbooks.io/golang/content/ )点个回放,看个重播.因 ...

  9. python 自动拉起进程脚本

    cat /usr/local/ssdb/moniter_ssdb.py #!/usr/bin/env python import os import sys import commands #ssdb ...

  10. Microsoft SQL Server 2008 R2数据库备份 - 人工备份

    业务介绍 数据库人工备份是指由相关管理人员通过主动的手工方式备份数据库文件.在一些特殊的时间节点,如重要资料的录入完成.软硬件环境更新前等需要特别关注数据库安全的时候,一定要进行数据库的人工备份,以保 ...