《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 ...
随机推荐
- Android之SharedPreferences两个工具类
相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...
- jquery插件-自定义select
由于原生select在各个浏览器的样式不统一,特别是在IE67下直接不可以使用样式控制,当PM让你做一个样式的时候,那是相当的痛苦.最好的办法就是使用自定义样式仿select效果.这里写了一个 ...
- pyqt说明
我是个PHP程序员,不过有时候觉得需要写些小软件,对于我这种不太熟悉桌面软件开发的人来说,界面问题最让我头痛.听说Qt很强大,而且是跨平台,所以决定学习它用来弥补我写桌面软件的不足. Qt一般是通过C ...
- handsontable常规配置的中文API
常规配置: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...
- shadow projection
1.概述 shadow projection,又可成为planar shadow, 这是一种非常简单的绘制阴影的方法. 主要应用的应用场景:物体在平面投射阴影. 主要思想:把阴影看作是物体在平面上的投 ...
- markdown 书写代码
近期基于github + hexo 搭建了自己的博客.開始用markdown写博客,推荐 mac 平台用 mou 这个软件或者 vim. 介绍下markdown语法插入代码的规则: 有一种方法是全部代 ...
- Udacity调试课笔记之断言异常
Udacity调试课笔记之断言异常 这一单元的内容不是很多,如Zeller教授所说,就是如何写.检查断言,并如何使用工具实现自动推导出断言的条件. 现在,多数的编程语言,尤其是高级编程语言都会有内置的 ...
- hdu 4427 Math Magic
一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替 ...
- iOS8 Core Image In Swift:人脸检测以及马赛克
iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...
- android——仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中
转载请注明出处: www.cnblogs.com/shoneworn 我这里只是简单的用了两个listview来实现的,先上效果图.比较粗糙.预留了自定义的空间. 思路: 从上图应该可以看的出来.就是 ...