# The Little Schemer读书笔记（九）

The Little Schemer总结篇。

## 读后感

特点，采用对话的形式来引导阅读，希望读者能够自行给出对于所涉及概念的定义。但是书中还是列举了许多Laws和Amendments，也就是条例和修正案，来帮助读者记住相关原理。

迭代和递归在基本概念上有所差异。假设处理对象是一串念珠。迭代的话是造一个窟窿，把念珠一个一个填到这个窟窿，然后进行处理。这个窟窿，其实是一个自由的变量，任何一颗念珠都可以填充进来。而递归，就像念经时一颗一颗数念珠，其实隐含了一个既定的次序，通过递归迭代的方式进行递归。每个念珠对下一颗念珠其实是有假设的。

迭代假设环境的一致性（虽然环境内的内容可以变化，但是环境内的实体不变），而递归强调环境的可变性（环境内的实体和内容都可以变化，环境还可以叠加）。

## The Ten Commandments

The First Commandment

When recurring on a list of atoms, lat, ask two questions about it:

`(null? lat)`

and`else`

. When Recurring on a number, n, ask two questions about it:`(zero? n)`

and`else`

. When recurring on a list of S-expressions, l, ask three question about it:`(null? l)`

,`(atom? (car l))`

, and`else`

.

The Second Commandment

Use cons to build lists.

The Third Commandment

When building a list, describ the first typical element, and then cons it onto the natural recursion.

The Fourth Commandment

Always change at least one argument while recurring. When recurring on a list of atoms,

`lat`

, use`(cdr lat)`

. When recurring on a number,`n`

, use`(sub1 n)`

. And when recurring on a list of S-expressions,`l`

, use`(car l)`

and`(cdr l)`

if neither`(null? l)`

nor`(atom? (car l))`

are true.It msut be changed to be closer to termination. The changing argument must be tested in the termination condition:

- when using
`cdr`

, test termination with`null?`

and- when using
`sub1`

, test termination with`zero?`

.

The Fifth Commandment

When building a value with +, always use 0 for the value of the terminating line, for adding 0 does not change the value of an addition.

When building a value with x, always use 1 for the value of the terminating line, for multiplying by 1 does not change the value of a multiplication.

When building a value with

`cons`

, always consider`()`

for the value of terminating line.

The Sixth Commandment

Simplify only after the function is correct.

The Seventh Commandment

Recur on the

`subparts`

that are of the same nature

- On the sublists of a list
- On the subexpressions of an arithmetic expression.

The Eight Commandment

Use help functions to abstract from representations.

The Ninth Commandment

Abstract common patterns with a new function

The Tenth Commandment

Build functions to collect more than one value at time.

## The Five Rules

The Law of Car

The primitive car is defined only for non-empty lists.

The Law of Cdr

The primitive car id defined only for non-empty lists. The cdr of any non-empty list is always another list.

The Law of Cons

The primitive cons takes two arguments. The second argument to cons must be a list. The result is a list.

The Law of Null?

The primitive null? is defined only for lists.

The Law of Eq?

The primitive eq? takes two arguments. Each must be a non-numeric atom.

## Intermission

下一幕是： The Reasoned Schemer。然后书中还列举了一些可供参考的书目。

## 其他

- The Little Schemer - 4th Edition
- 别人整理好的本书代码：https://github.com/viswanathgs/The-Little-Schemer
- 《The Little Schemer》笔记： https://github.com/chenjiee815/the_little_schemer/

（完）