Chapter 2 dealt with compound data as a means for constructing computational
objects that have several parts, in order to model real-world objects that have
several aspects. In that chapter we introduced the discipline of data
abstraction, according to which data structures are specified in terms of
constructors, which create data objects, and selectors, which access the parts
of compound data objects. But we now know that there is another aspect of data
that chapter 2 did not address. The desire to model systems composed of
objects that have changing state leads us to the need to modify compound data
objects, as well as to construct and select from them. In order to model
compound objects with changing state, we will design data abstractions to
include, in addition to selectors and constructors, operations called
第 2 章将复合数据作为构造具有多个组成部分的计算对象的手段,以便对具有多个方面的现实世界对象进行建模。在那一章中,我们引入了数据抽象的规范:数据结构由构造函数(用于创建数据对象)和选择函数(用于访问复合数据对象的各个部分)来规定。然而,我们现在认识到,第 2 章没有涉及数据的另一个方面。对由具有变化状态的对象所组成的系统进行建模,要求我们不仅能够构造和选取复合数据对象,还必须能够修改它们。为了对具有变化状态的复合对象建模,我们将在选择函数和构造函数之外,为数据抽象引入一类称为
mutators, which modify data objects. For instance, modeling a
banking system requires us to change account balances. Thus, a data structure
for representing bank accounts might admit an operation
修改函数 (mutators) 的操作,用于修改数据对象。例如,对银行系统建模要求我们能够修改账户余额。因此,表示银行账户的数据结构可能需要支持一种操作,
that changes the balance of the designated account to the designated new value.
Data objects for which mutators are defined are known as
mutable data objects.
将指定账户的余额修改为指定的新值。凡是定义了修改函数的数据对象,称为可变数据对象 (mutable data objects)。
Chapter 2 introduced pairs as a general-purpose “glue” for synthesizing
compound data. We begin this section by defining basic mutators for pairs, so
that pairs can serve as building blocks for constructing mutable data objects.
These mutators greatly enhance the representational power of pairs, enabling us
to build data structures other than the sequences and trees that we worked with
in 2.2. We also present some examples of simulations in which
complex systems are modeled as collections of objects with local state.
第 2 章将序对作为合成复合数据的通用"黏合剂"。本节开始时,我们先为序对定义基本的修改函数,以便序对能够作为构造可变数据对象的积木。这些修改函数极大地增强了序对的表示能力,使我们能够构造出 2.2 节中所用的序列和树之外的各种数据结构。我们还将给出一些模拟示例,其中复杂系统被建模为具有局部状态的对象集合。
(set-balance! ⟨account⟩ ⟨new-value⟩)