《how to design programs》第11章自然数
这章让我明白了原来自然数的定义本来就是个递归的过程。
我们通常用枚举的方式引出自然数的定义:0,1,2,3,等等(etc).最后的等等是什么意思?唯一能把等等从描述自然数的枚举方法中去除的方法是自引用,第一种尝试是:
0 is a natural number.
If n is a natural number, then one more than n is one, too.
虽然这种描述并不是十分严格,但对于scheme格式的数据定义来说,他是一个很好的开始:
natural-number(自然数是以下2者之一
1。0
2.(add1 n) 如果n个自然数
0是第一个自然数
(add1 0)是下一个,接着就明显了:
(add1 (add1 0))
(add1 (add1 (add1 0)))
这些例子让我们想到了表的构造过程,我们从empty出发,通过cons连接更多的元素,构造出表。现在我们从0出发,通过(不断地)使用add1加上1,构造出自然数。
处理自然数的函数必须能够提取构造自然数时所使用的数,就像处理表的函数能够提取cons结构中的元素一样,执行这种提取操作的被称为sub1,它的运算规则是:
(sub1 (add1 n) )= n
它与cdr操作的运算规则类似:
(cdr (cons a-value a-list))= a-list
习题11.2.1:设计一个函数repeat,该函数读入一个自然数n个一个符号,返回包含n个符号的表:
;; repeat : natural-number -> list-of-symbols
(define (repeat n a-symbol)
(cond
[(zero? n) empty]
[else (cons a-symbol (repeat (sub1 n) a-symbol))])) ;; Examples
(repeat 'doll) "should be" empty
(repeat 'rocket) "should be" (cons 'rocket (cons 'rocket empty))
习题11.2.2:
Develop the function tabulate-f, which tabulates the values of
;;f : number -> number
(define (f x)
(+ (* 3 (* x x))
(+ (* -6 x)
-1)))
for some natural numbers. Specifically, it consumes a natural number n and produces a list of n posns. The first one combines n with (f n), the second one n-1 with (f n-1), etc.
设计函数,把函数f应用于一些由自然数值组成的表,其中f是上面的函数。
具体地说,函数读取自然数n,返回有n个posn结构体组成表,表的第一个元素是点(n (f n)),第二个为(n-1 (f n-1)),以此类推。
;; a list-of-posns is either:
;; - empty
;; - (cons posn list-of-posns) ;; f : number -> number
(define (f x)
(+ (* (* x x))
(+ (* - x)
-))) ;; tabulate-f : number -> list-of-posns
;; produces a list of posns of length n. Each
;; posn's x coordinate is a number, from 0 to n, and each
;; posn's y coordinate is the result of f on the same posn's
;; x coordinate. #|
;; TEMPLATE
(define (tabulate-f n)
(cond
[(zero? n) ...]
[else (tabulate-f (sub1 n)) ...]))
|# (define (tabulate-f n)
(cond
[(zero? n) empty]
[else (cons (make-posn n (f n))
(tabulate-f (sub1 n)))])) ;; EXAMPLES AS TESTS
(tabulate-f ) "should be" empty
(tabulate-f ) "should be"
(cons (make-posn )
(cons (make-posn )
(cons (make-posn -)
(cons (make-posn -)
empty))))
11.2.4
Lists may contain lists that contain lists and so on. Here is a data definition that takes this idea to an extreme:
A deep-list is either
swheresis some symbol or(cons dl empty), wheredlis a deep list.
Develop the function depth, which consumes a deep list and determines how many times cons was used to construct it.
Develop the function make-deep, which consumes a symbol s and a natural number and produces a deep list containing s and constructed with nconses.
表的成员也可以是表,可以嵌套多层,下面就是这种思想的一个极端定义:
deep-list深层表示下列之一:
1.s其中s是symbol
2. (cons dl emtpy) dl是深层表。
设计函数depth,该函数读取一个深层表,测定这个表用了多少次cons来构成。设计函数make-deep,该函数读取符号s和自然数n,
返回包含s,使用n次cons构造的表。
;; depth : deep-list -> natural-number
;; counts the number of cons's used to create this deep list.
;depth:deep-list->number
(define (depth a-dl)
(cond
[(symbol? a-dl) ]
[else (+ (depth (car a-dl))) ]
)) (depth 'bottom) "should be" 0
(depth (cons (cons (cons (cons 'bottom empty) empty) empty) empty)) "should be" 4
(define (make-deep n s)
(cond
[(zero? n) s]
[else (cons (make-deep (sub1 n) s) empty)])) (make-deep 'bottom) "should be" 'bottom
(make-deep 'bottom) "should be"
(cons (cons (cons (cons 'bottom empty) empty) empty) empty)
DRracekt输出:'((((bottom))))
可以看到,很直观的表示4层嵌套。
自然数的另一种数据定义:
自然数(>=20)是系列之一:
1.20
2.(add 1 n)如果n是自然数。
说白了第一种定义时减法,第二种定义时加法。
计算从20(不包括20)到n(包括n)的乘积:
;计算n*(n-)...*
(define (product-from- n-above-)
(cond
[(= n-above- ) ]
[else (* n-above- (product-from- (sub1 n-above-)) )]
))
(product-from- ) ;462
《how to design programs》第11章自然数的更多相关文章
- 《how to design programs》12章函数复合
我们写代码时要学会适应辅助函数.作者提出了一个问题,如何对一个表排序.排序函数读取一个表,产生另一个表.排序函数的合约和用途如下: (sort empty) ;; expected value: em ...
- 《how to design programs》15章 相互引用的数据定义
由结构体组成的表与结构体中的表. 在用追溯形式建立家家谱树时,我们通常从某个后代除法,依次处理它的父母,组父母等.而构建树时,我们会不断添加谁是谁的孩子,而不是写出谁是谁的父母,从而建立一颗后代家谱树 ...
- 《how to design programs》14章 再论自引用数据
这是一个家族谱: ;child(define-struct child (father mother name date eyes)) #lang racket ;child (define-stru ...
- 《how to design programs》13章用list构造表
使用cons构造一个包含多个元素的表十分麻烦,因此scheme提供了list操作,该操作接受任意量的值作为输入以创建一个表,下面是扩展的语法: <prm>=list 扩展的scheme值的 ...
- 读《编写可维护的JavaScript》第11章总结
这周也是拿到了同程的offer,从此走上了前端之路!感谢我的贵人们.再次纪念一下~! 第11章 不是你的对象不要动 11.1 什么是你的 你的对象:当你的代码创建了这些对象或者你有职责维护其他人的代码 ...
- 第11章 Windows线程池(1)_传统的Windows线程池
第11章 Windows线程池 11.1 传统的Windows线程池及API (1)线程池中的几种底层线程 ①可变数量的长任务线程:WT_EXECUTELONGFUNCTION ②Timer线程:调用 ...
- 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- java JDK8 学习笔记——第11章 线程和并行API
第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...
- 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群
高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...
随机推荐
- Network Attack
Network Attack Nicola regularly inspects the local networks for security issues. He uses a smart and ...
- July 【补题】
A(zoj 3596) bfs,记忆搜都可以, 按余数来记录状态. B(zoj 3599) 博弈,跳过 C(zoj 3592) 简单dp,题意不好懂 D(zoj 3602) 子树哈希, 对根的左右儿子 ...
- 奇妙的go语言(聊天室的开发)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 这是一篇关于聊天室开发的博客,原来文章的地址来自于此.这篇文章非常具有代表性,对于代码中的函数 ...
- HDU 1863:畅通project(带权值的并查集)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 浅谈Android系统开发中LOG的使用
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用 ...
- Qt快速入门系列教程目录
Qt快速入门系列教程目录
- CPU性能测试
用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...
- .NET批量大数据插入性能分析及比较
数据插入使用了以下几种方式 1. 逐条数据插入2. 拼接sql语句批量插入3. 拼接sql语句并使用Transaction4. 拼接sql语句并使用SqlTransaction5. 使用DataAda ...
- C#枚举器接口IEnumerator的实现
原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...
- android 中使用缓存加载数据
最近app快完工了,但是很多列表加载,新闻咨询等数据一直从网络请求,速度很慢,影响用户体验,所以寻思用缓存来加载一些更新要求不太高的数据 废话不多说,上代码 欢迎转载,但请保留文章原始出处:) 博客 ...