97 lines
2.1 KiB
Markdown
97 lines
2.1 KiB
Markdown
|
+++
|
|||
|
title = "SICP 第一课"
|
|||
|
author = ["SouthFox"]
|
|||
|
date = 2022-09-02T22:17:00+08:00
|
|||
|
lastmod = 2022-09-02T22:42:53+08:00
|
|||
|
tags = ["publish"]
|
|||
|
draft = false
|
|||
|
+++
|
|||
|
|
|||
|
## 程序设计中要思考的问题是? {#程序设计中要思考的问题是}
|
|||
|
|
|||
|
- 程序是抽象的,不像电气设计中的考虑串联几千个放大器(因为要考虑误差等),程序设计是理想化的,可以考率不断循环下对于程序和设计有什么影响。
|
|||
|
|
|||
|
|
|||
|
## 程序设计的三大想法? {#程序设计的三大想法}
|
|||
|
|
|||
|
- 黑盒抽象
|
|||
|
将简单的部分组合一个部件然后像积木一样不断堆积,这个部件对于外部是抽象的,可以拿来直接用的。
|
|||
|
- 约定接口
|
|||
|
- 元语言抽象
|
|||
|
|
|||
|
|
|||
|
## Lisp 基础 <span class="tag"><span class="NODE">NODE</span></span> {#lisp-基础}
|
|||
|
|
|||
|
|
|||
|
### 括号 {#括号}
|
|||
|
|
|||
|
Lisp 用括号来表达一个组合式,括号多的时候可以用缩进来减少对身心的损耗(
|
|||
|
|
|||
|
|
|||
|
### 前缀 {#前缀}
|
|||
|
|
|||
|
Lisp 是前缀表达语言,如:
|
|||
|
|
|||
|
```scheme
|
|||
|
(+ 1 2)
|
|||
|
```
|
|||
|
|
|||
|
结果显示为 3 。
|
|||
|
|
|||
|
```text
|
|||
|
3
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
### 定义 {#定义}
|
|||
|
|
|||
|
用 DEFINE 关键词来定义一个过程(也叫 lambda),通过一些基本过程搭建的过程来完成一个复杂的目的,这就是黑盒。
|
|||
|
例子:求平方根
|
|||
|
|
|||
|
```scheme
|
|||
|
(define (square-iter guess x)
|
|||
|
(if (good-enough? guess x)
|
|||
|
guess
|
|||
|
(begin ; used this compound procedure in order to be able to
|
|||
|
(display guess) ; display the intermediate guess values
|
|||
|
(newline)
|
|||
|
(square-iter (improve guess x) x)
|
|||
|
)
|
|||
|
)
|
|||
|
)
|
|||
|
|
|||
|
(define (improve guess x)
|
|||
|
(average guess (/ x guess)))
|
|||
|
|
|||
|
(define (average x y)
|
|||
|
(/ (+ x y) 2))
|
|||
|
|
|||
|
(define (square x)
|
|||
|
(* x x))
|
|||
|
|
|||
|
(define (good-enough? guess x)
|
|||
|
(< (abs (- (square guess) x)) 0.001))
|
|||
|
|
|||
|
(define (sqrt x)
|
|||
|
(square-iter 1.0 x))
|
|||
|
|
|||
|
(define (square-iter-delta guess x)
|
|||
|
(if (in-delta? guess (improve guess x))
|
|||
|
guess
|
|||
|
(begin
|
|||
|
(display guess)
|
|||
|
(newline)
|
|||
|
(square-iter-delta (improve guess x) x)
|
|||
|
)
|
|||
|
)
|
|||
|
)
|
|||
|
|
|||
|
(define (in-delta? guess1 guess2)
|
|||
|
(< (abs (- guess1 guess2)) 0.001))
|
|||
|
|
|||
|
(define (sqrt-delta x)
|
|||
|
(square-iter-delta 1.0 x))
|
|||
|
|
|||
|
(sqrt-delta 25)
|
|||
|
```
|