这章让我明白了原来自然数的定义本来就是个递归的过程。

我们通常用枚举的方式引出自然数的定义: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:

deep-list is either

  1. s where s is some symbol or

  2. (cons dl empty), where dl is 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章自然数的更多相关文章

  1. 《how to design programs》12章函数复合

    我们写代码时要学会适应辅助函数.作者提出了一个问题,如何对一个表排序.排序函数读取一个表,产生另一个表.排序函数的合约和用途如下: (sort empty) ;; expected value: em ...

  2. 《how to design programs》15章 相互引用的数据定义

    由结构体组成的表与结构体中的表. 在用追溯形式建立家家谱树时,我们通常从某个后代除法,依次处理它的父母,组父母等.而构建树时,我们会不断添加谁是谁的孩子,而不是写出谁是谁的父母,从而建立一颗后代家谱树 ...

  3. 《how to design programs》14章 再论自引用数据

    这是一个家族谱: ;child(define-struct child (father mother name date eyes)) #lang racket ;child (define-stru ...

  4. 《how to design programs》13章用list构造表

    使用cons构造一个包含多个元素的表十分麻烦,因此scheme提供了list操作,该操作接受任意量的值作为输入以创建一个表,下面是扩展的语法: <prm>=list 扩展的scheme值的 ...

  5. 读《编写可维护的JavaScript》第11章总结

    这周也是拿到了同程的offer,从此走上了前端之路!感谢我的贵人们.再次纪念一下~! 第11章 不是你的对象不要动 11.1 什么是你的 你的对象:当你的代码创建了这些对象或者你有职责维护其他人的代码 ...

  6. 第11章 Windows线程池(1)_传统的Windows线程池

    第11章 Windows线程池 11.1 传统的Windows线程池及API (1)线程池中的几种底层线程 ①可变数量的长任务线程:WT_EXECUTELONGFUNCTION ②Timer线程:调用 ...

  7. 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  8. java JDK8 学习笔记——第11章 线程和并行API

    第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...

  9. 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群

    高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...

随机推荐

  1. Powershell变量的幕后管理

    Powershell变量的幕后管理 513 12月, 2011  在 Powershell  tagged 变量 / 类型 by Mooser Lee本文索引[隐藏]1修改变量的选项设置2激活变量的写 ...

  2. Absolute sort

    Absolute sort Let's try some sorting. Here is an array with the specific rules. The array (a tuple) ...

  3. CentOS 6.5配置nfs服务

    CentOS 6.5配置nfs服务 网络文件系统(Network File System,NFS),一种使用于分散式文件系统的协议,由升阳公司开发,于1984年向外公布.功能是通过网络让不同的机器.不 ...

  4. 移植strace调试工具到arm平台

    strace工具是一个非常强大的工具,是调试程序的好工具.要移植到arm平台,就需要使用交叉编译工具编译生成静态链接的可执行文件.具体步骤如下:1.下载 strace-4.5.16     移植str ...

  5. Net-Snmp安装配置

    1. 下载安装 net-snmp安装程序:net-snmp-5.4.2.1-1.win32.exe Perl安装程序:ActivePerl-5.10.0.1004-MSWin32-x86-287188 ...

  6. Sliding Window Maximum 解答

    Question Given an array of n integer with duplicate number, and a moving window(size k), move the wi ...

  7. ZooKeeper架构设计及其应用

    ZooKeeper是一个开源的分布式服务框架,它是Apache Hadoop项目的一个子项目,主要用来解决分布式应用场景中存在的一些问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置管理等 ...

  8. Bom和Dom编程以及js中prototype的详解

    一.Bom编程: 1.事件练习: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  9. CSS选择符 新的认识

    以前写选择符,很少使用">"符号,一般都是使用" "空格来代替.我觉得他们都是一样的功能,另一方面是为了方便.毕竟不用按住shift. 例如: .demo ...

  10. gdalwarp:变形工具

    1 gdalwarp:变形工具.包括投影.拼接.及相关的变形功能.此工具功能强大,但效率不高,使用时注意 gdalwarp [--help-general] [--formats]     [-s_s ...