Scheme -- Hierarchical Structures
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的更多相关文章
- 翻译 Improved Word Representation Learning with Sememes
		
翻译 Improved Word Representation Learning with Sememes 题目 Improved Word Representation Learning with ...
 - C++ Core Guidelines
		
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
 - Extended symmetrical multiprocessor architecture
		
An architecture for an extended multiprocessor (XMP) computer system is provided. The XMP computer s ...
 - Smart internet of things services
		
A method and apparatus enable Internet of Things (IoT) services based on a SMART IoT architecture by ...
 - [转]在SqlServer 中解析JSON数据
		
在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...
 - R 网页数据爬虫1
		
For collecting and analyzing data. [启示]本处所分享的内容均是笔者从一些专业书籍中学习所得,也许会有一些自己使用过程中的技巧.心得.小经验一类的,但远比不上书中所讲 ...
 - 【SICP感应】3  
级数据和符号数据
		
在本书的第二章学习时,有一个问题我一直很困扰,那是2.2.4举例节.因为没有华丽的输出模式书,它只能有一个对的英文字母.两三个月的这浅浅的学校前Common Lisp同样是真实的,当.了非常赞的线条, ...
 - [Python] logging.logger
		
<1>. mylogger = logging.getLogger("abc") logging.debug()/logging.info()/logging.warn ...
 - Python标准模块logging
		
http://blog.csdn.net/fxjtoday/article/details/6307285 开发Python, 一直以来都是使用自己编写的logging模块. 比较土...... 今天 ...
 
随机推荐
- cloneNode克隆节点在不同浏览器的差异
			
cloneNode是用于克隆节点的,如果待克隆的节点还有子节点以及自定义属性.添加的有事件,那么克隆时,可以指定是克隆节点本身,还是将其所有子节点信息也克隆进去,这是通过给cloneNode传递一个布 ...
 - iOS字符串修改及运用
			
//创建字符串 直接赋值 NSString *lytTest = @"A common string"; 1.获取字符串的长度 NSLog(@"%d",lytT ...
 - Javascript数组求和的方法总结 以及由斐波那契数列得到的启发
			
一次面试中,面试官要求用三种不同的Javascript方法进行一个数字数组的求和,当时思来想去只想到了使用循环这一种笨方法,因此面试比较失败,在这里总结了六种Javascript进行数组求和的方法,以 ...
 - windows查看端口占用命令
			
开始--运行--cmd 进入命令提示符 输入netstat -aon 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
 - 阿里云centos配置postfix
			
1. 为了防止垃圾邮件,先到域名控制面板设置好. MX A 记录及TXT记录 其中TXT记录如下 @ spf1 a mx ~all 意思就是使用spf1协议,允许a记录和MX记录对应的IP,不允许 ...
 - 继承JFrame,只是初步
			
import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import java.awt.event.WindowList ...
 - [转载] Tomcat架构分析
			
转载自http://gearever.iteye.com/category/223001
 - MyBatis《2》
			
MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/ 1.properties 属性 1.在MyBatis配置文件中引用属性文件 MyBatis允许在 ...
 - Kaggle初入门
			
今天成功的进驻kaggle社区了! 所以以后就要跟kaggle上面的各位一起学习啦! 今天十分成功的在tensorflow的环境里面装了一堆库--什么seaborn啊pandas啊都一次过 然后--并 ...
 - 延迟执行之 Invoke 函数
			
Invoke 函数需要继承 MonoBehaviour 类后才能使用. Invoke(string str,float a):a 秒后执行名为 str 函数(只会调用一次). Invoke(strin ...