## Marvin's Blog【程式人生】

Ability will never catch up with the demand for it

How to Design Programs, Second Edition阅读笔记，Preface，Prologue: How to Program以及部分chapter 1。

## Preface

program design—but not programming—deserves the same role in a liberal-arts education as mathematics and language skills.

### Systematic Program Design

1. 从问题分析到数据定义。哪些信息需要被表示，以及如何表示。
2. 特征，目的陈述，头部。陈述函数所能处理的数据是什么形态；并且勾勒出函数运行的目标结构；定义一个桩脚来表示函数的这些特征。
3. 函数示例。找到一些示例，来表明函数是如何工作的。
4. 函数模板。将数据定义翻译成函数的一个轮廓。
5. 函数定义。根据函数的目的和已有的例子来填充函数模板。
6. 测试。保证函数的运行能满足示例的要求。在过程中查找和发现错误。

### DrRacket and the Teaching Languages

The crucial problem is that beginners make mistakes before they know much of the language, yet programming languages always diagnose these errors as if the programmer already knew the whole language. As a result, diagnosis reports often stump beginners.

As a result, diagnosis reports often stump beginners. In contrast, learning to design programs is primarily about the study of principles and the acquisition of transferable skills.

A programming environment for professionals is analogous to the cockpit of a jumbo jet.

Racket作为教学语言，可以用来构建其他语言。DrRacket是一个对新手友好的环境。使用DrRacket可以让用户专注于数据，而不是数据处理，即不用关心数据如何加载和保存。这和model-view-controller模式有点类似。

### Skills that Transfer

1. 分析问题声明，并用文字表述
2. 提取和表达概要，以抽象的方式
3. 以例子阐述概要
4. 根据分析指定行动大纲和计划
5. 根据目标期望来衡量结果
6. 根据结果的得与失改进方案

### The Differences

1. 回应了整个程序设计和细分函数设计的区别。第二版聚焦于两类程序：event-driven以及批处理
2. 整体程序设计通常自上而下规划，以及而上构建。前者产生一个对函数的期待列表，后者实现函数。
3. 实现函数的时候需要使用到设计配方。
4. 强调design by composition。函数之间如何打配合。
5. 强烈依赖于DrRacket提供的学习环境。
6. 放弃了强令式（imperative）编程。部分内容移到系列第二卷：How to Design Components
7. techpacks可以通过require的方式在代码中获得

## Prologue: How to Program

### Arithmetic and Arithmetic

DrRacket的定义窗口可以直接插入图片，感觉不错。通过(require 2htdp/image)，还可以直接在图片上执行image-width和image-height等操作。还可以通过(circle 10 "solid" "red")来直接生成图形。下面是另一个例子：

(overlay (circle 5 "solid" "red")
(rectangle 20 20 "solid" "blue"))


### One Program, Many Definitions

Better than pondering is doing.

### Not!

Acquiring the mechanical skills of programming—learning to write expressions that the computer understands, getting to know which functions and libraries are available, and similar activities—isn’t helping you all that much with real programming. If it were, you could equally well learn a foreign language by memorizing a thousand words from the dictionary and a few rules from a grammar book.

A good program reflects the problem statements and its important concepts. It comes with a concise self-description. Examples illustrate this description and relate it back to the problem. The examples make sure that the future reader knows why and how your code works. In short, good programming is about solving problems systematically and conveying the system within the code.

## I Fixed-Size Data

We equate “organizing thoughts” with design.

### 1 Arithmetic

#### 1.1 The Arithmetic of Numbers

BSL对于number提供以下操作：+, -, *, /, abs, add1, ceiling, denominator, exact->inexact, expt, floor, gcd, log, max, numerator, quotient, random, remainder, sqr, tan.

### 2 Functions and Programs

#### Functions

BSL中只有两种定义：

• 常量定义
• 函数定义

(define (FunctionName Variable ... Variable)
Expression)