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. CSS与JS中的相对路径引用

    javascript和css文件中采用相对路径,其基准路径是完全不同的. 1.javascript引用资源(比如图片)相对路径是以宿主路径(被引用的网页比如你在首页index.php引用了某js文件, ...

  2. Linux系列教程(十五)——Linux用户和用户组管理之用户管理命令

    上篇博客我们介绍了用户管理的相关配置文件,包括用户信息文件/etc/passwd,用户密码文件/etc/shadow:然后介绍了用户组信息文件/etc/group,用户组密码文件/etc/gshado ...

  3. angualr4 路由 总结笔记

    使用cli命令创建根路由模块 ng g cl app.router 或自己建一个路由配置文件 如:app/app.router.ts // app/app.router.ts // 将文件修改为 im ...

  4. ASP.NET Core 与 Vue.js 服务端渲染

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  5. Android ANR异常解决方案

    1,ANR异常的解释: ANR(android not response)即应用程序无响应,在用户操作在5秒内没有响应的话就会出现ANR异常: 2,那为什么会出现ANR异常呢? Android系统中处 ...

  6. 从vultr购买到搭ss看世界

    title: 从Vultr购买到搭ss看世界 date: 2017-11-19 12:28:38 categories: 技术 tags: top: 800 password: 写在前面 ​ 服务器提 ...

  7. Zuul(SpringCloud学习笔记一)

    路由是微服务架构中必须(integral )的一部分,比如,"/" 可能映射到你的WEB程序上,"/api/users "可能映射到你的用户服务上," ...

  8. [OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)

    在上一篇基于OIDC的SSO的中涉及到了4个Web站点: oidc-server.dev:利用oidc实现的统一认证和授权中心,SSO站点. oidc-client-hybrid.dev:oidc的一 ...

  9. 关于IDEA无法引入包和类的情况

    原因:修改一些包结构后,会出现无法将类引入的情况. 通过清理缓存解决:

  10. 异常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException

    这个异常是出现在注入配置文件中配置好的属性时报错的: Injection of autowired dependencies failed; nested exception is java.lang ...