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模块. 比较土...... 今天 ...
随机推荐
- NFS服务安装及配置
服务器环境:CentOS6.9 Linux 2.6.32-696.el6.x86_64 安装NFS服务 nfs客户端和服务端都只需要安装nfs-utils包即可,并且yum安装时会连带安装rpcbi ...
- SQL基本查询_多表查询(实验三)
SQL基本查询_多表查询(实验三) 题目要求(一) 针对emp.dept两表完成如下查询,并验证查询结果的正确性 使用显式内连接查询所有员工的信息,显示其编号.姓名.薪水.入职日期及部门名称: 使用隐 ...
- PLSQL锁表之后改如何操作
(1)查看哪个表被锁select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...
- 初生牛犊不怕虎 golang入坑系列
读前必读,下面所有内容都是来自这里. 放到这里的目的,就是为了比对一下,哪里的读者多.平心而论,同样的Markdown,博客园排版真心X看,怎么瞅怎么X看.(X := '难' || X :='耐' | ...
- SQL Server多表同时查询
今天在练sql server发现多条语句同时使用可以多表同时查询,具体操作如下: 代码示例: USE teachingGOSELECT *FROM dbo.teach_classORDER BY cl ...
- update and的坑
开发那边抛出个有意思的问题,下面的现象如何解释呢? mysql> select * from A; +------+------+ | t1 | t2 | +------+------+ | 1 ...
- ssh远程登录,禁止root登录
1,useradd xiaobingpasswd xiaobing (设置密码) 2,禁止root登陆,修改 /etc/ssh/sshd_configPermitRootLogin yes 改为 Pe ...
- 常见的Mysql数据库优化总结
索引 1.主键索引 作用:唯一约束和提高查询速度 #建表时创建主键索引 create table `table_name`( `id` int unsigned not null auto_incre ...
- Java提高十五:容器元素比较Comparable&Comparator深入分析
我们经常用容器来存放元素,通常而言我们是不关系容器中的元素是否有序,但有些场景可能要求容器中的元素是有序的,这个时候用ArrayList LinkedList Hashtable HashMap ...
- linux配置远程Git仓库
一,安装git yum install git 二,在服务器(119.28.1.1)目录( /git/admin )上创建一个仓库 cd /git/admin touch aaa.html git i ...