灯下 登录
计算机科学 / SICP / section 中英对照

3.5 Streams

We’ve gained a good understanding of assignment as a tool in modeling, as well

as an appreciation of the complex problems that assignment raises. It is time

to ask whether we could have gone about things in a different way, so as to

avoid some of these problems. In this section, we explore an alternative

approach to modeling state, based on data structures called

streams.

As we shall see, streams can mitigate some of the complexity of modeling state.

我们已经深入理解了赋值作为建模工具的意义,也充分领略了赋值所带来的复杂问题。现在是时候追问:我们能否以另一种方式处理问题,从而规避其中的一些麻烦?在本节中,我们探讨一种基于称为流 (streams) 的数据结构的替代性状态建模方法。我们将看到,流能够缓解状态建模的部分复杂性。

Let’s step back and review where this complexity comes from. In an attempt to

model real-world phenomena, we made some apparently reasonable decisions: We

modeled real-world objects with local state by computational objects with local

variables. We identified time variation in the real world with time variation

in the computer. We implemented the time variation of the states of the model

objects in the computer with assignments to the local variables of the model

objects.

让我们退一步,回顾这种复杂性从何而来。在尝试对现实世界现象建模时,我们做出了一些看似合理的决定:我们用带有局部变量的计算对象来建模带有局部状态的现实世界对象;我们将现实世界中的时间变化与计算机中的时间变化相对应;我们通过对模型对象的局部变量进行赋值,来实现计算机中模型对象状态的时间变化。

Is there another approach? Can we avoid identifying time in the computer with

time in the modeled world? Must we make the model change with time in order to

model phenomena in a changing world? Think about the issue in terms of

mathematical functions. We can describe the time-varying behavior of a

quantity x as a function of time x

(

t

). If we concentrate on x

instant by instant, we think of it as a changing quantity. Yet if we

concentrate on the entire time history of values, we do not emphasize

change—the function itself does not change.

还有没有别的方法?我们能否避免将计算机中的时间与被建模世界中的时间相对应?为了对一个不断变化的世界中的现象建模,模型本身是否必须随时间变化?不妨从数学函数的角度来思考这个问题。我们可以将量 x 随时间变化的行为描述为时间的函数 x(t)。如果我们逐时刻地关注 x,则将其视为一个变化的量;然而,如果我们着眼于值的整个时间历程,便不再强调变化——函数本身并不改变。

If time is measured in discrete steps, then we can model a time function as a

(possibly infinite) sequence. In this section, we will see how to model change

in terms of sequences that represent the time histories of the systems being

modeled. To accomplish this, we introduce new data structures called

如果时间以离散步骤计量,那么我们可以将时间函数建模为一个(可能是无穷的)序列。在本节中,我们将看到如何用序列来建模变化——这些序列表示被建模系统的时间历程。为此,我们引入称为

streams. From an abstract point of view, a stream is simply a

sequence. However, we will find that the straightforward implementation of

streams as lists (as in 2.2.1) doesn’t fully reveal the power of

stream processing. As an alternative, we introduce the technique of

流的新数据结构。从抽象的观点来看,流不过是一个序列。然而,我们将发现,像 2.2.1 节那样直接将流实现为表,并不能充分揭示流处理的能力。作为替代,我们引入一种称为

delayed evaluation, which enables us to represent very large (even

infinite) sequences as streams.

延迟求值 (delayed evaluation) 的技术,它使我们能够将非常大(甚至无穷)的序列表示为流。

Stream processing lets us model systems that have state without ever using

assignment or mutable data. This has important implications, both theoretical

and practical, because we can build models that avoid the drawbacks inherent in

introducing assignment. On the other hand, the stream framework raises

difficulties of its own, and the question of which modeling technique leads to

more modular and more easily maintained systems remains open.

流处理使我们能够在不使用赋值或可变数据的情况下,对具有状态的系统进行建模。这在理论和实践上都具有重要意义,因为我们可以构建出规避了引入赋值所固有的种种缺陷的模型。另一方面,流框架也引发了它自身的一些困难,至于哪种建模方式能带来更具模块性、更易维护的系统,这一问题至今仍悬而未决。