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模块. 比较土...... 今天 ...
随机推荐
- NHibernate查询示例合集
基本查询 复杂查询示例 /// <summary> /// 获取自定义表单数据中属于部门的部分 /// </summary> /// <param name=&quo ...
- Python学习笔记整理总结【语言基础篇】
一.变量赋值及命名规则① 声明一个变量及赋值 #!/usr/bin/env python # -*- coding:utf-8 -*- # _author_soloLi name1="sol ...
- ASP.NET Core 与 Vue.js 服务端渲染
http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...
- Entity Framework 调用返回标量值的存储过程
最近项目用到EF,虽然说EF与Linq To SQL有很多地方相似,但是EF(这里指3.5版,4.0版的还没去留意)确实有些地方做得不够方便. 就拿存储过程来说吧,EF里面想调用存储过程不是直接在数据 ...
- HTML5 Web缓存&运用程序缓存&cookie,session
在介绍HTML5 web缓存前,来认识一下cookie和session: session: 由于HTTP是无状态的,你是谁?你干了什么?抱歉服务器都是不知道的. 因此session(会话)出现了,它会 ...
- 利用VGG19实现火灾分类(附tensorflow代码及训练集)
源码地址 https://github.com/stephen-v/tensorflow_vgg_classify 1. VGG介绍 1.1. VGG模型结构 1.2. VGG19架构 2. 用Ten ...
- 老男孩最新Python全栈开发视频教程(92天全)重点内容梳理笔记 看完就是全栈开发工程师
为什么要写这个系列博客呢? 说来讽刺,91年生人的我,同龄人大多有一份事业,或者有一个家庭了.而我,念了次985大学,年少轻狂,在大学期间迷信创业,觉得大学里的许多课程如同吃翔一样学了几乎一辈子都用不 ...
- overflow-x: scroll;横向滑动详细讲解
overflow-x: scroll;横向滑动(移动端使用详解) css3 , ie8以上 <!DOCTYPE html> <html lang="en"> ...
- 【机器学习】DNN训练中的问题与方法
感谢中国人民大学的胡鹤老师,人工智能课程讲的很有深度,与时俱进 由于深度神经网络(DNN)层数很多,每次训练都是逐层由后至前传递.传递项<1,梯度可能变得非常小趋于0,以此来训练网络几乎不会有什 ...
- 【复习】VueJS之内部指令
Vuejs 源码:https://github.com/zhuangZhou/vuejs 下载Vue.js 官网:http://vuejs.org live-server使用 live-server是 ...