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. C#递归查询

    一.sql --构造测试数据: 只作演示用 CREATE TABLE [dbo].[Tim_LinqTable]( [Id] int PRIMARY KEY IDENTITY(1,1) NOT NUL ...

  2. ubuntu 14.04搭建PHP项目基本流程

    首先准备需要安装东西的列表1.apache服务器,2.php,3.mysql,4.几个软件包的链接包,安装方式是以apt-get方式安装; 1.安装apache服务器: apt-get install ...

  3. 通过geotools读写shp文件

    依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...

  4. Adobe Html5 Extension开发初体验

    一.背景介绍       Adobe公司出品的多媒体处理软件产品线较多,涵盖了音视频编辑.图像处理.平面设计.影视后期等领域.为了扩展软件的功能,Adobe公司为开发者提供了两种方式来增加软件的功能: ...

  5. C# 使用正则表达式去掉字符串中的数字

    /// <summary>/// 去掉字符串中的数字/// </summary>/// <param name="key"></param ...

  6. 8. 理解ZooKeeper的内部工作原理

    到目前为止,我们已经讨论了ZooKeeper服务的基础知识,并详细了解了数据模型及其属性. 我们也熟悉了ZooKeeper 监视(watch)的概念,监视就是在ZooKeeper命名空间中的znode ...

  7. Python mysqldb模块

    #!/usr/bin/env python2.7 #-*- coding:utf8 -*- import os import sys import logging import MySQLdb fro ...

  8. C语言系列之printf和%12d的用法(三)

    看C语言程序的时候,往往会遇到printf函数输出,在此,我想总结一下printf的一般用法以及%12d是什么意思 printf函数的一般格式为 printf(格式控制,输出列表): 例如: prin ...

  9. border-sizing属性

    box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...

  10. 《Metasploit魔鬼训练营》第四章(下)

    p163 XSSF 默认kali 2.0中没有xssf,先下载:https://code.google.com/archive/p/xssf/downloads 将下载下来的zip文件解压,将其中的d ...