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. java设计师初入职场,如何站稳脚跟

    本文内容一共由3部分展开 a:新人如何快速融入团队 b:如何在职场中提升自己影响力 c:如何规进行职业规划 a:如何快速融入团队   能在层层选拔下进入公司,说明你工作的能力还是得到公司的认可,不过这 ...

  2. 零基础如何迅速学习HTML5?新手小白学习web前端H5自白!

    很多的人在毕业之后才发现原来学的专业不是自己想做的工作,或者专业对口的工作待遇让人觉得并不满意,于是很多人选择培训机构学新的一门技能转换行业.IT行业的web前端H5受到很多学员的青睐.那么学习web ...

  3. Java_数据交换_fastJSON_01_用法入门

    一.用法 1.序列化—将Object转为Json对象 Object data=JSON.toJSON( MyObject ); 注:本文的Object可以是Map.List.javaBean等 需求: ...

  4. 小程序 wx.getRecorderManager 录音 to 语音识别

    微信扫小程序码看调用效果(自然语言理解小助手) 欢迎转载,请保留原文链接:http://www.happycxz.com/m/?p=125 这次主要是把我的api更新了一下,支持微信小程序新的录音接口 ...

  5. 万年历java

    public void showTime(){/*万年历 :  1900年1月20号是星期几?1月1号是星期一1月8号是星期一1月15号是星期一1%7 = 18%7 = 115%7 = 1★: 1. ...

  6. C# 读写文本文件乱码解决方案

    在使用C#对文本文件读取的时候,如果其中包含了中文,经常会出现乱码.一般解决是在StreamReader加一个编码,我使用的是Encoding.UTF8,一般情况下使用这个参数就可以.但是,在这次我使 ...

  7. python定时利用QQ邮件发送天气预报

    大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...

  8. [转载] java多线程学习-java.util.concurrent详解(二)Semaphore/FutureTask/Exchanger

    转载自http://janeky.iteye.com/blog/770393 ------------------------------------------------------------- ...

  9. 深入理解CSS盒模型

    如果你在面试的时候面试官让你谈谈对盒模型的理解,你是不是不知从何谈起.这种看似简单的题其实是最不好答的. 下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种 ...

  10. Python之re正则模块二

    13.编译的标志 可以用re.I.re.M等参数,也可以直接在表达式中添加"?(iLmsux)"标志 *s:单行,“.”匹配包括换行符在内的所有字符 *i:忽略大小写 *L:让&q ...