Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions sicp/3/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ Scheme 支持与 Python 相同的词法作用域规则,允许进行局部定
在 Scheme 语言中,`pair` 是内置的数据结构。出于历史原因,`pair` 是通过内置函数 `cons` 创建的,而元素则可以通过 `car` 和 `cdr` 进行访问:

```scheme
(define x (cons 1 2))

scm> (define x (cons 1 2))
x

(car x)

(cdr x)
scm> x
(1 . 2)
scm> (car x)
1
scm> (cdr x)
2
```

Scheme 语言中也内置了递归列表,它们使用 `pair` 来构建。特殊的值 `nil` 或 `'()` 表示空列表。递归列表的值是通过将其元素放在括号内,用空格分隔开来表示的:
Expand All @@ -163,20 +164,20 @@ Scheme 语言中也内置了递归列表,它们使用 `pair` 来构建。特
(cons 2
(cons 3
(cons 4 nil))))

(1 2 3 4)
(list 1 2 3 4)

(1 2 3 4)
(define one-through-four (list 1 2 3 4))

(car one-through-four)

1
(cdr one-through-four)

(2 3 4)
(car (cdr one-through-four))

2
(cons 10 one-through-four)

(10 1 2 3 4)
(cons 5 one-through-four)
(5 1 2 3 4)
```

要确定一个列表是否为空,可以使用内置的 `null?` 谓词。借助它,我们可以定义用于计算长度和选择元素的标准序列操作:
Expand All @@ -191,10 +192,10 @@ Scheme 语言中也内置了递归列表,它们使用 `pair` 来构建。特
(car items)
(getitem (cdr items) (- n 1))))
(define squares (list 1 4 9 16 25))

(length squares)

5
(getitem squares 3)
16
```

## 3.2.4 符号数据
Expand All @@ -208,10 +209,13 @@ Scheme 语言中也内置了递归列表,它们使用 `pair` 来构建。特
(define b 2)

(list a b)
(1 2)

(list 'a 'b)
(a b)

(list 'a b)
(a 2)
```

在 Scheme 中,任何不被求值的表达式都被称为被引用。这种引用的概念源自一个经典的哲学区分,即一种事物(比如一只狗)会四处奔跑和吠叫,而“狗”这个词是一种语言构造,用来指代这种事物。当我们在引号中使用“狗”时,我们并不是指代某只特定的狗,而是指代一个词语。在语言中,引号允许我们讨论语言本身,而在 Scheme 中也是如此:
Expand All @@ -224,8 +228,9 @@ Scheme 语言中也内置了递归列表,它们使用 `pair` 来构建。特

```scheme
(car '(a b c))

a
(cdr '(a b c))
(b c)
```

完整的 Scheme 语言包括更多功能,如变异操作(mutation operations)、向量(vectors)和映射(maps)。然而,到目前为止,我们介绍的这个子集已经提供了一个强大的函数式编程语言,可以实现我们在本文中讨论过的许多概念。
Expand Down