diff --git a/.gitignore b/.gitignore index 8f6c2b3..6e8fc52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ # python specific *.pyc __pycache__ +.idea/ +venv/ -## generic files to ignore +# generic files to ignore *~ *.lock *.DS_Store @@ -10,6 +12,7 @@ __pycache__ *.out */Thumbs.db +# generated html files [0-9]*.html coding-js diff --git a/content/1-0-abstractions.content.html b/content/1-0-abstractions.content.html index c4f3663..644fe45 100644 --- a/content/1-0-abstractions.content.html +++ b/content/1-0-abstractions.content.html @@ -3,18 +3,19 @@ @@ {{main_text}} - +up chevron - + right chevron

Building Abstractions with Procedures


-
The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three: 1. Combining several simple ideas into one compound one, and thus all complex ideas are made. 2. The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations. 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made. +
+ The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three: 1. Combining several simple ideas into one compound one, and thus all complex ideas are made. 2. The second is bringing two ideas, whether simple or complex, together, and setting them by one another so as to take a view of them at once, without uniting them into one, by which it gets all its ideas of relations. 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all its general ideas are made. -

John Locke, An Essay Concerning Human Understanding (1690) +

John Locke, An Essay Concerning Human Understanding (1690)

We are about to study the idea of a computational process. Computational processes are abstract beings that inhabit computers. As they evolve, processes manipulate other abstract things called data. The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes. In effect, we conjure the spirits of the computer with our spells. @@ -31,11 +32,11 @@

Programming in Lisp

We need an appropriate language for describing processes, and we will use for this purpose the programming language Lisp. Just as our everyday thoughts are usually expressed in our natural language (such as English, French, or Japanese), and descriptions of quantitative phenomena are expressed with mathematical notations, our procedural thoughts will be expressed in Lisp. Lisp was invented in the late 1950s as a formalism for reasoning about the use of certain kinds of logical expressions, called recursion equations, as a model for computation. The language was conceived by John McCarthy and is based on his paper "Recursive Functions of Symbolic Expressions and Their Computation by Machine" (McCarthy 1960). -

Despite its inception as a mathematical formalism, Lisp is a practical programming language. A Lisp interpreter is a machine that carries out processes described in the Lisp language. The first Lisp interpreter was implemented by McCarthy with the help of colleagues and students in the Artificial Intelligence Group of the MIT Research Laboratory of Electronics and in the MIT Computation Center.1 Lisp, whose name is an acronym for LISt Processing, was designed to provide symbol-manipulating capabilities for attacking programming problems such as the symbolic differentiation and integration of algebraic expressions. It included for this purpose new data objects known as atoms and lists, which most strikingly set it apart from all other languages of the period. +

Despite its inception as a mathematical formalism, Lisp is a practical programming language. A Lisp interpreter is a machine that carries out processes described in the Lisp language. The first Lisp interpreter was implemented by McCarthy with the help of colleagues and students in the Artificial Intelligence Group of the MIT Research Laboratory of Electronics and in the MIT Computation Center.1 Lisp, whose name is an acronym for LISt Processing, was designed to provide symbol-manipulating capabilities for attacking programming problems such as the symbolic differentiation and integration of algebraic expressions. It included for this purpose new data objects known as atoms and lists, which most strikingly set it apart from all other languages of the period. -

Lisp was not the product of a concerted design effort. Instead, it evolved informally in an experimental manner in response to users' needs and to pragmatic implementation considerations. Lisp's informal evolution has continued through the years, and the community of Lisp users has traditionally resisted attempts to promulgate any ``official'' definition of the language. This evolution, together with the flexibility and elegance of the initial conception, has enabled Lisp, which is the second oldest language in widespread use today (only Fortran is older), to continually adapt to encompass the most modern ideas about program design. Thus, Lisp is by now a family of dialects, which, while sharing most of the original features, may differ from one another in significant ways. The dialect of Lisp used in this book is called Scheme.2 +

Lisp was not the product of a concerted design effort. Instead, it evolved informally in an experimental manner in response to users' needs and to pragmatic implementation considerations. Lisp's informal evolution has continued through the years, and the community of Lisp users has traditionally resisted attempts to promulgate any ``official'' definition of the language. This evolution, together with the flexibility and elegance of the initial conception, has enabled Lisp, which is the second oldest language in widespread use today (only Fortran is older), to continually adapt to encompass the most modern ideas about program design. Thus, Lisp is by now a family of dialects, which, while sharing most of the original features, may differ from one another in significant ways. The dialect of Lisp used in this book is called Scheme.2 -

Because of its experimental character and its emphasis on symbol manipulation, Lisp was at first very inefficient for numerical computations, at least in comparison with Fortran. Over the years, however, Lisp compilers have been developed that translate programs into machine code that can perform numerical computations reasonably efficiently. And for special applications, Lisp has been used with great effectiveness.3 Although Lisp has not yet overcome its old reputation as hopelessly inefficient, Lisp is now used in many applications where efficiency is not the central concern. For example, Lisp has become a language of choice for operating-system shell languages and for extension languages for editors and computer-aided design systems. +

Because of its experimental character and its emphasis on symbol manipulation, Lisp was at first very inefficient for numerical computations, at least in comparison with Fortran. Over the years, however, Lisp compilers have been developed that translate programs into machine code that can perform numerical computations reasonably efficiently. And for special applications, Lisp has been used with great effectiveness.3 Although Lisp has not yet overcome its old reputation as hopelessly inefficient, Lisp is now used in many applications where efficiency is not the central concern. For example, Lisp has become a language of choice for operating-system shell languages and for extension languages for editors and computer-aided design systems.

If Lisp is not a mainstream language, why are we using it as the framework for our discussion of programming? Because the language possesses unique features that make it an excellent medium for studying important programming constructs and data structures and for relating them to the linguistic features that support them. The most significant of these features is the fact that Lisp descriptions of processes, called procedures, can themselves be represented and manipulated as Lisp data. The importance of this is that there are powerful program-design techniques that rely on the ability to blur the traditional distinction between ``passive'' data and ``active'' processes. As we shall discover, Lisp's flexibility in handling procedures as data makes it one of the most convenient languages in existence for exploring these techniques. The ability to represent procedures as data also makes Lisp an excellent language for writing programs that must manipulate other programs as data, such as the interpreters and compilers that support computer languages. Above and beyond these considerations, programming in Lisp is great fun. diff --git a/content/1-1-elements.content.html b/content/1-1-elements.content.html index 7e39e71..0cfb994 100644 --- a/content/1-1-elements.content.html +++ b/content/1-1-elements.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - +up chevron - - +left chevron +right chevron

The Elements of Programming

@@ -27,10 +27,9 @@

The Elements of Programming

In programming, we deal with two kinds of elements: procedures and data. (Later we will discover that they are really not so distinct.) Informally, data is “stuff ” that we want to manipulate, and procedures are descriptions of the rules for manipulating the data. Thus, any powerful programming language should be able to describe primitive data and primitive procedures and should have methods for combining and abstracting procedures and data. -

In this chapter we will deal only with simple numerical data so that we can focus on the rules for building procedures.4 In later chapters we will see that these same rules allow us to build procedures to manipulate compound data as well. +

In this chapter we will deal only with simple numerical data so that we can focus on the rules for building procedures.4 In later chapters we will see that these same rules allow us to build procedures to manipulate compound data as well. - -

1.1.1 Expressions

+

1.1.1 Expressions

One easy way to get started at programming is to examine some typical interactions with an interpreter for the Scheme dialect of Lisp. Imagine that you are sitting at a computer terminal. You type an expression, and the interpreter responds by displaying the result of its evaluating that expression. @@ -43,7 +42,7 @@

1.1.1 Expressions

C.make_editable("scheme-number"); -

the interpreter will respond by printing5 +

the interpreter will respond by printing5

-

Expressions representing numbers may be combined with an expression representing a primitive procedure (such as + or *) to form a compound expression that represents the application of the procedure to those numbers. For example: +

Expressions representing numbers may be combined with an expression representing a primitive procedure (such as + or *) to form a compound expression that represents the application of the procedure to those numbers. For example:

(+ 137 349) @@ -127,7 +126,7 @@

1.1.1 Expressions

C.prompt("scheme-long-tree"); -

which the interpreter would readily evaluate to be 57. We can help ourselves by writing such an expression in the form +

which the interpreter would readily evaluate to be 57. We can help ourselves by writing such an expression in the form

(+ (* 3 @@ -140,11 +139,11 @@

1.1.1 Expressions

C.prompt("scheme-long-tree-pp"); -

following a formatting convention known as pretty-printing, in which each long combination is written so that the operands are aligned vertically. The resulting indentations display clearly the structure of the expression.6 +

following a formatting convention known as pretty-printing, in which each long combination is written so that the operands are aligned vertically. The resulting indentations display clearly the structure of the expression.6 -

Even with complex expressions, the interpreter always operates in the same basic cycle: It reads an expression from the terminal, evaluates the expression, and prints the result. This mode of operation is often expressed by saying that the interpreter runs in a read-eval-print loop. Observe in particular that it is not necessary to explicitly instruct the interpreter to print the value of the expression.7 +

Even with complex expressions, the interpreter always operates in the same basic cycle: It reads an expression from the terminal, evaluates the expression, and prints the result. This mode of operation is often expressed by saying that the interpreter runs in a read-eval-print loop. Observe in particular that it is not necessary to explicitly instruct the interpreter to print the value of the expression.7 -

+

Exercise 1.1.1. Below is a sequence of expressions. What is the result printed by the interpreter in response to each expression? Assume that the sequence is to be evaluated in the order in which it is presented. @@ -194,7 +193,7 @@

1.1.1 Expressions

-

+

Exercise 1.2. Translate the following expression into prefix form. @@ -228,12 +227,11 @@

1.1.1 Expressions

- -

1.1.2 Naming and the Environment

+

1.1.2 Naming and the Environment

A critical aspect of a programming language is the means it provides for using names to refer to computational objects. We say that the name identifies a variable whose value is the object. -

In the Scheme dialect of Lisp, we name things with define. Typing +

In the Scheme dialect of Lisp, we name things with define. Typing

(define size 2) @@ -242,7 +240,7 @@

1.1.2 Naming and the Environment

C.prompt("scheme-define-size"); -

causes the interpreter to associate the value 2 with the name size.8 Once the name size has been associated with the number 2, we can refer to the value 2 by name: +

causes the interpreter to associate the value 2 with the name size.8 Once the name size has been associated with the number 2, we can refer to the value 2 by name:

size @@ -260,7 +258,7 @@

1.1.2 Naming and the Environment

C.prompt("scheme-times-size", ["scheme-define-size"]); -

Here are further examples of the use of define: +

Here are further examples of the use of define:

(define pi 3.14159) @@ -293,11 +291,11 @@

1.1.2 Naming and the Environment

C.prompt("scheme-circumference", ["scheme-define-circumference"]); -

define is our language's simplest means of abstraction, for it allows us to use simple names to refer to the results of compound operations, such as the circumference computed above. In general, computational objects may have very complex structures, and it would be extremely inconvenient to have to remember and repeat their details each time we want to use them. Indeed, complex programs are constructed by building, step by step, computational objects of increasing complexity. The interpreter makes this step-by-step program construction particularly convenient because name-object associations can be created incrementally in successive interactions. This feature encourages the incremental development and testing of programs and is largely responsible for the fact that a Lisp program usually consists of a large number of relatively simple procedures. +

define is our language's simplest means of abstraction, for it allows us to use simple names to refer to the results of compound operations, such as the circumference computed above. In general, computational objects may have very complex structures, and it would be extremely inconvenient to have to remember and repeat their details each time we want to use them. Indeed, complex programs are constructed by building, step by step, computational objects of increasing complexity. The interpreter makes this step-by-step program construction particularly convenient because name-object associations can be created incrementally in successive interactions. This feature encourages the incremental development and testing of programs and is largely responsible for the fact that a Lisp program usually consists of a large number of relatively simple procedures. -

It should be clear that the possibility of associating values with symbols and later retrieving them means that the interpreter must maintain some sort of memory that keeps track of the name-object pairs. This memory is called the environment (more precisely the global environment, since we will see later that a computation may involve a number of different environments).9 +

It should be clear that the possibility of associating values with symbols and later retrieving them means that the interpreter must maintain some sort of memory that keeps track of the name-object pairs. This memory is called the environment (more precisely the global environment, since we will see later that a computation may involve a number of different environments).9 -

-

Even this simple rule illustrates some important points about processes in general. First, observe that the first step dictates that in order to accomplish the evaluation process for a combination we must first perform the evaluation process on each element of the combination. Thus, the evaluation rule is recursive in nature; that is, it includes, as one of its steps, the need to invoke the rule itself.10 +

Even this simple rule illustrates some important points about processes in general. First, observe that the first step dictates that in order to accomplish the evaluation process for a combination we must first perform the evaluation process on each element of the combination. Thus, the evaluation rule is recursive in nature; that is, it includes, as one of its steps, the need to invoke the rule itself.10

Notice how succinctly the idea of recursion can be used to express what, in the case of a deeply nested combination, would otherwise be viewed as a rather complicated process. For example, evaluating @@ -441,7 +438,7 @@

1.1.3 Evaluating Combinations

$("#gv-out").html(svg); } if (typeof Viz == "undefined") { - $("#gv-out").html("

loading mdaines/viz.js") + $("#gv-out").html("

loading mdaines/viz.js") $.getScript('http://mdaines.github.com/viz.js/viz.js', update); } else { update(); @@ -459,7 +456,7 @@

1.1.3 Evaluating Combinations

requires that the evaluation rule be applied to four different combinations. We can obtain a picture of this process by representing the combination in the form of a tree, as shown in figure 1.1. -


+

the process represented in tree form

Figure 1.1 : Tree representation, showing the value of each subcombination.

Each combination is represented by a node with branches corresponding to the operator and the operands of the combination stemming from it. The terminal nodes (that is, nodes with no branches stemming from them) represent either operators or numbers. Viewing evaluation in terms of the tree, we can imagine that the values of the operands percolate upward, starting from the terminal nodes and then combining at higher and higher levels. In general, we shall see that recursion is a very powerful technique for dealing with hierarchical, treelike objects. In fact, the "percolate values upward" form of the evaluation rule is an example of a general kind of process known as tree accumulation. @@ -474,14 +471,13 @@

1.1.3 Evaluating Combinations

  • the values of other names are the objects associated with those names in the environment. -

    We may regard the second rule as a special case of the third one by stipulating that symbols such as + and * are also included in the global environment, and are associated with the sequences of machine instructions that are their "values." The key point to notice is the role of the environment in determining the meaning of the symbols in expressions. In an interactive language such as Lisp, it is meaningless to speak of the value of an expression such as (+ x 1) without specifying any information about the environment that would provide a meaning for the symbol x (or even for the symbol +). As we shall see in chapter 3, the general notion of the environment as providing a context in which evaluation takes place will play an important role in our understanding of program execution. +

    We may regard the second rule as a special case of the third one by stipulating that symbols such as + and * are also included in the global environment, and are associated with the sequences of machine instructions that are their "values." The key point to notice is the role of the environment in determining the meaning of the symbols in expressions. In an interactive language such as Lisp, it is meaningless to speak of the value of an expression such as (+ x 1) without specifying any information about the environment that would provide a meaning for the symbol x (or even for the symbol +). As we shall see in chapter 3, the general notion of the environment as providing a context in which evaluation takes place will play an important role in our understanding of program execution. -

    Notice that the evaluation rule given above does not handle definitions. For instance, evaluating (define x 3) does not apply define to two arguments, one of which is the value of the symbol x and the other of which is 3, since the purpose of the define is precisely to associate x with a value. (That is, (define x 3) is not a combination.) +

    Notice that the evaluation rule given above does not handle definitions. For instance, evaluating (define x 3) does not apply define to two arguments, one of which is the value of the symbol x and the other of which is 3, since the purpose of the define is precisely to associate x with a value. (That is, (define x 3) is not a combination.) -

    Such exceptions to the general evaluation rule are called special forms. Define is the only example of a special form that we have seen so far, but we will meet others shortly. Each special form has its own evaluation rule. The various kinds of expressions (each with its associated evaluation rule) constitute the syntax of the programming language. In comparison with most other programming languages, Lisp has a very simple syntax; that is, the evaluation rule for expressions can be described by a simple general rule together with specialized rules for a small number of special forms.11 +

    Such exceptions to the general evaluation rule are called special forms. Define is the only example of a special form that we have seen so far, but we will meet others shortly. Each special form has its own evaluation rule. The various kinds of expressions (each with its associated evaluation rule) constitute the syntax of the programming language. In comparison with most other programming languages, Lisp has a very simple syntax; that is, the evaluation rule for expressions can be described by a simple general rule together with specialized rules for a small number of special forms.11 - -

    1.1.4 Compound Procedures

    +

    1.1.4 Compound Procedures

    We have identified in Lisp some of the elements that must appear in any powerful programming language: @@ -513,7 +509,7 @@

    1.1.4 Compound Procedures

     To      square something, multiply   it by  itself.
    -

    We have here a compound procedure, which has been given the name square. The procedure represents the operation of multiplying something by itself. The thing to be multiplied is given a local name, x, which plays the same role that a pronoun plays in natural language. Evaluating the definition creates this compound procedure and associates it with the name square.12 +

    We have here a compound procedure, which has been given the name square. The procedure represents the operation of multiplying something by itself. The thing to be multiplied is given a local name, x, which plays the same role that a pronoun plays in natural language. Evaluating the definition creates this compound procedure and associates it with the name square.12

    The general form of a procedure definition is @@ -524,7 +520,7 @@

    1.1.4 Compound Procedures

    C.make_static("scheme-define-syntax"); -

    The <name> is a symbol to be associated with the procedure definition in the environment.13 The <formal parameters> are the names used within the body of the procedure to refer to the corresponding arguments of the procedure. The <body> is an expression that will yield the value of the procedure application when the formal parameters are replaced by the actual arguments to which the procedure is applied.14 The <name> and the <formal parameters> are grouped within parentheses, just as they would be in an actual call to the procedure being defined. +

    The <name> is a symbol to be associated with the procedure definition in the environment.13 The <formal parameters> are the names used within the body of the procedure to refer to the corresponding arguments of the procedure. The <body> is an expression that will yield the value of the procedure application when the formal parameters are replaced by the actual arguments to which the procedure is applied.14 The <name> and the <formal parameters> are grouped within parentheses, just as they would be in an actual call to the procedure being defined.

    Having defined square, we can now use it: @@ -592,12 +588,11 @@

    1.1.4 Compound Procedures

    C.prompt("scheme-f", ["scheme-define-sum-of-squares"]); -

    Compound procedures are used in exactly the same way as primitive procedures. Indeed, one could not tell by looking at the definition of sum-of-squares given above whether square was built into the interpreter, like + and *, or defined as a compound procedure. +

    Compound procedures are used in exactly the same way as primitive procedures. Indeed, one could not tell by looking at the definition of sum-of-squares given above whether square was built into the interpreter, like + and *, or defined as a compound procedure. - -

    1.1.5 The Substitution Model for Procedure Application

    +

    1.1.5 The Substitution Model for Procedure Application

    -

    To evaluate a combination whose operator names a compound procedure, the interpreter follows much the same process as for combinations whose operators name primitive procedures, which we described in section 1.1.3. That is, the interpreter evaluates the elements of the combination and applies the procedure (which is the value of the operator of the combination) to the arguments (which are the values of the operands of the combination). +

    To evaluate a combination whose operator names a compound procedure, the interpreter follows much the same process as for combinations whose operators name primitive procedures, which we described in section 1.1.3. That is, the interpreter evaluates the elements of the combination and applies the procedure (which is the value of the operator of the combination) to the arguments (which are the values of the operands of the combination).

    We can assume that the mechanism for applying primitive procedures to arguments is built into the interpreter. For compound procedures, the application process is as follows: @@ -612,7 +607,7 @@

    1.1.5 The Substitution Model for Procedure Application

    C.make_static("scheme-f-1"); -

    where f is the procedure defined in section 1.1.4. We begin by retrieving the body of f: +

    where f is the procedure defined in section 1.1.4. We begin by retrieving the body of f:

    (sum-of-squares (+ a 1) (* a 2)) @@ -621,7 +616,7 @@

    1.1.5 The Substitution Model for Procedure Application

    C.make_static("scheme-f-2"); -

    Then we replace the formal parameter a by the argument 5: +

    Then we replace the formal parameter a by the argument 5:

    (sum-of-squares (+ 5 1) (* 5 2)) @@ -630,7 +625,7 @@

    1.1.5 The Substitution Model for Procedure Application

    C.make_static("scheme-f-3"); -

    Thus the problem reduces to the evaluation of a combination with two operands and an operator sum-of-squares. Evaluating this combination involves three subproblems. We must evaluate the operator to get the procedure to be applied, and we must evaluate the operands to get the arguments. Now (+ 5 1) produces 6 and (* 5 2) produces 10, so we must apply the sum-of-squares procedure to 6 and 10. These values are substituted for the formal parameters x and y in the body of sum-of-squares, reducing the expression to +

    Thus the problem reduces to the evaluation of a combination with two operands and an operator sum-of-squares. Evaluating this combination involves three subproblems. We must evaluate the operator to get the procedure to be applied, and we must evaluate the operands to get the arguments. Now (+ 5 1) produces 6 and (* 5 2) produces 10, so we must apply the sum-of-squares procedure to 6 and 10. These values are substituted for the formal parameters x and y in the body of sum-of-squares, reducing the expression to

    (+ (square 6) (square 10)) @@ -674,13 +669,13 @@

    1.1.5 The Substitution Model for Procedure Application

    The purpose of the substitution is to help us think about procedure application, not to provide a description of how the interpreter really works. Typical interpreters do not evaluate procedure applications by manipulating the text of a procedure to substitute values for the formal parameters. In practice, the "substitution" is accomplished by using a local environment for the formal parameters. We will discuss this more fully in chapters 3 and 4 when we examine the implementation of an interpreter in detail.

  • -
  • Over the course of this book, we will present a sequence of increasingly elaborate models of how interpreters work, culminating with a complete implementation of an interpreter and compiler in chapter 5. The substitution model is only the first of these models — a way to get started thinking formally about the evaluation process. In general, when modeling phenomena in science and engineering, we begin with simplified, incomplete models. As we examine things in greater detail, these simple models become inadequate and must be replaced by more refined models. The substitution model is no exception. In particular, when we address in chapter 3 the use of procedures with "mutable data," we will see that the substitution model breaks down and must be replaced by a more complicated model of procedure application.15 +

  • Over the course of this book, we will present a sequence of increasingly elaborate models of how interpreters work, culminating with a complete implementation of an interpreter and compiler in chapter 5. The substitution model is only the first of these models — a way to get started thinking formally about the evaluation process. In general, when modeling phenomena in science and engineering, we begin with simplified, incomplete models. As we examine things in greater detail, these simple models become inadequate and must be replaced by more refined models. The substitution model is no exception. In particular, when we address in chapter 3 the use of procedures with "mutable data," we will see that the substitution model breaks down and must be replaced by a more complicated model of procedure application.15

  • Applicative order versus normal order

    -

    According to the description of evaluation given in section 1.1.3, the interpreter first evaluates the operator and operands and then applies the resulting procedure to the resulting arguments. This is not the only way to perform evaluation. An alternative evaluation model would not evaluate the operands until their values were needed. Instead it would first substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation. If we used this method, the evaluation of +

    According to the description of evaluation given in section 1.1.3, the interpreter first evaluates the operator and operands and then applies the resulting procedure to the resulting arguments. This is not the only way to perform evaluation. An alternative evaluation model would not evaluate the operands until their values were needed. Instead it would first substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation. If we used this method, the evaluation of

    (f 5) @@ -743,7 +738,7 @@

    Applicative order versus normal order

    C.make_static("scheme-f-7-n"); -

    This gives the same answer as our previous evaluation model, but the process is different. In particular, the evaluations of (+ 5 1) and (* 5 2) are each performed twice here, corresponding to the reduction of the expression +

    This gives the same answer as our previous evaluation model, but the process is different. In particular, the evaluations of (+ 5 1) and (* 5 2) are each performed twice here, corresponding to the reduction of the expression

    (* x x) @@ -752,14 +747,13 @@

    Applicative order versus normal order

    C.make_static("scheme-x"); -

    with x replaced respectively by (+ 5 1) and (* 5 2). +

    with x replaced respectively by (+ 5 1) and (* 5 2).

    This alternative "fully expand and then reduce" evaluation method is known as normal-order evaluation, in contrast to the "evaluate the arguments and then apply" method that the interpreter actually uses, which is called applicative-order evaluation. It can be shown that, for procedure applications that can be modeled using substitution (including all the procedures in the first two chapters of this book) and that yield legitimate values, normal-order and applicative-order evaluation produce the same value. (See exercise 1.5 for an instance of an "illegitimate" value where normal-order and applicative-order evaluation do not give the same result.) -

    Lisp uses applicative-order evaluation, partly because of the additional efficiency obtained from avoiding multiple evaluations of expressions such as those illustrated with (+ 5 1) and (* 5 2) above and, more significantly, because normal-order evaluation becomes much more complicated to deal with when we leave the realm of procedures that can be modeled by substitution. On the other hand, normal-order evaluation can be an extremely valuable tool, and we will investigate some of its implications in chapters 3 and 4.16 +

    Lisp uses applicative-order evaluation, partly because of the additional efficiency obtained from avoiding multiple evaluations of expressions such as those illustrated with (+ 5 1) and (* 5 2) above and, more significantly, because normal-order evaluation becomes much more complicated to deal with when we leave the realm of procedures that can be modeled by substitution. On the other hand, normal-order evaluation can be an extremely valuable tool, and we will investigate some of its implications in chapters 3 and 4.16 - -

    1.1.6 Conditional Expressions and Predicates

    +

    1.1.6 Conditional Expressions and Predicates

    The expressive power of the class of procedures that we can define at this point is very limited, because we have no way to make tests and to perform different operations depending on the result of a test. For instance, we cannot define a procedure that computes the absolute value of a number by testing whether the number is positive, negative, or zero and taking different actions in the different cases according to the rule @@ -793,11 +787,11 @@

    1.1.6 Conditional Expressions and Predicates

    C.no_output_frozen_prompt("scheme-cond-syntax"); -

    consisting of the symbol cond followed by parenthesized pairs of expressions (<p> <e>) called clauses. The first expression in each pair is a predicate — that is, an expression whose value is interpreted as either true or false.17 +

    consisting of the symbol cond followed by parenthesized pairs of expressions (<p> <e>) called clauses. The first expression in each pair is a predicate — that is, an expression whose value is interpreted as either true or false.17 -

    Conditional expressions are evaluated as follows. The predicate <p1> is evaluated first. If its value is false, then <p2> is evaluated. If <p2>'s value is also false, then <p3> is evaluated. This process continues until a predicate is found whose value is true, in which case the interpreter returns the value of the corresponding consequent expression <e> of the clause as the value of the conditional expression. If none of the <p>'s is found to be true, the value of the cond is undefined. +

    Conditional expressions are evaluated as follows. The predicate <p1> is evaluated first. If its value is false, then <p2> is evaluated. If <p2>'s value is also false, then <p3> is evaluated. This process continues until a predicate is found whose value is true, in which case the interpreter returns the value of the corresponding consequent expression <e> of the clause as the value of the conditional expression. If none of the <p>'s is found to be true, the value of the cond is undefined. -

    The word predicate is used for procedures that return true or false, as well as for expressions that evaluate to true or false. The absolute-value procedure abs makes use of the primitive predicates >, <, and =.18 These take two numbers as arguments and test whether the first number is, respectively, greater than, less than, or equal to the second number, returning true or false accordingly. +

    The word predicate is used for procedures that return true or false, as well as for expressions that evaluate to true or false. The absolute-value procedure abs makes use of the primitive predicates >, <, and =.18 These take two numbers as arguments and test whether the first number is, respectively, greater than, less than, or equal to the second number, returning true or false accordingly.

    Another way to write the absolute-value procedure is @@ -808,7 +802,7 @@

    1.1.6 Conditional Expressions and Predicates

    -

    which could be expressed in English as "If $x$ is less than zero return $-x$; otherwise return $x$." else is a special symbol that can be used in place of the <p> in the final clause of a cond. This causes the cond to return as its value the value of the corresponding <e> whenever all previous clauses have been bypassed. In fact, any expression that always evaluates to a true value could be used as the <p> here. +

    which could be expressed in English as "If $x$ is less than zero return $-x$; otherwise return $x$." else is a special symbol that can be used in place of the <p> in the final clause of a cond. This causes the cond to return as its value the value of the corresponding <e> whenever all previous clauses have been bypassed. In fact, any expression that always evaluates to a true value could be used as the <p> here.

    Here is yet another way to write the absolute-value procedure:

    @@ -827,29 +821,29 @@

    1.1.6 Conditional Expressions and Predicates

    -

    To evaluate an if expression, the interpreter starts by evaluating the <predicate> part of the expression. If the <predicate> evaluates to a true value, the interpreter then evaluates the <consequent> and returns its value. Otherwise it evaluates the <alternative> and returns its value.19 +

    To evaluate an if expression, the interpreter starts by evaluating the <predicate> part of the expression. If the <predicate> evaluates to a true value, the interpreter then evaluates the <consequent> and returns its value. Otherwise it evaluates the <alternative> and returns its value.19 -

    In addition to primitive predicates such as <, =, and >, there are logical composition operations, which enable us to construct compound predicates. The three most frequently used are these: +

    In addition to primitive predicates such as <, =, and >, there are logical composition operations, which enable us to construct compound predicates. The three most frequently used are these:

    @@ -880,7 +874,7 @@

    1.1.6 Conditional Expressions and Predicates

    -

    +

    Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. @@ -934,7 +928,7 @@

    1.1.6 Conditional Expressions and Predicates

    -

    +

    Exercise 1.4. Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure: @@ -948,9 +942,9 @@

    1.1.6 Conditional Expressions and Predicates

    - -

    1.1.7 Example: Square Roots by Newton's Method

    +

    1.1.7 Example: Square Roots by Newton's Method

    Procedures, as introduced above, are much like ordinary mathematical functions. They specify a value that is determined by one or more parameters. But there is an important difference between mathematical functions and computer procedures. Procedures must be effective. @@ -1028,10 +1021,10 @@

    1.1.7 Example: Square Roots by Newton's Method

    This only begs the question. -

    The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge. In mathematics we are usually concerned with declarative (what is) descriptions, whereas in computer science we are usually concerned with imperative (how to) descriptions.20 +

    The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge. In mathematics we are usually concerned with declarative (what is) descriptions, whereas in computer science we are usually concerned with imperative (how to) descriptions.20

    How does one compute square roots? The most common way is to use Newton's method of successive approximations, which says that whenever we have a guess $y$ for the value of the square root of a number $x$, we can perform a simple manipulation to get a better guess (one closer to the actual square root) by averaging $y$ with -$\frac{x}{y}$.21 For example, we can compute the square root of 2 as follows. Suppose our initial guess is $1$:

    +$\frac{x}{y}$.21 For example, we can compute the square root of 2 as follows. Suppose our initial guess is $1$:

    \begin{array}{c|c|c} \text{Guess} & \text{Quotient} & \text{Average} \\ @@ -1077,17 +1070,17 @@

    1.1.7 Example: Square Roots by Newton's Method

    C.prompt("scheme-define-average"); -

    We also have to say what we mean by "good enough." The following will do for illustration, but it is not really a very good test. (See exercise 1.7.) The idea is to improve the answer until it is close enough so that its square differs from the radicand by less than a predetermined tolerance (here 0.001):22 +

    We also have to say what we mean by "good enough." The following will do for illustration, but it is not really a very good test. (See exercise 1.7.) The idea is to improve the answer until it is close enough so that its square differs from the radicand by less than a predetermined tolerance (here 0.001):22

    (define (good-enough? guess x) - (< (abs (- (square guess) x)) 0.001)) + (< (abs (- (square guess) x)) 0.001))
    -

    Finally, we need a way to get started. For instance, we can always guess that the square root of any number is 1:23 +

    Finally, we need a way to get started. For instance, we can always guess that the square root of any number is 1:23

    (define (sqrt x) @@ -1133,11 +1126,10 @@

    1.1.7 Example: Square Roots by Newton's Method

    C.prompt("scheme-square-sqrt", ["scheme-define-square", "scheme-define-sqrt"]); -

    The sqrt program also illustrates that the simple procedural language we have introduced so far is sufficient for writing any purely numerical program that one could write in, say, C or Pascal. This might seem surprising, since we have not included in our language any iterative (looping) constructs that direct the computer to do something over and over again. Sqrt-iter, on the other hand, demonstrates how iteration can be accomplished using no special construct other than the ordinary ability to call a procedure.24 +

    The sqrt program also illustrates that the simple procedural language we have introduced so far is sufficient for writing any purely numerical program that one could write in, say, C or Pascal. This might seem surprising, since we have not included in our language any iterative (looping) constructs that direct the computer to do something over and over again. Sqrt-iter, on the other hand, demonstrates how iteration can be accomplished using no special construct other than the ordinary ability to call a procedure.24 -

    - -

    Exercise 1.6. Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if: +

    +

    Exercise 1.6. Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version of if:

    (define (new-if predicate then-clause else-clause) @@ -1182,16 +1174,16 @@

    1.1.7 Example: Square Roots by Newton's Method

    ["Infinite loop: sqrt-iter always evaluates itself"], ["It works as before", "Infinite loop: cond evaluates all its arguments", - "Error: only if can perform conditional evaluations" + "Error: only if can perform conditional evaluations" ]);

    -

    -

    Exercise 1.7. The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. +

    +

    Exercise 1.7. The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. -

    An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers? +

    An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?

    (define (my-sqrt x) @@ -1231,7 +1223,7 @@

    1.1.7 Example: Square Roots by Newton's Method

    2)) (define (good-enough? guess previous-guess) - (< (abs (- guess previous-guess)) + (< (abs (- guess previous-guess)) 0.001)) (define (improve guess x) @@ -1252,15 +1244,14 @@

    1.1.7 Example: Square Roots by Newton's Method

    -

    - +

    Exercise 1.8. Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value $$ \frac{x/y^2 + 2y}{3} $$ -

    Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In Section 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square root and cube-root procedures.) +

    Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In Section 1.3.4 we will see how to implement Newton’s method in general as an abstraction of these square root and cube-root procedures.)

    (define (cube-root x) @@ -1284,7 +1275,7 @@

    1.1.7 Example: Square Roots by Newton's Method

    (* x x x)) (define (good-enough? guess x) - (< (abs (- (cube guess) x)) 0.001)) + (< (abs (- (cube guess) x)) 0.001)) (define (improve guess x) (/ (+ (/ x (* guess guess)) @@ -1302,21 +1293,20 @@

    1.1.7 Example: Square Roots by Newton's Method

    - -

    1.1.8 Procedures as Black-Box Abstractions

    +

    1.1.8 Procedures as Black-Box Abstractions

    Sqrt is our first example of a process defined by a set of mutually defined procedures. Notice that the definition of sqrt-iter is recursive; that is, the procedure is defined in terms of itself. The idea of being able to define a procedure in terms of itself may be disturbing; it may seem unclear how such a “circular” definition could make sense at all, much less specify a well-defined process to be carried out by a computer. This will be addressed more carefully in section 1.2. But first let's consider some other important points illustrated by the sqrt example.

    Observe that the problem of computing square roots breaks up naturally into a number of subproblems: how to tell whether a guess is good enough, how to improve a guess, and so on. Each of these tasks is accomplished by a separate procedure. The entire sqrt program can be viewed as a cluster of procedures (shown in figure 1.2) that mirrors the decomposition of the problem into subproblems.

    - -

    Figure 1.2: Procedural decomposition of the sqrt program. +the process drawn in tree form +

    Figure 1.2: Procedural decomposition of the sqrt program.

    The importance of this decomposition strategy is not simply that one is dividing the program into parts. After all, we could take any large program and divide it into parts — the first ten lines, the next ten lines, the next ten lines, and so on. Rather, it is crucial that each procedure accomplishes an identifiable task that can be used as a module in defining other procedures. For example, when we define the good-enough? procedure in terms of square, we are able to regard the square procedure as a “black box.” We are not at that moment concerned with how the procedure computes its result, only with the fact that it computes the square. The details of how the square is computed can be suppressed, to be considered at a later time. Indeed, as far as the good-enough? procedure is concerned, square is not quite a procedure but rather an abstraction of a procedure, a so-called procedural abstraction. At this level of abstraction, any procedure that computes the square is equally good. -

    Thus, considering only the values they return, the following two procedures for squaring a number should be indistinguishable. Each takes a numerical argument and produces the square of that number as the value.25 +

    Thus, considering only the values they return, the following two procedures for squaring a number should be indistinguishable. Each takes a numerical argument and produces the square of that number as the value.25

    (define (square x) (* x x)) @@ -1377,9 +1367,9 @@

    Local names

    If the parameters were not local to the bodies of their respective procedures, then the parameter x in square could be confused with the parameter x in good-enough?, and the behavior of good-enough? would depend upon which version of square we used. Thus, square would not be the black box we desired. -

    A formal parameter of a procedure has a very special role in the procedure definition, in that it doesn't matter what name the formal parameter has. Such a name is called a bound variable, and we say that the procedure definition binds its formal parameters. The meaning of a procedure definition is unchanged if a bound variable is consistently renamed throughout the definition.26 If a variable is not bound, we say that it is free. The set of expressions for which a binding defines a name is called the scope of that name. In a procedure definition, the bound variables declared as the formal parameters of the procedure have the body of the procedure as their scope. +

    A formal parameter of a procedure has a very special role in the procedure definition, in that it doesn't matter what name the formal parameter has. Such a name is called a bound variable, and we say that the procedure definition binds its formal parameters. The meaning of a procedure definition is unchanged if a bound variable is consistently renamed throughout the definition.26 If a variable is not bound, we say that it is free. The set of expressions for which a binding defines a name is called the scope of that name. In a procedure definition, the bound variables declared as the formal parameters of the procedure have the body of the procedure as their scope. -

    In the definition of good-enough? above, guess and x are bound variables but <, -, abs, and square are free. The meaning of good-enough? should be independent of the names we choose for guess and x so long as they are distinct and different from <, -, abs, and square. (If we renamed guess to abs we would have introduced a bug by capturing the variable abs. It would have changed from free to bound.) The meaning of good-enough? is not independent of the names of its free variables, however. It surely depends upon the fact (external to this definition) that the symbol abs names a procedure for computing the absolute value of a number. Good-enough? will compute a different function if we substitute cos for abs in its definition. +

    In the definition of good-enough? above, guess and x are bound variables but <, -, abs, and square are free. The meaning of good-enough? should be independent of the names we choose for guess and x so long as they are distinct and different from <, -, abs, and square. (If we renamed guess to abs we would have introduced a bug by capturing the variable abs. It would have changed from free to bound.) The meaning of good-enough? is not independent of the names of its free variables, however. It surely depends upon the fact (external to this definition) that the symbol abs names a procedure for computing the absolute value of a number. Good-enough? will compute a different function if we substitute cos for abs in its definition.

    Internal definitions and block structure

    @@ -1395,7 +1385,7 @@

    Internal definitions and block structure

    (sqrt-iter (improve guess x) x))) (define (good-enough? guess x) - (< (abs (- (square guess) x)) 0.001)) + (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) @@ -1409,7 +1399,7 @@

    Internal definitions and block structure

    (define (sqrt x) (define (good-enough? guess x) - (< (abs (- (square guess) x)) 0.001)) + (< (abs (- (square guess) x)) 0.001)) (define (improve guess x) (average guess (/ x guess))) (define (sqrt-iter guess x) @@ -1422,12 +1412,12 @@

    Internal definitions and block structure

    C.prompt("scheme-define-sqrt-block", ["scheme-define-square", "scheme-define-average"]); -

    Such nesting of definitions, called block structure, is basically the right solution to the simplest name-packaging problem. But there is a better idea lurking here. In addition to internalizing the definitions of the auxiliary procedures, we can simplify them. Since x is bound in the definition of sqrt, the procedures good-enough?, improve, and sqrt-iter, which are defined internally to sqrt, are in the scope of x. Thus, it is not necessary to pass x explicitly to each of these procedures. Instead, we allow x to be a free variable in the internal definitions, as shown below. Then x gets its value from the argument with which the enclosing procedure sqrt is called. This discipline is called lexical scoping.27 +

    Such nesting of definitions, called block structure, is basically the right solution to the simplest name-packaging problem. But there is a better idea lurking here. In addition to internalizing the definitions of the auxiliary procedures, we can simplify them. Since x is bound in the definition of sqrt, the procedures good-enough?, improve, and sqrt-iter, which are defined internally to sqrt, are in the scope of x. Thus, it is not necessary to pass x explicitly to each of these procedures. Instead, we allow x to be a free variable in the internal definitions, as shown below. Then x gets its value from the argument with which the enclosing procedure sqrt is called. This discipline is called lexical scoping.27

    (define (sqrt x) (define (good-enough? guess) - (< (abs (- (square guess) x)) 0.001)) + (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) @@ -1440,7 +1430,7 @@

    Internal definitions and block structure

    C.prompt("scheme-define-sqrt-no-x", ["scheme-define-square", "scheme-define-average"]); -

    We will use block structure extensively to help us break up large programs into tractable pieces.28 The idea of block structure originated with the programming language Algol 60. It appears in most advanced programming languages and is an important tool for helping to organize the construction of large programs. +

    We will use block structure extensively to help us break up large programs into tractable pieces.28 The idea of block structure originated with the programming language Algol 60. It appears in most advanced programming languages and is an important tool for helping to organize the construction of large programs. @@ {{footnotes}} @@ -1475,7 +1465,7 @@

    Internal definitions and block structure

    10 - It may seem strange that the evaluation rule says, as part of the first step, that we should evaluate the leftmost element of a combination, since at this point that can only be an operator such as + or * representing a built-in primitive procedure such as addition or multiplication. We will see later that it is useful to be able to work with combinations whose operators are themselves compound expressions. + It may seem strange that the evaluation rule says, as part of the first step, that we should evaluate the leftmost element of a combination, since at this point that can only be an operator such as + or * representing a built-in primitive procedure such as addition or multiplication. We will see later that it is useful to be able to work with combinations whose operators are themselves compound expressions.

    @@ -1485,7 +1475,7 @@

    Internal definitions and block structure

    12 - Observe that there are two different operations being combined here: we are creating the procedure, and we are giving it the name square. It is possible, indeed important, to be able to separate these two notions — to create procedures without naming them, and to give names to procedures that have already been created. We will see how to do this in section 1.3.2. + Observe that there are two different operations being combined here: we are creating the procedure, and we are giving it the name square. It is possible, indeed important, to be able to separate these two notions — to create procedures without naming them, and to give names to procedures that have already been created. We will see how to do this in section 1.3.2.

    @@ -1505,21 +1495,21 @@

    Internal definitions and block structure

    16 - In chapter 3 we will introduce stream processing, which is a way of handling apparently “infinite” data structures by incorporating a limited form of normal-order evaluation. In section 4.2 we will modify the Scheme interpreter to produce a normal-order variant of Scheme. + In chapter 3 we will introduce stream processing, which is a way of handling apparently “infinite” data structures by incorporating a limited form of normal-order evaluation. In section 4.2 we will modify the Scheme interpreter to produce a normal-order variant of Scheme.

    17 - “Interpreted as either true or false” means this: In Scheme, there are two distinguished values that are denoted by the constants #t and #f. When the interpreter checks a predicate's value, it interprets #f as false. Any other value is treated as true. (Thus, providing #t is logically unnecessary, but it is convenient.) In this book we will use names true and false, which are associated with the values #t and #f respectively. + “Interpreted as either true or false” means this: In Scheme, there are two distinguished values that are denoted by the constants #t and #f. When the interpreter checks a predicate's value, it interprets #f as false. Any other value is treated as true. (Thus, providing #t is logically unnecessary, but it is convenient.) In this book we will use names true and false, which are associated with the values #t and #f respectively.

    -

    18 abs also uses the “minus” operator -, which, when used with a single operand, as in (- x), indicates negation. +

    18 abs also uses the “minus” operator -, which, when used with a single operand, as in (- x), indicates negation.

    19 - A minor difference between if and cond is that the <e> part of each cond clause may be a sequence of expressions. If the corresponding <p> is found to be true, the expressions <e> are evaluated in sequence and the value of the final expression in the sequence is returned as the value of the cond. In an if expression, however, the <consequent> and <alternative> must be single expressions. + A minor difference between if and cond is that the <e> part of each cond clause may be a sequence of expressions. If the corresponding <p> is found to be true, the expressions <e> are evaluated in sequence and the value of the final expression in the sequence is returned as the value of the cond. In an if expression, however, the <consequent> and <alternative> must be single expressions.

    @@ -1529,7 +1519,7 @@

    Internal definitions and block structure

    21 - This square-root algorithm is actually a special case of Newton's method, which is a general technique for finding roots of equations. The square-root algorithm itself was developed by Heron of Alexandria in the first century A.D. We will see how to express the general Newton's method as a Lisp procedure in section 1.3.4. + This square-root algorithm is actually a special case of Newton's method, which is a general technique for finding roots of equations. The square-root algorithm itself was developed by Heron of Alexandria in the first century AD. We will see how to express the general Newton's method as a Lisp procedure in section 1.3.4.

    @@ -1539,12 +1529,12 @@

    Internal definitions and block structure

    23 - Observe that we express our initial guess as $1.0$ rather than $1$. This would not make any difference in many Lisp implementations. MIT Scheme, however, distinguishes between exact integers and decimal values, and dividing two integers produces a rational number rather than a decimal. For example, dividing $10$ by $6$ yields $5/3$, while dividing $10.0$ by $6.0$ yields $1.6666666666666667$. (We will learn how to implement arithmetic on rational numbers in section 2.1.1.) If we start with an initial guess of $1$ in our square-root program, and $x$ is an exact integer, all subsequent values produced in the square-root computation will be rational numbers rather than decimals. Mixed operations on rational numbers and decimals always yield decimals, so starting with an initial guess of $1.0$ forces all subsequent values to be decimals. + Observe that we express our initial guess as $1.0$ rather than $1$. This would not make any difference in many Lisp implementations. MIT Scheme, however, distinguishes between exact integers and decimal values, and dividing two integers produces a rational number rather than a decimal. For example, dividing $10$ by $6$ yields $5/3$, while dividing $10.0$ by $6.0$ yields $1.6666666666666667$. (We will learn how to implement arithmetic on rational numbers in section 2.1.1.) If we start with an initial guess of $1$ in our square-root program, and $x$ is an exact integer, all subsequent values produced in the square-root computation will be rational numbers rather than decimals. Mixed operations on rational numbers and decimals always yield decimals, so starting with an initial guess of $1.0$ forces all subsequent values to be decimals.

    24 - Readers who are worried about the efficiency issues involved in using procedure calls to implement iteration should note the remarks on “tail recursion” in section 1.2.1. + Readers who are worried about the efficiency issues involved in using procedure calls to implement iteration should note the remarks on “tail recursion” in section 1.2.1.

    diff --git a/content/1-2-procedures.content.html b/content/1-2-procedures.content.html index d36c7c0..74e4d46 100644 --- a/content/1-2-procedures.content.html +++ b/content/1-2-procedures.content.html @@ -4,10 +4,10 @@ {{main_text}} - + - - + +

    Procedures and the Processes They Generate

    @@ -22,8 +22,7 @@

    Procedures and the Processes They Generate

    In this section we will examine some common “shapes” for processes generated by simple procedures. We will also investigate the rates at which these processes consume the important computational resources of time and space. The procedures we will consider are very simple. Their role is like that played by test patterns in photography: as oversimplified prototypical patterns, rather than practical examples in their own right. - -

    1.2.1 Linear Recursion and Iteration

    +

    1.2.1 Linear Recursion and Iteration

    We begin by considering the factorial function, defined by @@ -50,9 +49,9 @@

    1.2.1 Linear Recursion and Iteration

    C.prompt("scheme-define-factorial"); -

    We can use the substitution model of section 1.1.5 to watch this procedure in action computing 6!, as shown in figure 1.3. +

    We can use the substitution model of section 1.1.5 to watch this procedure in action computing 6!, as shown in figure 1.3. -


    +


    Figure 1.3: A linear recursive process for computing $6!$. @@ -69,7 +68,7 @@

    1.2.1 Linear Recursion and Iteration

    and stipulating that $n!$ is the value of the product when the counter exceeds $n$. -

    Once again, we can recast our description as a procedure for computing factorials:29 +

    Once again, we can recast our description as a procedure for computing factorials:29

    (define (factorial n) @@ -88,7 +87,7 @@

    1.2.1 Linear Recursion and Iteration

    As before, we can use the substitution model to visualize the process of computing $6!$, as shown in figure 1.4. -


    +


    Figure 1.4: A linear iterative process for computing $6!$. @@ -98,14 +97,13 @@

    1.2.1 Linear Recursion and Iteration

    By contrast, the second process does not grow and shrink. At each step, all we need to keep track of, for any $n$, are the current values of the variables product, counter, and max-count. We call this an iterative process. In general, an iterative process is one whose state can be summarized by a fixed number of state variables, together with a fixed rule that describes how the state variables should be updated as the process moves from state to state and an (optional) end test that specifies conditions under which the process should terminate. In computing $n!$, the number of steps required grows linearly with $n$. Such a process is called a linear iterative process. -

    The contrast between the two processes can be seen in another way. In the iterative case, the program variables provide a complete description of the state of the process at any point. If we stopped the computation between steps, all we would need to do to resume the computation is to supply the interpreter with the values of the three program variables. Not so with the recursive process. In this case there is some additional “hidden” information, maintained by the interpreter and not contained in the program variables, which indicates “where the process is” in negotiating the chain of deferred operations. The longer the chain, the more information must be maintained.30 +

    The contrast between the two processes can be seen in another way. In the iterative case, the program variables provide a complete description of the state of the process at any point. If we stopped the computation between steps, all we would need to do to resume the computation is to supply the interpreter with the values of the three program variables. Not so with the recursive process. In this case there is some additional “hidden” information, maintained by the interpreter and not contained in the program variables, which indicates “where the process is” in negotiating the chain of deferred operations. The longer the chain, the more information must be maintained.30

    In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure. When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers (either directly or indirectly) to the procedure itself. But when we describe a process as following a pattern that is, say, linearly recursive, we are speaking about how the process evolves, not about the syntax of how a procedure is written. It may seem disturbing that we refer to a recursive procedure such as fact-iter as generating an iterative process. However, the process really is iterative: Its state is captured completely by its three state variables, and an interpreter need keep track of only three variables in order to execute the process. -

    One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose “looping constructs” such as do, repeat, until, for, and while. The implementation of Scheme we shall consider in chapter 5 does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tail-recursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar.31 - -

    +

    One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose “looping constructs” such as do, repeat, until, for, and while. The implementation of Scheme we shall consider in chapter 5 does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tail-recursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar.31 +

    Exercise 1.9. Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.

    @@ -144,7 +142,7 @@

    1.2.1 Linear Recursion and Iteration

    C.add_dep("scheme-define-plus-dec-inc", ["scheme-define-inc-dec"]); -

    Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive? +

    Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

    -

    Consider the following procedures, where A is the procedure defined above: +

    Consider the following procedures, where A is the procedure defined above:

    (define (f n) (A 0 n)) @@ -221,12 +218,11 @@

    1.2.1 Linear Recursion and Iteration

    C.make_static("scheme-define-5-n-squared"); -

    Give concise mathematical definitions for the functions computed by the procedures $f$, $g$, and $h$ for positive integer values of $n$. For example, (k n) computes $5n^2$. +

    Give concise mathematical definitions for the functions computed by the procedures $f$, $g$, and $h$ for positive integer values of $n$. For example, (k n) computes $5n^2$.

    - -

    1.2.2 Tree Recursion

    +

    1.2.2 Tree Recursion

    Another common pattern of computation is called tree recursion. As an example, consider computing the sequence of Fibonacci numbers, in which each number is the sum of the preceding two: @@ -304,7 +300,7 @@

    1.2.2 Tree Recursion

    This second method for computing Fib(n) is a linear iteration. The difference in number of steps required by the two methods — one linear in n, one growing as fast as Fib(n) itself — is enormous, even for small inputs. -

    One should not conclude from this that tree-recursive processes are useless. When we consider processes that operate on hierarchically structured data rather than numbers, we will find that tree recursion is a natural and powerful tool.32 But even in numerical operations, tree-recursive processes can be useful in helping us to understand and design programs. For instance, although the first fib procedure is much less efficient than the second one, it is more straightforward, being little more than a translation into Lisp of the definition of the Fibonacci sequence. To formulate the iterative algorithm required noticing that the computation could be recast as an iteration with three state variables. +

    One should not conclude from this that tree-recursive processes are useless. When we consider processes that operate on hierarchically structured data rather than numbers, we will find that tree recursion is a natural and powerful tool.32 But even in numerical operations, tree-recursive processes can be useful in helping us to understand and design programs. For instance, although the first fib procedure is much less efficient than the second one, it is more straightforward, being little more than a translation into Lisp of the definition of the Fibonacci sequence. To formulate the iterative algorithm required noticing that the computation could be recast as an iteration with three state variables.

    Example: Counting change

    @@ -324,7 +320,7 @@

    Example: Counting change

    To see why this is true, observe that the ways to make change can be divided into two groups: those that do not use any of the first kind of coin, and those that do. Therefore, the total number of ways to make change for some amount is equal to the number of ways to make change for the amount without using any of the first kind of coin, plus the number of ways to make change assuming that we do use the first kind of coin. But the latter number is equal to the number of ways to make change for the amount that remains after using a coin of the first kind. -

    Thus, we can recursively reduce the problem of changing a given amount to the problem of changing smaller amounts using fewer kinds of coins. Consider this reduction rule carefully, and convince yourself that we can use it to describe an algorithm if we specify the following degenerate cases:33 +

    Thus, we can recursively reduce the problem of changing a given amount to the problem of changing smaller amounts using fewer kinds of coins. Consider this reduction rule carefully, and convince yourself that we can use it to describe an algorithm if we specify the following degenerate cases:33

      @@ -370,10 +366,10 @@

      Example: Counting change

      C.add_dep("scheme-count-change-100", ["scheme-define-count-change"]); -

      Count-change generates a tree-recursive process with redundancies similar to those in our first implementation of fib. (It will take quite a while for that 292 to be computed.) On the other hand, it is not obvious how to design a better algorithm for computing the result, and we leave this problem as a challenge. The observation that a tree-recursive process may be highly inefficient but often easy to specify and understand has led people to propose that one could get the best of both worlds by designing a “smart compiler” that could transform tree-recursive procedures into more efficient procedures that compute the same result.34 +

      Count-change generates a tree-recursive process with redundancies similar to those in our first implementation of fib. (It will take quite a while for that 292 to be computed.) On the other hand, it is not obvious how to design a better algorithm for computing the result, and we leave this problem as a challenge. The observation that a tree-recursive process may be highly inefficient but often easy to specify and understand has led people to propose that one could get the best of both worlds by designing a “smart compiler” that could transform tree-recursive procedures into more efficient procedures that compute the same result.34 -

      +

      Exercise 1.11. A function $f$ is defined by the rule that $f(n) = n$ if $n<3$ and $f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3)$ if $n > 3$. Write a procedure that computes $f$ by means of a recursive process.

      Write a procedure that computes f by means of an iterative process. @@ -425,14 +421,14 @@

      Example: Counting change

      -

      +

      Exercise 1.12. The following pattern of numbers is called Pascal's triangle.

      -

      The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 +

      The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 -

      Write a procedure that computes elements of Pascal's triangle by means of a recursive process. If a number lies outside of the triangle, return 0 (this makes sense if we view pascal as the combination function ). Start counting rows and columns from 0. +

      Write a procedure that computes elements of Pascal's triangle by means of a recursive process. If a number lies outside of the triangle, return 0 (this makes sense if we view pascal as the combination function ). Start counting rows and columns from 0.

      @@ -487,7 +483,7 @@

      Example: Counting change

      -

      +

      Exercise 1.13. Prove that $\Fib(n)$ is the closest integer to $\phi^n / \sqrt 5$, where $$ @@ -500,7 +496,7 @@

      Example: Counting change

      \psi = \frac{1 - \sqrt 5}{2} = 1 - \phi = -\frac{1}{\phi} \approx -0.6180339887 $$ -

      Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that +

      Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that $$ \Fib(n) = \frac{\phi^n - \psi^n}{\phi - \psi} = \frac{\phi^n - \psi^n}{\sqrt 5} @@ -508,8 +504,7 @@

      Example: Counting change

      - -

      1.2.3 Orders of Growth

      +

      1.2.3 Orders of Growth

      The previous examples illustrate that processes can differ considerably in the rates at which they consume computational resources. One convenient way to describe this difference is to use the notion of order of growth to obtain a gross measure of the resources required by a process as the inputs become larger. @@ -523,17 +518,17 @@

      1.2.3 Orders of Growth

      for any sufficiently large value of n. (In other words, for large n, the value R(n) is sandwiched between $k_1 f(n)$ and $k_2f(n)$.) -

      For instance, with the linear recursive process for computing factorial described in section 1.2.1 the number of steps grows proportionally to the input $n$. Thus, the steps required for this process grows as $\Theta(n)$. We also saw that the space required grows as $\Theta(n)$. For the iterative factorial, the number of steps is still $\Theta(n)$ but the space is $\Theta(1)$ — that is, constant.36 The tree-recursive Fibonacci computation requires $\Theta(\phi^n)$ steps and space $\Theta(n)$, where $\phi$ is the golden ratio described in section 1.2.2. +

      For instance, with the linear recursive process for computing factorial described in section 1.2.1 the number of steps grows proportionally to the input $n$. Thus, the steps required for this process grows as $\Theta(n)$. We also saw that the space required grows as $\Theta(n)$. For the iterative factorial, the number of steps is still $\Theta(n)$ but the space is $\Theta(1)$ — that is, constant.36 The tree-recursive Fibonacci computation requires $\Theta(\phi^n)$ steps and space $\Theta(n)$, where $\phi$ is the golden ratio described in section 1.2.2.

      Orders of growth provide only a crude description of the behavior of a process. For example, a process requiring $n^2$ steps and a process requiring $1000n^2$ steps and a process requiring $3n^2 + 10n + 17$ steps all have $\Theta(n^2)$ order of growth. On the other hand, order of growth provides a useful indication of how we may expect the behavior of the process to change as we change the size of the problem. For a $\Theta(n)$ (linear) process, doubling the size will roughly double the amount of resources used. For an exponential process, each increment in problem size will multiply the resource utilization by a constant factor. In the remainder of section 1.2 we will examine two algorithms whose order of growth is logarithmic, so that doubling the problem size increases the resource requirement by a constant amount. -

      -

      Exercise 1.14. Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases? +

      +

      Exercise 1.14. Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

      -

      +

      Exercise 1.15. The sine of an angle (specified in radians) can be computed by making use of the approximation $\sin(x) = x$ if $x$ is sufficiently small, and the trigonometric identity $$ @@ -563,8 +558,6 @@

      1.2.3 Orders of Growth

      -
      -
    • What is the order of growth in space and number of steps (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated?

      @@ -583,8 +576,7 @@

      1.2.3 Orders of Growth

    • - -

      1.2.4 Exponentiation

      +

      1.2.4 Exponentiation

      Consider the problem of computing the exponential of a given number. We would like a procedure that takes as arguments a base b and a positive integer exponent $n$ and computes $b^n$. One way to do this is via the recursive definition @@ -678,11 +670,11 @@

      1.2.4 Exponentiation

      C.prompt("scheme-define-even"); -

      The process evolved by fast-expt grows logarithmically with n in both space and number of steps. To see this, observe that computing $b^{2n}$ using fast-expt requires only one more multiplication than computing $b^n$. The size of the exponent we can compute therefore doubles (approximately) with every new multiplication we are allowed. Thus, the number of multiplications required for an exponent of n grows about as fast as the logarithm of n to the base 2. The process has (log n) growth.37 +

      The process evolved by fast-expt grows logarithmically with n in both space and number of steps. To see this, observe that computing $b^{2n}$ using fast-expt requires only one more multiplication than computing $b^n$. The size of the exponent we can compute therefore doubles (approximately) with every new multiplication we are allowed. Thus, the number of multiplications required for an exponent of n grows about as fast as the logarithm of n to the base 2. The process has (log n) growth.37 -

      The difference between Θ(log n) growth and Θ(n) growth becomes striking as n becomes large. For example, fast-expt for n = 1000 requires only 14 multiplications.38 It is also possible to use the idea of successive squaring to devise an iterative algorithm that computes exponentials with a logarithmic number of steps (see exercise 1.16), although, as is often the case with iterative algorithms, this is not written down so straightforwardly as the recursive algorithm.39 +

      The difference between Θ(log n) growth and Θ(n) growth becomes striking as n becomes large. For example, fast-expt for n = 1000 requires only 14 multiplications.38 It is also possible to use the idea of successive squaring to devise an iterative algorithm that computes exponentials with a logarithmic number of steps (see exercise 1.16), although, as is often the case with iterative algorithms, this is not written down so straightforwardly as the recursive algorithm.39 -

      +

      Exercise 1.16. Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that ${(b^{n/2}})^2 = ({b^2})^{n/2}$, keep, along with the exponent $n$ and the base $b$, an additional state variable $a$, and define the state transformation in such a way that the product $a b^n$ is unchanged from state to state. At the beginning of the process $a$ is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.)

      @@ -735,7 +727,7 @@

      1.2.4 Exponentiation

      -

      +

      Exercise 1.17. The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure:

      @@ -784,8 +776,8 @@

      1.2.4 Exponentiation

      -

      -

      Exercise 1.18. Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.40 +

      +

      Exercise 1.18. Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.40

      (define (mul a b) @@ -820,8 +812,8 @@

      1.2.4 Exponentiation

      -

      -

      Exercise 1.19. There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the fib-iter process of section 1.2.2: +

      +

      Exercise 1.19. There is a clever algorithm for computing the Fibonacci numbers in a logarithmic number of steps. Recall the transformation of the state variables a and b in the fib-iter process of section 1.2.2: \begin{align} a &\leftarrow a + b \\ @@ -835,7 +827,7 @@

      1.2.4 Exponentiation

      b &\leftarrow bp + aq \end{align} -

      Show that if we apply such a transformation $T_{pq}$ twice, the effect is the same as using a single transformation $T_{p'q'}$ of the same form, and compute $p'$ and $q'$ in terms of $p$ and $q$. This gives us an explicit way to square these transformations, and thus we can compute $T^n$ using successive squaring, as in the fast-expt procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:41 +

      Show that if we apply such a transformation $T_{pq}$ twice, the effect is the same as using a single transformation $T_{p'q'}$ of the same form, and compute $p'$ and $q'$ in terms of $p$ and $q$. This gives us an explicit way to square these transformations, and thus we can compute $T^n$ using successive squaring, as in the fast-expt procedure. Put this all together to complete the following procedure, which runs in a logarithmic number of steps:41

      @@ -901,8 +893,7 @@

      1.2.4 Exponentiation

      (+ (* 2 p q) (* q q)) ; compute q' !--> - -

      1.2.5 Greatest Common Divisors

      +

      1.2.5 Greatest Common Divisors

      The greatest common divisor (GCD) of two integers a and b is defined to be the largest integer that divides both a and b with no remainder. For example, the GCD of 16 and 28 is 4. In chapter 2, when we investigate how to implement rational-number arithmetic, we will need to be able to compute GCDs in order to reduce rational numbers to lowest terms. (To reduce a rational number to lowest terms, we must divide both the numerator and the denominator by their GCD. For example, 16/28 reduces to 4/7.) One way to find the GCD of two integers is to factor them and search for common factors, but there is a famous algorithm that is much more efficient. @@ -914,7 +905,7 @@

      1.2.5 Greatest Common Divisors

      to successively reduce the problem of computing a GCD to the problem of computing the GCD of smaller and smaller pairs of integers. For example, -

      reduces GCD(206,40) to GCD(2,0), which is 2. It is possible to show that starting with any two positive integers and performing repeated reductions will always eventually produce a pair where the second number is 0. Then the GCD is the other number in the pair. This method for computing the GCD is known as Euclid's Algorithm.42 +

      reduces GCD(206,40) to GCD(2,0), which is 2. It is possible to show that starting with any two positive integers and performing repeated reductions will always eventually produce a pair where the second number is 0. Then the GCD is the other number in the pair. This method for computing the GCD is known as Euclid's Algorithm.42

      It is easy to express Euclid's Algorithm as a procedure: @@ -932,12 +923,12 @@

      1.2.5 Greatest Common Divisors

      The fact that the number of steps required by Euclid's Algorithm has logarithmic growth bears an interesting relation to the Fibonacci numbers: -

      Lamé's Theorem: If Euclid's Algorithm requires k steps to compute the GCD of some pair, then the smaller number in the pair must be greater than or equal to the kth Fibonacci number.43

      +

      Lamé's Theorem: If Euclid's Algorithm requires k steps to compute the GCD of some pair, then the smaller number in the pair must be greater than or equal to the kth Fibonacci number.43

      We can use this theorem to get an order-of-growth estimate for Euclid's Algorithm. Let n be the smaller of the two inputs to the procedure. If the process takes k steps, then we must have $n > \Fib(k) \approx \phi^k / \sqrt 5$. Therefore the number of steps k grows as the logarithm (to the base $\phi$) of n. Hence, the order of growth is $\Theta(\log n)$. -

      -

      Exercise 1.20. The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation? +

      +

      Exercise 1.20. The process that a procedure generates is of course dependent on the rules used by the interpreter. As an example, consider the iterative gcd procedure given above. Suppose we were to interpret this procedure using normal-order evaluation, as discussed in section 1.1.5. (The normal-order-evaluation rule for if is described in exercise 1.5.) Using the substitution method (for normal order), illustrate the process generated in evaluating (gcd 206 40) and indicate the remainder operations that are actually performed. How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? In the applicative-order evaluation?

      Normal

      @@ -956,8 +947,7 @@

      Applicative

      - -

      1.2.6 Example: Testing for Primality

      +

      1.2.6 Example: Testing for Primality

      This section describes two methods for checking the primality of an integer n, one with order of growth $\Theta(\sqrt n)$, and a "probabilistic" algorithm with order of growth Θ(log n). The exercises at the end of this section suggest programming projects based on these algorithms. @@ -982,7 +972,7 @@

      Searching for divisors

      -

      +

      Exercise 1.21. Use the smallest-divisor procedure to find the smallest divisor of each of the following numbers: 199, 1999, 19999.

      @@ -1021,11 +1011,11 @@

      Searching for divisors

      C.add_dep("scheme-define-prime", ["scheme-define-smallest-divisor"]); -

      The end test for find-divisor is based on the fact that if $n$ is not prime it must have a divisor less than or equal to $\sqrt n$.44 This means that the algorithm need only test divisors between 1 and $\sqrt n$. Consequently, the number of steps required to identify $n$ as prime will have order of growth $\Theta(\sqrt n)$. +

      The end test for find-divisor is based on the fact that if $n$ is not prime it must have a divisor less than or equal to $\sqrt n$.44 This means that the algorithm need only test divisors between 1 and $\sqrt n$. Consequently, the number of steps required to identify $n$ as prime will have order of growth $\Theta(\sqrt n)$.

      The Fermat test

      -

      The Θ(log n) primality test is based on a result from number theory known as Fermat's Little Theorem.45 +

      The Θ(log n) primality test is based on a result from number theory known as Fermat's Little Theorem.45

      Fermat's Little Theorem: If n is a prime number and a is any positive integer less than n, then a raised to the nth power is congruent to a modulo n.
      @@ -1050,7 +1040,7 @@

      The Fermat test

      C.add_dep("scheme-define-expmod", ["scheme-define-square"]); -

      This is very similar to the fast-expt procedure of section 1.2.4. It uses successive squaring, so that the number of steps grows logarithmically with the exponent.46 +

      This is very similar to the fast-expt procedure of section 1.2.4. It uses successive squaring, so that the number of steps grows logarithmically with the exponent.46

      The Fermat test is performed by choosing at random a number a between 1 and n - 1 inclusive and checking whether the remainder modulo n of the nth power of a is equal to a. The random number a is chosen using the procedure random, which we assume is included as a primitive in Scheme. Random returns a nonnegative integer less than its integer input. Hence, to obtain a random number between 1 and n - 1, we call random with an input of n - 1 and add 1 to the result: @@ -1082,11 +1072,11 @@

      Probabilistic methods

      The Fermat test differs in character from most familiar algorithms, in which one computes an answer that is guaranteed to be correct. Here, the answer obtained is only probably correct. More precisely, if n ever fails the Fermat test, we can be certain that n is not prime. But the fact that n passes the test, while an extremely strong indication, is still not a guarantee that n is prime. What we would like to say is that for any number n, if we perform the test enough times and find that n always passes the test, then the probability of error in our primality test can be made as small as we like. -

      Unfortunately, this assertion is not quite correct. There do exist numbers that fool the Fermat test: numbers $n$ that are not prime and yet have the property that $a^n$ is congruent to $a$ modulo $n$ for all integers $a \lt n$. Such numbers are extremely rare, so the Fermat test is quite reliable in practice.47 There are variations of the Fermat test that cannot be fooled. In these tests, as with the Fermat method, one tests the primality of an integer $n$ by choosing a random integer $a \lt n$ and checking some condition that depends upon $n$ and $a$. (See exercise 1.28 for an example of such a test.) On the other hand, in contrast to the Fermat test, one can prove that, for any $n$, the condition does not hold for most of the integers $a \lt n$ unless $n$ is prime. Thus, if $n$ passes the test for some random choice of $a$, the chances are better than even that $n$ is prime. If $n$ passes the test for two random choices of $a$, the chances are better than 3 out of 4 that $n$ is prime. By running the test with more and more randomly chosen values of $a$ we can make the probability of error as small as we like. +

      Unfortunately, this assertion is not quite correct. There do exist numbers that fool the Fermat test: numbers $n$ that are not prime and yet have the property that $a^n$ is congruent to $a$ modulo $n$ for all integers $a \lt n$. Such numbers are extremely rare, so the Fermat test is quite reliable in practice.47 There are variations of the Fermat test that cannot be fooled. In these tests, as with the Fermat method, one tests the primality of an integer $n$ by choosing a random integer $a \lt n$ and checking some condition that depends upon $n$ and $a$. (See exercise 1.28 for an example of such a test.) On the other hand, in contrast to the Fermat test, one can prove that, for any $n$, the condition does not hold for most of the integers $a \lt n$ unless $n$ is prime. Thus, if $n$ passes the test for some random choice of $a$, the chances are better than even that $n$ is prime. If $n$ passes the test for two random choices of $a$, the chances are better than 3 out of 4 that $n$ is prime. By running the test with more and more randomly chosen values of $a$ we can make the probability of error as small as we like. -

      The existence of tests for which one can prove that the chance of error becomes arbitrarily small has sparked interest in algorithms of this type, which have come to be known as probabilistic algorithms. There is a great deal of research activity in this area, and probabilistic algorithms have been fruitfully applied to many fields.48 +

      The existence of tests for which one can prove that the chance of error becomes arbitrarily small has sparked interest in algorithms of this type, which have come to be known as probabilistic algorithms. There is a great deal of research activity in this area, and probabilistic algorithms have been fruitfully applied to many fields.48 -

      +

      Exercise 1.22. Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds). The following timed-prime-test procedure, when called with an integer $n$, prints $n$ and checks to see if $n$ is prime. If n is prime, the procedure prints three asterisks followed by the amount of time used in performing the test.

      @@ -1135,26 +1125,26 @@

      Probabilistic methods

      -

      Using this procedure, write a procedure search-for-primes that checks the primality of consecutive odd integers in a specified range. Use your procedure to find the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. Note the time needed to test each prime. Since the testing algorithm has order of growth of $\Theta(\sqrt n)$, you should expect that testing for primes around 10,000 should take about $\sqrt 10$ times as long as testing for primes around 1000. Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 support the $\sqrt n$ prediction? Is your result compatible with the notion that programs on your machine run in time proportional to the number of steps required for the computation? +

      Using this procedure, write a procedure search-for-primes that checks the primality of consecutive odd integers in a specified range. Use your procedure to find the three smallest primes larger than 1000; larger than 10,000; larger than 100,000; larger than 1,000,000. Note the time needed to test each prime. Since the testing algorithm has order of growth of $\Theta(\sqrt n)$, you should expect that testing for primes around 10,000 should take about $\sqrt 10$ times as long as testing for primes around 1000. Do your timing data bear this out? How well do the data for 100,000 and 1,000,000 support the $\sqrt n$ prediction? Is your result compatible with the notion that programs on your machine run in time proportional to the number of steps required for the computation?

      -

      -

      Exercise 1.23. The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should not be $2, 3, 4, 5, 6, \ldots$, but rather $2, 3, 5, 7, 9, \ldots$ To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in exercise 1.22. Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2? +

      +

      Exercise 1.23. The smallest-divisor procedure shown at the start of this section does lots of needless testing: After it checks to see if the number is divisible by 2 there is no point in checking to see if it is divisible by any larger even numbers. This suggests that the values used for test-divisor should not be $2, 3, 4, 5, 6, \ldots$, but rather $2, 3, 5, 7, 9, \ldots$ To implement this change, define a procedure next that returns 3 if its input is equal to 2 and otherwise returns its input plus 2. Modify the smallest-divisor procedure to use (next test-divisor) instead of (+ test-divisor 1). With timed-prime-test incorporating this modified version of smallest-divisor, run the test for each of the 12 primes found in exercise 1.22. Since this modification halves the number of test steps, you should expect it to run about twice as fast. Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, and how do you explain the fact that it is different from 2?

      -

      -

      Exercise 1.24. Modify the timed-prime-test procedure of exercise 1.22 to use fast-prime? (the Fermat method), and test each of the 12 primes you found in that exercise. Since the Fermat test has $\Theta(log n)$ growth, how would you expect the time to test primes near 1,000,000 to compare with the time needed to test primes near 1000? Do your data bear this out? Can you explain any discrepancy you find? +

      +

      Exercise 1.24. Modify the timed-prime-test procedure of exercise 1.22 to use fast-prime? (the Fermat method), and test each of the 12 primes you found in that exercise. Since the Fermat test has $\Theta(log n)$ growth, how would you expect the time to test primes near 1,000,000 to compare with the time needed to test primes near 1000? Do your data bear this out? Can you explain any discrepancy you find?

      -

      -

      Exercise 1.25. Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. After all, she says, since we already know how to compute exponentials, we could have simply written +

      +

      Exercise 1.25. Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod. After all, she says, since we already know how to compute exponentials, we could have simply written

      (define (expmod base exp m) @@ -1178,7 +1168,7 @@

      Probabilistic methods

      -

      +

      Exercise 1.26. Louis Reasoner is having great difficulty doing exercise 1.24. His fast-prime? test seems to run more slowly than his prime? test. Louis calls his friend Eva Lu Ator over to help. When they examine Louis's code, they find that he has rewritten the expmod procedure to use an explicit multiplication, rather than calling square:

      @@ -1201,10 +1191,10 @@

      Probabilistic methods

      @@ -1212,13 +1202,13 @@

      Probabilistic methods

      -

      +

      Exercise 1.27. Demonstrate that the Carmichael numbers listed in footnote 47 really do fool the Fermat test. That is, write a procedure that takes an integer $n$ and tests whether $a^n$ is congruent to $a$ modulo $n$ for every $a < n$, and try your procedure on the given Carmichael numbers.

      -

      +

      Exercise 1.28. One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat's Little Theorem, which states that if $n$ is a prime number and $a$ is any positive integer less than $n$, then $a$ raised to the $(n - 1)$st power is congruent to 1 modulo $n$. To test the primality of a number $n$ by the Miller-Rabin test, we pick a random number $a \lt n$ and raise $a$ to the $(n - 1)$st power modulo $n$ using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a “nontrivial square root of 1 modulo $n$,” that is, a number not equal to 1 or $n - 1$ whose square is equal to 1 modulo $n$. It is possible to prove that if such a nontrivial square root of 1 exists, then $n$ is not prime. It is also possible to prove that if $n$ is an odd number that is not prime, then, for at least half the numbers $a \lt n$, computing $a^{n-1}$ in this way will reveal a nontrivial square root of 1 modulo $n$. (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0.

      @@ @@ -1227,7 +1217,7 @@

      Probabilistic methods

      29 - In a real program we would probably use the block structure introduced in the last section to hide the definition of fact-iter:

      (define (factorial n)
        (define (iter product counter)
          (if (> counter n)
              product
              (iter (* counter product)
                    (+ counter 1))))
        (iter 1 1))

      We avoided doing this here so as to minimize the number of things to think about at once. + In a real program we would probably use the block structure introduced in the last section to hide the definition of fact-iter:

      (define (factorial n)
        (define (iter product counter)
          (if (> counter n)
              product
              (iter (* counter product)
                    (+ counter 1))))
        (iter 1 1))

      We avoided doing this here so as to minimize the number of things to think about at once.

      @@ -1242,7 +1232,7 @@

      Probabilistic methods

      32 - An example of this was hinted at in section 1.1.3: The interpreter itself evaluates expressions using a tree-recursive process. + An example of this was hinted at in section 1.1.3: The interpreter itself evaluates expressions using a tree-recursive process.

      @@ -1252,7 +1242,7 @@

      Probabilistic methods

      34 - One approach to coping with redundant computations is to arrange matters so that we automatically construct a table of values as they are computed. Each time we are asked to apply the procedure to some argument, we first look to see if the value is already stored in the table, in which case we avoid performing the redundant computation. This strategy, known as tabulation or memoization, can be implemented in a straightforward way. Tabulation can sometimes be used to transform processes that require an exponential number of steps (such as count-change) into processes whose space and time requirements grow linearly with the input. See exercise 3.27. + One approach to coping with redundant computations is to arrange matters so that we automatically construct a table of values as they are computed. Each time we are asked to apply the procedure to some argument, we first look to see if the value is already stored in the table, in which case we avoid performing the redundant computation. This strategy, known as tabulation or memoization, can be implemented in a straightforward way. Tabulation can sometimes be used to transform processes that require an exponential number of steps (such as count-change) into processes whose space and time requirements grow linearly with the input. See exercise 3.27.

      @@ -1272,17 +1262,17 @@

      Probabilistic methods

      38 - You may wonder why anyone would care about raising numbers to the 1000th power. See section 1.2.6. + You may wonder why anyone would care about raising numbers to the 1000th power. See section 1.2.6.

      39 - This iterative algorithm is ancient. It appears in the Chandah-sutra by Áchárya Pingala, written before 200 B.C. See Knuth 1981, section 4.6.3, for a full discussion and analysis of this and other methods of exponentiation. + This iterative algorithm is ancient. It appears in the Chandah-sutra by Áchárya Pingala, written before 200 B.C. See Knuth 1981, section 4.6.3, for a full discussion and analysis of this and other methods of exponentiation.

      40 - This algorithm, which is sometimes known as the “Russian peasant method” of multiplication, is ancient. Examples of its use are found in the Rhind Papyrus, one of the two oldest mathematical documents in existence, written about 1700 B.C. (and copied from an even older document) by an Egyptian scribe named A'h-mose. + This algorithm, which is sometimes known as the “Russian peasant method” of multiplication, is ancient. Examples of its use are found in the Rhind Papyrus, one of the two oldest mathematical documents in existence, written about 1700 B.C. (and copied from an even older document) by an Egyptian scribe named A'h-mose.

      @@ -1292,7 +1282,7 @@

      Probabilistic methods

      42 - Euclid's Algorithm is so called because it appears in Euclid's Elements (Book 7, ca. 300 B.C.). According to Knuth (1973), it can be considered the oldest known nontrivial algorithm. The ancient Egyptian method of multiplication (exercise 1.18) is surely older, but, as Knuth explains, Euclid's algorithm is the oldest known to have been presented as a general algorithm, rather than as a set of illustrative examples. + Euclid's Algorithm is so called because it appears in Euclid's Elements (Book 7, ca. 300 B.C.). According to Knuth (1973), it can be considered the oldest known nontrivial algorithm. The ancient Egyptian method of multiplication (exercise 1.18) is surely older, but, as Knuth explains, Euclid's algorithm is the oldest known to have been presented as a general algorithm, rather than as a set of illustrative examples.

      @@ -1312,7 +1302,7 @@

      Probabilistic methods

      46 - The reduction steps in the cases where the exponent e is greater than 1 are based on the fact that, for any integers x, y, and m, we can find the remainder of x times y modulo m by computing separately the remainders of x modulo m and y modulo m, multiplying these, and then taking the remainder of the result modulo m. For instance, in the case where e is even, we compute the remainder of be/2 modulo m, square this, and take the remainder modulo m. This technique is useful because it means we can perform our computation without ever having to deal with numbers much larger than m. (Compare exercise 1.25.) + The reduction steps in the cases where the exponent e is greater than 1 are based on the fact that, for any integers x, y, and m, we can find the remainder of x times y modulo m by computing separately the remainders of x modulo m and y modulo m, multiplying these, and then taking the remainder of the result modulo m. For instance, in the case where e is even, we compute the remainder of be/2 modulo m, square this, and take the remainder modulo m. This technique is useful because it means we can perform our computation without ever having to deal with numbers much larger than m. (Compare exercise 1.25.)

      diff --git a/content/1-3-hop.content.html b/content/1-3-hop.content.html index 0a53b1b..c4447b2 100644 --- a/content/1-3-hop.content.html +++ b/content/1-3-hop.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

      @@ -47,8 +47,7 @@

      Formulating Abstractions with Higher-Order Procedures

      Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures that manipulate procedures are called higher-order procedures. This section shows how higher-order procedures can serve as powerful abstraction mechanisms, vastly increasing the expressive power of our language. - -

      1.3.1 Procedures as Arguments

      +

      1.3.1 Procedures as Arguments

      Consider the following three procedures. The first computes the sum of the integers from a through b: @@ -80,7 +79,7 @@

      1.3.1 Procedures as Arguments

      \frac{1}{1 \cdot 3} + \frac{1}{5 \cdot 7} + \frac{1}{9 \cdot 11} + \cdots $$ -

      which converges to $\pi/8$ (very slowly):49 +

      which converges to $\pi/8$ (very slowly):49

      (define (pi-sum a b) @@ -126,7 +125,7 @@

      1.3.1 Procedures as Arguments

      C.prompt("scheme-define-sum"); -

      Notice that sum takes as its arguments the lower and upper bounds a and b together with the procedures term and next. We can use sum just as we would any procedure. For example, we can use it (along with a procedure inc that increments its argument by 1) to define sum-cubes: +

      Notice that sum takes as its arguments the lower and upper bounds a and b together with the procedures term and next. We can use sum just as we would any procedure. For example, we can use it (along with a procedure inc that increments its argument by 1) to define sum-cubes:

      (define (inc n) (+ n 1)) @@ -155,7 +154,7 @@

      1.3.1 Procedures as Arguments

      C.deps_of["scheme-sum-cubes-1-10"] = ["scheme-define-sum-cubes-sum"]; -

      With the aid of an identity procedure to compute the term, we can define sum-integers in terms of sum: +

      With the aid of an identity procedure to compute the term, we can define sum-integers in terms of sum:

      (define (identity x) x) @@ -178,7 +177,7 @@

      1.3.1 Procedures as Arguments

      C.deps_of["scheme-sum-integers-1-10"] = ["scheme-define-sum-integers-sum"]; -

      We can also define pi-sum in the same way:50 +

      We can also define pi-sum in the same way:50

      (define (pi-sum a b) @@ -203,7 +202,7 @@

      1.3.1 Procedures as Arguments

      C.deps_of["scheme-pi-sum-pi"] = ["scheme-define-pi-sum-sum"]; -

      Once we have sum, we can use it as a building block in formulating further concepts. For instance, the definite integral of a function f between the limits a and b can be approximated numerically using the formula +

      Once we have sum, we can use it as a building block in formulating further concepts. For instance, the definite integral of a function f between the limits a and b can be approximated numerically using the formula $$ {\int_a^b f} @@ -246,7 +245,7 @@

      1.3.1 Procedures as Arguments

      (The exact value of the integral of cube between $0$ and $1$ is $1/4$.) -

      +

      Exercise 1.29. Simpson's Rule is a more accurate method of numerical integration than the method illustrated above. Using Simpson's Rule, the integral of a function f between a and b is approximated as $$ @@ -336,7 +335,7 @@

      1.3.1 Procedures as Arguments

      -

      +

      Exercise 1.30. The sum procedure above generates a linear recursion. The procedure can be rewritten so that the sum is performed iteratively. Show how to do this by filling in the missing expressions in the following definition:

      @@ -368,10 +367,10 @@

      1.3.1 Procedures as Arguments

      -

      +

      Exercise 1.31. -

      The sum procedure is only the simplest of a vast number of similar abstractions that can be captured as higher-order procedures.51 Write an analogous procedure called product that returns the product of the values of a function at points over a given range. Try both a recursive and an iterative approach. Show how to define factorial in terms of product. Also use product to compute approximations to using the formula52 +

      The sum procedure is only the simplest of a vast number of similar abstractions that can be captured as higher-order procedures.51 Write an analogous procedure called product that returns the product of the values of a function at points over a given range. Try both a recursive and an iterative approach. Show how to define factorial in terms of product. Also use product to compute approximations to using the formula52 $$ \frac{\pi}{4} = \frac{2 \cdot 4 \cdot 4 \cdot 6 \cdot 6 \cdot 8 \cdots}{3 \cdot 3 \cdot 5 \cdot 5 \cdot 7 \cdot 7 \cdots} @@ -409,7 +408,7 @@

      1.3.1 Procedures as Arguments

      -

      +

      Exercise 1.32.

      Show that sum and product (exercise 1.31) are both special cases of a still more general notion called accumulate that combines a collection of terms, using some general accumulation function: @@ -457,7 +456,7 @@

      1.3.1 Procedures as Arguments

      -

      +

      Exercise 1.33. You can obtain an even more general version of accumulate (exercise 1.32) by introducing the notion of a filter on the terms to be combined. That is, combine only those terms derived from values in the range that satisfy a specified condition. The resulting filtered-accumulate abstraction takes the same arguments as accumulate, together with an additional predicate of one argument that specifies the filter. Write filtered-accumulate as a procedure.

      @@ -494,7 +493,7 @@

      1.3.1 Procedures as Arguments

      1. -

        the sum of the squares of the prime numbers in the interval a to b (assuming that you have a prime? predicate already written) +

        the sum of the squares of the prime numbers in the interval a to b (assuming that you have a prime? predicate already written)

      2. @@ -504,10 +503,9 @@

        1.3.1 Procedures as Arguments

      - -

      1.3.2 Constructing Procedures Using Lambda

      +

      1.3.2 Constructing Procedures Using Lambda

      -

      In using sum as in section 1.3.1, it seems terribly awkward to have to define trivial procedures such as pi-term and pi-next just so we can use them as arguments to our higher-order procedure. Rather than define pi-next and pi-term, it would be more convenient to have a way to directly specify “the procedure that returns its input incremented by 4” and “the procedure that returns the reciprocal of its input times its input plus 2.” We can do this by introducing the special form lambda, which creates procedures. Using lambda we can describe what we want as +

      In using sum as in section 1.3.1, it seems terribly awkward to have to define trivial procedures such as pi-term and pi-next just so we can use them as arguments to our higher-order procedure. Rather than define pi-next and pi-term, it would be more convenient to have a way to directly specify “the procedure that returns its input incremented by 4” and “the procedure that returns the reciprocal of its input times its input plus 2.” We can do this by introducing the special form lambda, which creates procedures. Using lambda we can describe what we want as

      (lambda (x) (+ x 4)) @@ -583,10 +581,10 @@

      1.3.2 Constructing Procedures Using Lambda

      We can read a lambda expression as follows: -

          (lambda             (x)             (+    x     4))
      -                                                
      - the procedure   of an argument x  that adds  x and 4
      -

      +

          (lambda             (x)             (+    x     4))
      +                                                
      + the procedure   of an argument x  that adds  x and 4
      +

      Like any expression that has a procedure as its value, a lambda expression can be used as the operator in a combination such as @@ -598,11 +596,11 @@

      1.3.2 Constructing Procedures Using Lambda

      C.deps_of["scheme-apply-lambda"] = ["scheme-define-square"]; -

      or, more generally, in any context where we would normally use a procedure name.53 +

      or, more generally, in any context where we would normally use a procedure name.53

      Using let to create local variables

      -

      Another use of lambda is in creating local variables. We often need local variables in our procedures other than those that have been bound as formal parameters. For example, suppose we wish to compute the function +

      Another use of lambda is in creating local variables. We often need local variables in our procedures other than those that have been bound as formal parameters. For example, suppose we wish to compute the function $$ f(x,y) = x (1 + xy)^2 + y(1-y) + (1 + xy)(1 - y) @@ -632,7 +630,7 @@

      Using let to create local variables

      C.deps_of["scheme-define-f-helpers"] = ["scheme-define-square"]; -

      Of course, we could use a lambda expression to specify an anonymous procedure for binding our local variables. The body of f then becomes a single call to that procedure: +

      Of course, we could use a lambda expression to specify an anonymous procedure for binding our local variables. The body of f then becomes a single call to that procedure:

      (define (f x y) @@ -648,7 +646,7 @@

      Using let to create local variables

      C.deps_of["scheme-define-f-lambda"] = ["scheme-define-square"]; -

      This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as +

      This construct is so useful that there is a special form called let to make its use more convenient. Using let, the f procedure could be written as

      (define (f x y) @@ -663,7 +661,7 @@

      Using let to create local variables

      C.deps_of["scheme-define-f-let"] = ["scheme-define-square"]; -

      The general form of a let expression is +

      The general form of a let expression is

      (let ((<var1> <exp1>) @@ -686,7 +684,7 @@

      Using let to create local variables

      in <body> -

      The first part of the let expression is a list of name-expression pairs. When the let is evaluated, each name is associated with the value of the corresponding expression. The body of the let is evaluated with these names bound as local variables. The way this happens is that the let expression is interpreted as an alternate syntax for +

      The first part of the let expression is a list of name-expression pairs. When the let is evaluated, each name is associated with the value of the corresponding expression. The body of the let is evaluated with these names bound as local variables. The way this happens is that the let expression is interpreted as an alternate syntax for

      ((lambda (<var_1> ... <var_n>) @@ -699,14 +697,14 @@

      Using let to create local variables

      C.make_static("scheme-let-expanded-syntax"); -

      No new mechanism is required in the interpreter in order to provide local variables. A let expression is simply syntactic sugar for the underlying lambda application. +

      No new mechanism is required in the interpreter in order to provide local variables. A let expression is simply syntactic sugar for the underlying lambda application. -

      We can see from this equivalence that the scope of a variable specified by a let expression is the body of the let. This implies that: +

      We can see from this equivalence that the scope of a variable specified by a let expression is the body of the let. This implies that:

      • -let allows one to bind variables as locally as possible to where they are to be used. For example, if the value of x is 5, the value of the expression +let allows one to bind variables as locally as possible to where they are to be used. For example, if the value of x is 5, the value of the expression
        (define x 5) @@ -732,11 +730,11 @@

        Using let to create local variables

        }); -

        Here, the x in the body of the let is 3, so the value of the let expression is 33. On the other hand, the x that is the second argument to the outermost + is still 5. +

        Here, the x in the body of the let is 3, so the value of the let expression is 33. On the other hand, the x that is the second argument to the outermost + is still 5.

      • -

        The variables' values are computed outside the let. This matters when the expressions that provide the values for the local variables depend upon variables having the same names as the local variables themselves. For example, if the value of x is 2, the expression +

        The variables' values are computed outside the let. This matters when the expressions that provide the values for the local variables depend upon variables having the same names as the local variables themselves. For example, if the value of x is 2, the expression

        (define x 2) @@ -760,11 +758,11 @@

        Using let to create local variables

        }); -

        will have the value because, inside the body of the let, x will be 3 and y will be 4 (which is the outer x plus 2). +

        will have the value because, inside the body of the let, x will be 3 and y will be 4 (which is the outer x plus 2).

      -

      Sometimes we can use internal definitions to get the same effect as with let. For example, we could have defined the procedure f above as +

      Sometimes we can use internal definitions to get the same effect as with let. For example, we could have defined the procedure f above as

      (define (f x y) @@ -779,9 +777,9 @@

      Using let to create local variables

      C.deps_of["scheme-define-f-close-define"] = ["scheme-define-square"]; -

      We prefer, however, to use let in situations like this and to use internal define only for internal procedures.54 +

      We prefer, however, to use let in situations like this and to use internal define only for internal procedures.54 -

      +

      Exercise 1.34: Suppose we define the procedure

      @@ -809,24 +807,23 @@

      Using let to create local variables

      What happens if we (perversely) ask the interpreter to evaluate the combination -(f f)? Explain. +(f f)? Explain.

      - -

      1.3.3 Procedures as General Methods

      +

      1.3.3 Procedures as General Methods

      -

      We introduced compound procedures in section @ref{1-1-4} as a mechanism for abstracting patterns of numerical operations so as to make them independent of the particular numbers involved. With higher-order procedures, such as the integral procedure of section @ref{1-3-1}, we began to see a more powerful kind of abstraction: procedures used to express general methods of computation, independent of the particular functions involved. In this section we discuss two more elaborate examples — general methods for finding zeros and fixed points of functions — and show how these methods can be expressed directly as procedures. +

      We introduced compound procedures in section 1.1.4 as a mechanism for abstracting patterns of numerical operations so as to make them independent of the particular numbers involved. With higher-order procedures, such as the integral procedure of section 1.3.1, we began to see a more powerful kind of abstraction: procedures used to express general methods of computation, independent of the particular functions involved. In this section we discuss two more elaborate examples — general methods for finding zeros and fixed points of functions — and show how these methods can be expressed directly as procedures.

      Finding roots of equations by the half-interval method

      @@ -860,7 +857,7 @@

      Finding roots of equations by the half-interval method

      We assume that we are initially given the function f together with points at which its values are negative and positive. We first compute the midpoint of the two given points. Next we check to see if the given interval is small enough, and if so we simply return the midpoint as our answer. Otherwise, we compute as a test value the value of f at the midpoint. If the test value is positive, then we continue the process with a new interval running from the original negative point to the midpoint. If the test value is negative, we continue with the interval from the midpoint to the positive point. Finally, there is the possibility that the test value is 0, in which case the midpoint is itself the root we are searching for. -

      To test whether the endpoints are “close enough” we can use a procedure similar to the one used in section 1.1.7 for computing square roots:55 +

      To test whether the endpoints are “close enough” we can use a procedure similar to the one used in section 1.1.7 for computing square roots:55

      (define (close-enough? x y) @@ -870,11 +867,11 @@

      Finding roots of equations by the half-interval method

      C.prompt("scheme-define-close-enough"); -

      search is awkward to use directly, because we can accidentally give it points at which f 's values do not have the required sign, in which case we get a wrong answer. Instead we will use search via the following procedure, which checks to see which of the endpoints has a negative function value and which has a positive value, and calls the search procedure accordingly. If the function has the same sign on the two given points, the half-interval method cannot be used, in which case the procedure signals an error.56 +

      search is awkward to use directly, because we can accidentally give it points at which f 's values do not have the required sign, in which case we get a wrong answer. Instead we will use search via the following procedure, which checks to see which of the endpoints has a negative function value and which has a positive value, and calls the search procedure accordingly. If the function has the same sign on the two given points, the half-interval method cannot be used, in which case the procedure signals an error.56

      (define (negative? x) - (< x 0)) + (< x 0)) (define (positive? x) (> x 0)) @@ -929,7 +926,7 @@

      Finding fixed points of functions

      f(x), f(f(x), (f(f(f(x)))) $$ -

      until the value does not change very much. Using this idea, we can devise a procedure fixed-point that takes as inputs a function and an initial guess and produces an approximation to a fixed point of the function. We apply the function repeatedly until we find two successive values whose difference is less than some prescribed tolerance: +

      until the value does not change very much. Using this idea, we can devise a procedure fixed-point that takes as inputs a function and an initial guess and produces an approximation to a fixed point of the function. We apply the function repeatedly until we find two successive values whose difference is less than some prescribed tolerance:

      (define tolerance 0.00001) @@ -948,7 +945,7 @@

      Finding fixed points of functions

      C.prompt("scheme-define-fixed-point", ["scheme-define-close-enough"]); -

      For example, we can use this method to approximate the fixed point of the cosine function, starting with 1 as an initial approximation:57 +

      For example, we can use this method to approximate the fixed point of the cosine function, starting with 1 as an initial approximation:57

      (fixed-point cos 1.0) @@ -967,7 +964,7 @@

      Finding fixed points of functions

      C.prompt("scheme-fixed-point-sin-cos", ["scheme-define-fixed-point"]); -

      The fixed-point process is reminiscent of the process we used for finding square roots in section 1.1.7. Both are based on the idea of repeatedly improving a guess until the result satisfies some criterion. In fact, we can readily formulate the square-root computation as a fixed-point search. Computing the square root of some number $x$ requires finding a $y$ such that $y^2 = x$. Putting this equation into the equivalent form $y = x/y$, we recognize that we are looking for a fixed point of the function58 $y \mapsto x/y$, and we can therefore try to compute square roots as +

      The fixed-point process is reminiscent of the process we used for finding square roots in section 1.1.7. Both are based on the idea of repeatedly improving a guess until the result satisfies some criterion. In fact, we can readily formulate the square-root computation as a fixed-point search. Computing the square root of some number $x$ requires finding a $y$ such that $y^2 = x$. Putting this equation into the equivalent form $y = x/y$, we recognize that we are looking for a fixed point of the function58 $y \mapsto x/y$, and we can therefore try to compute square roots as

      (define (sqrt x) @@ -994,10 +991,10 @@

      Finding fixed points of functions

      (Note that $y = \frac{1}{2}(y + \frac{x}{y})$ is a simple transformation of the equation $y = \frac{x}{y}$; to derive it, add $y$ to both sides of the equation and divide by $2$.) -

      With this modification, the square-root procedure works. In fact, if we unravel the definitions, we can see that the sequence of approximations to the square root generated here is precisely the same as the one generated by our original square-root procedure of section 1.1.7. This approach of averaging successive approximations to a solution, a technique we that we call average damping, often aids the convergence of fixed-point searches. +

      With this modification, the square-root procedure works. In fact, if we unravel the definitions, we can see that the sequence of approximations to the square root generated here is precisely the same as the one generated by our original square-root procedure of section 1.1.7. This approach of averaging successive approximations to a solution, a technique we that we call average damping, often aids the convergence of fixed-point searches. -

      -

      Exercise 1.35. Show that the golden ratio (section 1.2.2) is a fixed point of the transformation $x \mapsto 1 + \frac{1}{x}$, and use this fact to compute $\phi$ by means of the fixed-point procedure. +

      +

      Exercise 1.35. Show that the golden ratio (section 1.2.2) is a fixed point of the transformation $x \mapsto 1 + \frac{1}{x}$, and use this fact to compute $\phi$ by means of the fixed-point procedure.

      (define phi @@ -1011,8 +1008,8 @@

      Finding fixed points of functions

      -

      -

      Exercise 1.36. Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22. Then find a solution to $x^x = 1000$ by finding a fixed point of $x \mapsto \frac{\log(1000)}{\log(x)}$. (Use Scheme's primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start fixed-point with a guess of 1, as this would cause division by $\log(1) = 0.$) +

      +

      Exercise 1.36. Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22. Then find a solution to $x^x = 1000$ by finding a fixed point of $x \mapsto \frac{\log(1000)}{\log(x)}$. (Use Scheme's primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start fixed-point with a guess of 1, as this would cause division by $\log(1) = 0.$)

      (define tolerance 0.00001) @@ -1055,20 +1052,20 @@

      Finding fixed points of functions

      -

      +

      Exercise 1.37. An infinite continued fraction is an expression of the form $$ f = \cfrac{N_1}{D_1 + \cfrac{N_2}{D_2 + \cfrac{N_3}{D_3 + \cdots}}} $$ -

      As an example, one can show that the infinite continued fraction expansion with the $N_i$ and the $D_i$ all equal to $1$ produces $\frac{1}{\phi}$, where $\phi$ is the golden ratio (described in section 1.2.2). One way to approximate an infinite continued fraction is to truncate the expansion after a given number of terms. Such a truncation — a so-called k-term finite continued fraction — has the form +

      As an example, one can show that the infinite continued fraction expansion with the $N_i$ and the $D_i$ all equal to $1$ produces $\frac{1}{\phi}$, where $\phi$ is the golden ratio (described in section 1.2.2). One way to approximate an infinite continued fraction is to truncate the expansion after a given number of terms. Such a truncation — a so-called k-term finite continued fraction — has the form $$ f = \cfrac{N_1}{D_1 + \cfrac{N_2}{\ddots + \cfrac{N_k}{D_k}}} $$ -

      Suppose that n and d are procedures of one argument (the term index $i$ that return the $N_i$ and $D_i$ of the terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont-frac n d k) computes the value of the $k$-term finite continued fraction. +

      Suppose that n and d are procedures of one argument (the term index $i$ that return the $N_i$ and $D_i$ of the terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont-frac n d k) computes the value of the $k$-term finite continued fraction.

      (define (cont-frac n d k) @@ -1099,16 +1096,16 @@

      Finding fixed points of functions

      C.prompt("scheme-test-cont-frac", ["scheme-ex-define-cont-frac"]); -

      for successive values of k. How large must you make k in order to get an approximation that is accurate to 4 decimal places? +

      for successive values of k. How large must you make k in order to get an approximation that is accurate to 4 decimal places? -

      Try to write both an iterative and a recursive version of cont-frac. +

      Try to write both an iterative and a recursive version of cont-frac.

      -

      -

      Exercise 1.38. In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, which included a continued fraction expansion for $e - 2$, where $e$ is the base of the natural logarithms. In this fraction, the $N_i$ are all $1$, and the $D_i$ are successively $1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, \ldots$ Write a program that uses your cont-frac procedure from exercise 1.37 to approximate $e$, based on Euler's expansion. +

      +

      Exercise 1.38. In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, which included a continued fraction expansion for $e - 2$, where $e$ is the base of the natural logarithms. In this fraction, the $N_i$ are all $1$, and the $D_i$ are successively $1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, \ldots$ Write a program that uses your cont-frac procedure from exercise 1.37 to approximate $e$, based on Euler's expansion.

      (cont-frac n d 10) @@ -1127,14 +1124,14 @@

      Finding fixed points of functions

      -

      +

      Exercise 1.39. A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert: $$ \tan x = \cfrac{x}{1 - \cfrac{x^2}{3 - \cfrac{x^2}{5 - \ddots}}} $$ -

      where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert's formula. k specifies the number of terms to compute, as in Exercise 1-37. +

      where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert's formula. k specifies the number of terms to compute, as in Exercise 1-37.

      (define (tan-cf x k) @@ -1157,16 +1154,11 @@

      Finding fixed points of functions

      - -

      1.3.4 Procedures as Returned Values

      +

      1.3.4 Procedures as Returned Values

      The above examples demonstrate how the ability to pass procedures as arguments significantly enhances the expressive power of our programming language. We can achieve even more expressive power by creating procedures whose returned values are themselves procedures. -

      We can illustrate this idea by looking again at the fixed-point example described at the end of section 1.3.3. We formulated a new version of the square-root procedure as a fixed-point search, starting with the observation that x is a fixed-point of the function y -> x/y. Then we used average damping to make the approximations converge. Average damping is a useful general technique in itself. Namely, given a function f, we consider the function whose value at x is equal to the average of x and f(x). - -

      Average damping is a useful general technique in itself. Namely, given a -function $f$, we consider the function whose value at $x$ is equal to the -average of $x$ and $f(x)$. +

      We can illustrate this idea by looking again at the fixed-point example described at the end of section 1.3.3. We formulated a new version of the square-root procedure as a fixed-point search, starting with the observation that x is a fixed-point of the function y -> x/y. Then we used average damping to make the approximations converge. Average damping is a useful general technique in itself. Namely, given a function $f$, we consider the function whose value at $x$ is equal to the average of $x$ and $f(x)$.

      We can express the idea of average damping by means of the following procedure: @@ -1178,7 +1170,7 @@

      1.3.4 Procedures as Returned Values

      C.prompt("scheme-define-average-damp", ["scheme-define-average"]); -

      average-damp is a procedure that takes as its argument a procedure f and returns as its value a procedure (produced by the lambda) that, when applied to a number x, produces the average of x and (f x). For example, applying average-damp to the square procedure produces a procedure whose value at some number $x$ is the average of $x$ and $x^2$. Applying this resulting procedure to $10$ returns the average of $10$ and $100$, or $55$:59 +

      average-damp is a procedure that takes as its argument a procedure f and returns as its value a procedure (produced by the lambda) that, when applied to a number x, produces the average of x and (f x). For example, applying average-damp to the square procedure produces a procedure whose value at some number $x$ is the average of $x$ and $x^2$. Applying this resulting procedure to $10$ returns the average of $10$ and $100$, or $55$:59

      ((average-damp square) 10) @@ -1187,7 +1179,7 @@

      1.3.4 Procedures as Returned Values

      C.prompt("scheme-average-damp-square-10", ["scheme-define-average-damp", "scheme-define-square"]); -

      Using average-damp, we can reformulate the square-root procedure as follows: +

      Using average-damp, we can reformulate the square-root procedure as follows:

      (define (sqrt x) @@ -1199,7 +1191,7 @@

      1.3.4 Procedures as Returned Values

      C.add_dep("scheme-define-sqrt-average-damp", ["scheme-define-fixed-point", "scheme-define-average-damp"]); -

      Notice how this formulation makes explicit the three ideas in the method: fixed-point search, average damping, and the function $y \mapsto \frac{x}{y}$. It is instructive to compare this formulation of the square-root method with the original version given in section 1.1.7. Bear in mind that these procedures express the same process, and notice how much clearer the idea becomes when we express the process in terms of these abstractions. In general, there are many ways to formulate a process as a procedure. Experienced programmers know how to choose procedural formulations that are particularly perspicuous, and where useful elements of the process are exposed as separate entities that can be reused in other applications. As a simple example of reuse, notice that the cube root of $x$ is a fixed point of the function $y \mapsto \frac{x}{y^2}$, so we can immediately generalize our square-root procedure to one that extracts cube roots:60 +

      Notice how this formulation makes explicit the three ideas in the method: fixed-point search, average damping, and the function $y \mapsto \frac{x}{y}$. It is instructive to compare this formulation of the square-root method with the original version given in section 1.1.7. Bear in mind that these procedures express the same process, and notice how much clearer the idea becomes when we express the process in terms of these abstractions. In general, there are many ways to formulate a process as a procedure. Experienced programmers know how to choose procedural formulations that are particularly perspicuous, and where useful elements of the process are exposed as separate entities that can be reused in other applications. As a simple example of reuse, notice that the cube root of $x$ is a fixed point of the function $y \mapsto \frac{x}{y^2}$, so we can immediately generalize our square-root procedure to one that extracts cube roots:60

      (define (cube-root x) @@ -1213,13 +1205,13 @@

      1.3.4 Procedures as Returned Values

      Newton's method

      -

      When we first introduced the square-root procedure, in section 1.1.7, we mentioned that this was a special case of Newton's method. If x g(x) is a differentiable function, then a solution of the equation g(x) = 0 is a fixed point of the function x f(x) where +

      When we first introduced the square-root procedure, in section 1.1.7, we mentioned that this was a special case of Newton's method. If x g(x) is a differentiable function, then a solution of the equation g(x) = 0 is a fixed point of the function x f(x) where $$ f(x) = x - \frac{g(x)}{Dg(x)} $$ -

      and $Dg(x)$ is the derivative of $g$ evaluated at $x$. Newton's method is the use of the fixed-point method we saw above to approximate a solution of the equation by finding a fixed point of the function $f$.61 For many functions $g$ and for sufficiently good initial guesses for $x$, Newton's method converges very rapidly to a solution of g(x) = 0.62 +

      and $Dg(x)$ is the derivative of $g$ evaluated at $x$. Newton's method is the use of the fixed-point method we saw above to approximate a solution of the equation by finding a fixed point of the function $f$.61 For many functions $g$ and for sufficiently good initial guesses for $x$, Newton's method converges very rapidly to a solution of g(x) = 0.62

      In order to implement Newton's method as a procedure, we must first express the idea of derivative. Note that “derivative”, like average damping, is something that transforms a function into another function. For instance, the derivative of the function $x \mapsto x^3$ is the function $x \mapsto 3x^2$. In general, if $g$ is a function and $dx$ is a small number, then the derivative $Dg$ of $g$ is the function whose value at any number $x$ is given (in the limit of small $dx$) by @@ -1259,7 +1251,7 @@

      Newton's method

      C.prompt("scheme-deriv-cube", ["scheme-define-deriv", "scheme-define-dx"]); -

      With the aid of deriv, we can express Newton's method as a fixed-point process: +

      With the aid of deriv, we can express Newton's method as a fixed-point process:

      (define (newton-transform g) @@ -1273,7 +1265,7 @@

      Newton's method

      C.prompt("scheme-define-newton-transform", ["scheme-define-deriv", "scheme-define-dx", "scheme-define-fixed-point"]); -

      The newton-transform procedure expresses the formula at the beginning of this section, and newtons-method is readily defined in terms of this. It takes as arguments a procedure that computes the function for which we want to find a zero, together with an initial guess. For instance, to find the square root of x, we can use Newton's method to find a zero of the function $y \mapsto y^2 - x$ starting with an initial guess of 1.63 This provides yet another form of the square-root procedure: +

      The newton-transform procedure expresses the formula at the beginning of this section, and newtons-method is readily defined in terms of this. It takes as arguments a procedure that computes the function for which we want to find a zero, together with an initial guess. For instance, to find the square root of x, we can use Newton's method to find a zero of the function $y \mapsto y^2 - x$ starting with an initial guess of 1.63 This provides yet another form of the square-root procedure:

      (define (sqrt x) @@ -1328,23 +1320,22 @@

      Abstractions and first-class procedures

      As programmers, we should be alert to opportunities to identify the underlying abstractions in our programs and to build upon them and generalize them to create more powerful abstractions. This is not to say that one should always write programs in the most abstract way possible; expert programmers know how to choose the level of abstraction appropriate to their task. But it is important to be able to think in terms of these abstractions, so that we can be ready to apply them in new contexts. The significance of higher-order procedures is that they enable us to represent these abstractions explicitly as elements in our programming language, so that they can be handled just like other computational elements. -

      In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the “rights and privileges” of first-class elements are:64 +

      In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the “rights and privileges” of first-class elements are:64

      • They may be named by variables.

      • They may be passed as arguments to procedures.

      • They may be returned as the results of procedures.

      • -
      • They may be included in data structures.65

      • +
      • They may be included in data structures.65

      -

      Lisp, unlike other common programming languages, awards procedures full first-class status. This poses challenges for efficient implementation, but the resulting gain in expressive power is enormous.66 - -

      +

      Lisp, unlike other common programming languages, awards procedures full first-class status. This poses challenges for efficient implementation, but the resulting gain in expressive power is enormous.66 -

      Exercise 1.40. Define a procedure cubic -that can be used together with the newtons-method procedure in +

      +

      Exercise 1.40. Define a procedure cubic +that can be used together with the newtons-method procedure in expressions of the form

      @@ -1363,8 +1354,8 @@

      Abstractions and first-class procedures

      -

      -

      Exercise 1.41. Define a procedure double that takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2. +

      +

      Exercise 1.41. Define a procedure double that takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2.

      (define (double f) @@ -1402,7 +1393,7 @@

      Abstractions and first-class procedures

      -

      +

      Exercise 1.42. Let f and g be two one-argument functions. The composition f after g is defined to be the function x f(g(x)). Define a procedure compose that implements composition.

      @@ -1447,7 +1438,7 @@

      Abstractions and first-class procedures

      -

      +

      Exercise 1.43. If f is a numerical function and n is a positive integer, then we can form the $n^{th}$ repeated application of $f$, which is defined to be the function whose value at $x$ is $f(f(\cdots(f(x))\cdots))$. For example, if $f$ is the function $x \mapsto x + 1$, then the $n^{th}$ repeated application of $f$ is the function $x \mapsto x + n$. If $f$ is the operation of squaring a number, then the $n^{th}$ repeated application of $f$ is the function that raises its argument to the $2n^{th}$ power. Write a procedure that takes as inputs a procedure that computes $f$ and a positive integer $n$ and returns the procedure that computes the $n^{th}$ repeated application of $f$.

      @@ -1492,14 +1483,14 @@

      Abstractions and first-class procedures

      C.frozen_prompt("scheme-repeated-square-2-5", ["scheme-ex-define-repeated", "scheme-define-square"]); -

      and should return 625. Hint: You may find it convenient to use compose from Exercise 1-42. +

      and should return 625. Hint: You may find it convenient to use compose from Exercise 1-42.

      -

      -

      Exercise 1.44. The idea of smoothing a function is an important concept in signal processing. If $f$ is a function and $dx$ is some small number, then the smoothed version of f is the function whose value at a point $x$ is the average of $f(x - dx)$, $f(x)$, and $f(x + dx)$. Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f. It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtained the n-fold smoothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from exercise 1.43. +

      +

      Exercise 1.44. The idea of smoothing a function is an important concept in signal processing. If $f$ is a function and $dx$ is some small number, then the smoothed version of f is the function whose value at a point $x$ is the average of $f(x - dx)$, $f(x)$, and $f(x + dx)$. Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f. It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtained the n-fold smoothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from exercise 1.43.

      (define dx 0.1) @@ -1538,8 +1529,8 @@

      Abstractions and first-class procedures

      -

      -

      Exercise 1.45. We saw in section 1.3.3 that attempting to compute square roots by naively finding a fixed point of $y \mapsto \frac{x}{y}$ does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped $y \mapsto \frac{x}{y^2}$. Unfortunately, the process does not work for fourth roots — a single average damp is not enough to make a fixed-point search for $y \mapsto \frac{x}{y^3}$ converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of $y \mapsto \frac{x}{y^3}$) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute $n^{th}$ roots as a fixed-point search based upon repeated average damping of $y \mapsto \frac{x}{y^{n-1}}$. Use this to implement a simple procedure for computing $n^{th}$ roots using fixed-point, average-damp, and the repeated procedure of exercise 1.43. Assume that any arithmetic operations you need are available as primitives. +

      +

      Exercise 1.45. We saw in section 1.3.3 that attempting to compute square roots by naively finding a fixed point of $y \mapsto \frac{x}{y}$ does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-damped $y \mapsto \frac{x}{y^2}$. Unfortunately, the process does not work for fourth roots — a single average damp is not enough to make a fixed-point search for $y \mapsto \frac{x}{y^3}$ converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of $y \mapsto \frac{x}{y^3}$) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute $n^{th}$ roots as a fixed-point search based upon repeated average damping of $y \mapsto \frac{x}{y^{n-1}}$. Use this to implement a simple procedure for computing $n^{th}$ roots using fixed-point, average-damp, and the repeated procedure of exercise 1.43. Assume that any arithmetic operations you need are available as primitives.

      (define (root n x) @@ -1553,8 +1544,8 @@

      Abstractions and first-class procedures

      -

      -

      Exercise 1.46. Several of the numerical methods described in this chapter are instances of an extremely general computational strategy known as iterative improvement. Iterative improvement says that, to compute something, we start with an initial guess for the answer, test if the guess is good enough, and otherwise improve the guess and continue the process using the improved guess as the new guess. Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. Iterative-improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough. Rewrite the sqrt procedure of section 1.1.7 and the fixed-point procedure of section 1.3.3 in terms of iterative-improve. +

      +

      Exercise 1.46. Several of the numerical methods described in this chapter are instances of an extremely general computational strategy known as iterative improvement. Iterative improvement says that, to compute something, we start with an initial guess for the answer, test if the guess is good enough, and otherwise improve the guess and continue the process using the improved guess as the new guess. Write a procedure iterative-improve that takes two procedures as arguments: a method for telling whether a guess is good enough and a method for improving a guess. Iterative-improve should return as its value a procedure that takes a guess as argument and keeps improving the guess until it is good enough. Rewrite the sqrt procedure of section 1.1.7 and the fixed-point procedure of section 1.3.3 in terms of iterative-improve.

      (define (iterative-improve good-enough? improve) @@ -1572,17 +1563,17 @@

      Abstractions and first-class procedures

      49 - This series, usually written in the equivalent form $\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots$, is due to Leibniz. We'll see how to use this as the basis for some fancy numerical tricks in section 3.5.3. + This series, usually written in the equivalent form $\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots$, is due to Leibniz. We'll see how to use this as the basis for some fancy numerical tricks in section 3.5.3.

      50 - Notice that we have used block structure (section 1.1.8) to embed the definitions of pi-next and pi-term within pi-sum, since these procedures are unlikely to be useful for any other purpose. We will see how to get rid of them altogether in section 1.3.2. + Notice that we have used block structure (section 1.1.8) to embed the definitions of pi-next and pi-term within pi-sum, since these procedures are unlikely to be useful for any other purpose. We will see how to get rid of them altogether in section 1.3.2.

      51 - The intent of exercises 1.31-1.33 is to demonstrate the expressive power that is attained by using an appropriate abstraction to consolidate many seemingly disparate operations. However, though accumulation and filtering are elegant ideas, our hands are somewhat tied in using them at this point since we do not yet have data structures to provide suitable means of combination for these abstractions. We will return to these ideas in section 2.2.3 when we show how to use sequences as interfaces for combining filters and accumulators to build even more powerful abstractions. We will see there how these methods really come into their own as a powerful and elegant approach to designing programs. + The intent of exercises 1.31-1.33 is to demonstrate the expressive power that is attained by using an appropriate abstraction to consolidate many seemingly disparate operations. However, though accumulation and filtering are elegant ideas, our hands are somewhat tied in using them at this point since we do not yet have data structures to provide suitable means of combination for these abstractions. We will return to these ideas in section 2.2.3 when we show how to use sequences as interfaces for combining filters and accumulators to build even more powerful abstractions. We will see there how these methods really come into their own as a powerful and elegant approach to designing programs.

      @@ -1592,12 +1583,12 @@

      Abstractions and first-class procedures

      53 - It would be clearer and less intimidating to people learning Lisp if a name more obvious than lambda, such as make-procedure, were used. But the convention is firmly entrenched. The notation is adopted from the $\lambda$ calculus, a mathematical formalism introduced by the mathematical logician Alonzo Church (1941). Church developed the $\lambda$ calculus to provide a rigorous foundation for studying the notions of function and function application. The $\lambda$ calculus has become a basic tool for mathematical investigations of the semantics of programming languages. + It would be clearer and less intimidating to people learning Lisp if a name more obvious than lambda, such as make-procedure, were used. But the convention is firmly entrenched. The notation is adopted from the $\lambda$ calculus, a mathematical formalism introduced by the mathematical logician Alonzo Church (1941). Church developed the $\lambda$ calculus to provide a rigorous foundation for studying the notions of function and function application. The $\lambda$ calculus has become a basic tool for mathematical investigations of the semantics of programming languages.

      54 - Understanding internal definitions well enough to be sure a program means what we intend it to mean requires a more elaborate model of the evaluation process than we have presented in this chapter. The subtleties do not arise with internal definitions of procedures, however. We will return to this issue in section 4.1.6, after we learn more about evaluation. + Understanding internal definitions well enough to be sure a program means what we intend it to mean requires a more elaborate model of the evaluation process than we have presented in this chapter. The subtleties do not arise with internal definitions of procedures, however. We will return to this issue in section 4.1.6, after we learn more about evaluation.

      @@ -1607,27 +1598,27 @@

      Abstractions and first-class procedures

      56 - This can be accomplished using error, which takes as arguments a number of items that are printed as error messages. + This can be accomplished using error, which takes as arguments a number of items that are printed as error messages.

      57 - Try this during a boring lecture: Set your calculator to radians mode and then repeatedly press the cos button until you obtain the fixed point. + Try this during a boring lecture: Set your calculator to radians mode and then repeatedly press the cos button until you obtain the fixed point.

      58 - $\mapsto$ (pronounced “maps to”) is the mathematician's way of writing lambda. $y \mapsto x/y$ means (lambda(y) (/ x y)), that is, the function whose value at $y$ is $x/y$. + $\mapsto$ (pronounced “maps to”) is the mathematician's way of writing lambda. $y \mapsto x/y$ means (lambda(y) (/ x y)), that is, the function whose value at $y$ is $x/y$.

      59 - Observe that this is a combination whose operator is itself a combination. Exercise 1.4 already demonstrated the ability to form such combinations, but that was only a toy example. Here we begin to see the real need for such combinations — when applying a procedure that is obtained as the value returned by a higher-order procedure. + Observe that this is a combination whose operator is itself a combination. Exercise 1.4 already demonstrated the ability to form such combinations, but that was only a toy example. Here we begin to see the real need for such combinations — when applying a procedure that is obtained as the value returned by a higher-order procedure.

      60 - See exercise 1.45 for a further generalization. + See exercise 1.45 for a further generalization.

      @@ -1657,7 +1648,7 @@

      Abstractions and first-class procedures

      66 - The major implementation cost of first-class procedures is that allowing procedures to be returned as values requires reserving storage for a procedure's free variables even while the procedure is not executing. In the Scheme implementation we will study in section 4.1, these variables are stored in the procedure's environment. + The major implementation cost of first-class procedures is that allowing procedures to be returned as values requires reserving storage for a procedure's free variables even while the procedure is not executing. In the Scheme implementation we will study in section 4.1, these variables are stored in the procedure's environment.

      @@ diff --git a/content/2-0-building-abstractions-with-data.content.html b/content/2-0-building-abstractions-with-data.content.html index 57f75b3..c36db0b 100644 --- a/content/2-0-building-abstractions-with-data.content.html +++ b/content/2-0-building-abstractions-with-data.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

      Building Abstractions with Data

      diff --git a/content/2-1-data.content.html b/content/2-1-data.content.html index 1e44c74..e7fa9f1 100644 --- a/content/2-1-data.content.html +++ b/content/2-1-data.content.html @@ -3,22 +3,21 @@ @@ {{main_text}} - + - - + +

      Introduction to Data Abstraction


      -

      In section 1.1.8, we noted that a procedure used as an element in creating a more complex procedure could be regarded not only as a collection of particular operations but also as a procedural abstraction. That is, the details of how the procedure was implemented could be suppressed, and the particular procedure itself could be replaced by any other procedure with the same overall behavior. In other words, we could make an abstraction that would separate the way the procedure would be used from the details of how the procedure would be implemented in terms of more primitive procedures. The analogous notion for compound data is called data abstraction. Data abstraction is a methodology that enables us to isolate how a compound data object is used from the details of how it is constructed from more primitive data objects. +

      In section 1.1.8, we noted that a procedure used as an element in creating a more complex procedure could be regarded not only as a collection of particular operations but also as a procedural abstraction. That is, the details of how the procedure was implemented could be suppressed, and the particular procedure itself could be replaced by any other procedure with the same overall behavior. In other words, we could make an abstraction that would separate the way the procedure would be used from the details of how the procedure would be implemented in terms of more primitive procedures. The analogous notion for compound data is called data abstraction. Data abstraction is a methodology that enables us to isolate how a compound data object is used from the details of how it is constructed from more primitive data objects.

      The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on "abstract data." That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a "concrete" data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation. To illustrate this technique, we will consider how to design a set of procedures for manipulating rational numbers. - -

      2.1.1 Example: Arithmetic Operations for Rational Numbers

      +

      2.1.1 Example: Arithmetic Operations for Rational Numbers

      Suppose we want to do arithmetic with rational numbers. We want to be able to add, subtract, multiply, and divide them and to test whether two rational numbers are equal. @@ -27,20 +26,20 @@

      2.1.1 Example: Arithmetic Operations for Rational Numbers

      • -(make-rat n d) returns the rational number whose -numerator is the integer n and whose denominator is the integer d. +(make-rat n d) returns the rational number whose +numerator is the integer n and whose denominator is the integer d.
      • -(numer x) returns the numerator of the rational number x. +(numer x) returns the numerator of the rational number x.
      • -(denom x) returns the denominator of the rational number x. +(denom x) returns the denominator of the rational number x.
      -

      We are using here a powerful strategy of synthesis: wishful thinking. We haven't yet said how a rational number is represented, or how the procedures numer, denom, and make-rat should be implemented. Even so, if we did have these three procedures, we could then add, subtract, multiply, divide, and test equality by using the following relations: +

      We are using here a powerful strategy of synthesis: wishful thinking. We haven't yet said how a rational number is represented, or how the procedures numer, denom, and make-rat should be implemented. Even so, if we did have these three procedures, we could then add, subtract, multiply, divide, and test equality by using the following relations: $$ \frac{n_1}{d_1} - \frac{n_2}{d_2} = \frac{n_1 d_2 - n_2 d_1}{d_1 d_2} @@ -75,11 +74,11 @@

      2.1.1 Example: Arithmetic Operations for Rational Numbers

      C.prompt("scheme-define-arith-rat", ["scheme-define-rat"]); -

      Now we have the operations on rational numbers defined in terms of the selector and constructor procedures numer, denom, and make-rat. But we haven't yet defined these. What we need is some way to glue together a numerator and a denominator to form a rational number. +

      Now we have the operations on rational numbers defined in terms of the selector and constructor procedures numer, denom, and make-rat. But we haven't yet defined these. What we need is some way to glue together a numerator and a denominator to form a rational number.

      Pairs

      -

      To enable us to implement the concrete level of our data abstraction, our language provides a compound structure called a pair, which can be constructed with the primitive procedure cons. This procedure takes two arguments and returns a compound data object that contains the two arguments as parts. Given a pair, we can extract the parts using the primitive procedures car and cdr. 2 Thus, we can use cons, car, and cdr as follows: +

      To enable us to implement the concrete level of our data abstraction, our language provides a compound structure called a pair, which can be constructed with the primitive procedure cons. This procedure takes two arguments and returns a compound data object that contains the two arguments as parts. Given a pair, we can extract the parts using the primitive procedures car and cdr. 2 Thus, we can use cons, car, and cdr as follows:

      (define x (cons 1 2)) @@ -102,7 +101,7 @@

      Pairs

      C.prompt("scheme-cdr-x", ["scheme-define-x-cons-1-2"]); -

      Notice that a pair is a data object that can be given a name and manipulated, just like a primitive data object. Moreover, cons can be used to form pairs whose elements are pairs, and so on: +

      Notice that a pair is a data object that can be given a name and manipulated, just like a primitive data object. Moreover, cons can be used to form pairs whose elements are pairs, and so on:

      (define x (cons 1 2)) @@ -131,11 +130,11 @@

      Pairs

      C.prompt("scheme-car-cdr-z", ["scheme-define-xyz"]); -

      In section 2.2 we will see how this ability to combine pairs means that pairs can be used as general-purpose building blocks to create all sorts of complex data structures. The single compound-data primitive pair, implemented by the procedures cons, car, and cdr, is the only glue we need. Data objects constructed from pairs are called list-structured data. +

      In section 2.2 we will see how this ability to combine pairs means that pairs can be used as general-purpose building blocks to create all sorts of complex data structures. The single compound-data primitive pair, implemented by the procedures cons, car, and cdr, is the only glue we need. Data objects constructed from pairs are called list-structured data.

      Representing rational numbers

      -

      Pairs offer a natural way to complete the rational-number system. Simply represent a rational number as a pair of two integers: a numerator and a denominator. Then make-rat, numer, and denom are readily implemented as follows:3 +

      Pairs offer a natural way to complete the rational-number system. Simply represent a rational number as a pair of two integers: a numerator and a denominator. Then make-rat, numer, and denom are readily implemented as follows:3

      (define (make-rat n d) (cons n d)) @@ -146,7 +145,7 @@

      Representing rational numbers

      C.prompt("scheme-define-rat"); -

      Also, in order to display the results of our computations, we can print rational numbers by printing the numerator, a slash, and the denominator:4 +

      Also, in order to display the results of our computations, we can print rational numbers by printing the numerator, a slash, and the denominator:4

      (define (print-rat x) @@ -211,7 +210,7 @@

      Representing rational numbers

      C.prompt("scheme-print-third-plus-third", ["scheme-define-one-half", "scheme-define-one-third", "scheme-define-arith-rat", "scheme-define-print-rat"]); -

      As the final example shows, our rational-number implementation does not reduce rational numbers to lowest terms. We can remedy this by changing make-rat. If we have a gcd procedure like the one in section 1.2.5 that produces the greatest common divisor of two integers, we can use gcd to reduce the numerator and the denominator to lowest terms before constructing the pair: +

      As the final example shows, our rational-number implementation does not reduce rational numbers to lowest terms. We can remedy this by changing make-rat. If we have a gcd procedure like the one in section 1.2.5 that produces the greatest common divisor of two integers, we can use gcd to reduce the numerator and the denominator to lowest terms before constructing the pair:

      (define (gcd a b) @@ -241,18 +240,17 @@

      Representing rational numbers

      C.prompt("scheme-test-rat-reduce", ["scheme-define-arith-rat", "scheme-define-one-half", "scheme-define-one-third", "scheme-define-print-rat", "scheme-define-make-rat-reduce"]); -

      as desired. This modification was accomplished by changing the constructor make-rat without changing any of the procedures (such as add-rat and mul-rat) that implement the actual operations. +

      as desired. This modification was accomplished by changing the constructor make-rat without changing any of the procedures (such as add-rat and mul-rat) that implement the actual operations. -

      -

      Exercise 2.1. Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative. +

      +

      Exercise 2.1. Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative.

      - -

      2.1.2 Abstraction Barriers

      +

      2.1.2 Abstraction Barriers

      -

      Before continuing with more examples of compound data and data abstraction, let us consider some of the issues raised by the rational-number example. We defined the rational-number operations in terms of a constructor make-rat and selectors numer and denom. In general, the underlying idea of data abstraction is to identify for each type of data object a basic set of operations in terms of which all manipulations of data objects of that type will be expressed, and then to use only those operations in manipulating the data. +

      Before continuing with more examples of compound data and data abstraction, let us consider some of the issues raised by the rational-number example. We defined the rational-number operations in terms of a constructor make-rat and selectors numer and denom. In general, the underlying idea of data abstraction is to identify for each type of data object a basic set of operations in terms of which all manipulations of data objects of that type will be expressed, and then to use only those operations in manipulating the data. -

      We can envision the structure of the rational-number system as shown in Figure 2-1. The horizontal lines represent abstraction barriers that isolate different "levels" of the system. At each level, the barrier separates the programs (above) that use the data abstraction from the programs (below) that implement the data abstraction. Programs that use rational numbers manipulate them solely in terms of the procedures supplied "for public use" by the rational-number package: add-rat, sub-rat, mul-rat, div-rat, and equal-rat?. These, in turn, are implemented solely in terms of the constructor and selectors make-rat, numer, and denom, which themselves are implemented in terms of pairs. The details of how pairs are implemented are irrelevant to the rest of the rational-number package so long as pairs can be manipulated by the use of cons, car, and cdr. In effect, procedures at each level are the interfaces that define the abstraction barriers and connect the different levels. +

      We can envision the structure of the rational-number system as shown in Figure 2-1. The horizontal lines represent abstraction barriers that isolate different "levels" of the system. At each level, the barrier separates the programs (above) that use the data abstraction from the programs (below) that implement the data abstraction. Programs that use rational numbers manipulate them solely in terms of the procedures supplied "for public use" by the rational-number package: add-rat, sub-rat, mul-rat, div-rat, and equal-rat?. These, in turn, are implemented solely in terms of the constructor and selectors make-rat, numer, and denom, which themselves are implemented in terms of pairs. The details of how pairs are implemented are irrelevant to the rest of the rational-number package so long as pairs can be manipulated by the use of cons, car, and cdr. In effect, procedures at each level are the interfaces that define the abstraction barriers and connect the different levels.

      @@ -278,12 +276,12 @@

      2.1.2 Abstraction Barriers

      C.prompt("scheme-define-rat-reduce-in-constructor", ["scheme-define-gcd"]); -

      The difference between this implementation and the previous one lies in when we compute the gcd. If in our typical use of rational numbers we access the numerators and denominators of the same rational numbers many times, it would be preferable to compute the gcd when the rational numbers are constructed. If not, we may be better off waiting until access time to compute the gcd. In any case, when we change from one representation to the other, the procedures add-rat, sub-rat, and so on do not have to be modified at all. +

      The difference between this implementation and the previous one lies in when we compute the gcd. If in our typical use of rational numbers we access the numerators and denominators of the same rational numbers many times, it would be preferable to compute the gcd when the rational numbers are constructed. If not, we may be better off waiting until access time to compute the gcd. In any case, when we change from one representation to the other, the procedures add-rat, sub-rat, and so on do not have to be modified at all. -

      Constraining the dependence on the representation to a few interface procedures helps us design programs as well as modify them, because it allows us to maintain the flexibility to consider alternate implementations. To continue with our simple example, suppose we are designing a rational-number package and we can't decide initially whether to perform the gcd at construction time or at selection time. The data-abstraction methodology gives us a way to defer that decision without losing the ability to make progress on the rest of the system. +

      Constraining the dependence on the representation to a few interface procedures helps us design programs as well as modify them, because it allows us to maintain the flexibility to consider alternate implementations. To continue with our simple example, suppose we are designing a rational-number package and we can't decide initially whether to perform the gcd at construction time or at selection time. The data-abstraction methodology gives us a way to defer that decision without losing the ability to make progress on the rest of the system. -

      -

      Exercise 2.2. Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points: +

      +

      Exercise 2.2. Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points:

      (define (print-point p) @@ -301,24 +299,23 @@

      2.1.2 Abstraction Barriers

      -

      +

      Exercise 2.3. Implement a representation for rectangles in a plane. (Hint: You may want to make use of Exercise 2-2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?

      -
      -

      2.1.3 What Is Meant by Data?

      +

      2.1.3 What Is Meant by Data?

      -

      We began the rational-number implementation in section 2.1.1 by implementing the rational-number operations add-rat, sub-rat, and so on in terms of three unspecified procedures: make-rat, numer, and denom. At that point, we could think of the operations as being defined in terms of data objects---numerators, denominators, and rational numbers---whose behavior was specified by the latter three procedures. +

      We began the rational-number implementation in section 2.1.1 by implementing the rational-number operations add-rat, sub-rat, and so on in terms of three unspecified procedures: make-rat, numer, and denom. At that point, we could think of the operations as being defined in terms of data objects---numerators, denominators, and rational numbers---whose behavior was specified by the latter three procedures. -

      But exactly what is meant by data ? It is not enough to say "whatever is implemented by the given selectors and constructors." Clearly, not every arbitrary set of three procedures can serve as an appropriate basis for the rational-number implementation. We need to guarantee that, if we construct a rational number x from a pair of integers n and d, then extracting the numer and the denom of x and dividing them should yield the same result as dividing n by d. In other words, make-rat, numer, and denom must satisfy the condition that, for any integer n and any non-zero integer d, if x is (make-rat n d), then +

      But exactly what is meant by data ? It is not enough to say "whatever is implemented by the given selectors and constructors." Clearly, not every arbitrary set of three procedures can serve as an appropriate basis for the rational-number implementation. We need to guarantee that, if we construct a rational number x from a pair of integers n and d, then extracting the numer and the denom of x and dividing them should yield the same result as dividing n by d. In other words, make-rat, numer, and denom must satisfy the condition that, for any integer n and any non-zero integer d, if x is (make-rat n d), then $$ \frac{\text{(numer x)}}{\text{(denom x)}} = \frac{n}{d} $$ -

      In fact, this is the only condition make-rat, numer, and denom must fulfill in order to form a suitable basis for a rational-number representation. In general, we can think of data as defined by some collection of selectors and constructors, together with specified conditions that these procedures must fulfill in order to be a valid representation. 5 +

      In fact, this is the only condition make-rat, numer, and denom must fulfill in order to form a suitable basis for a rational-number representation. In general, we can think of data as defined by some collection of selectors and constructors, together with specified conditions that these procedures must fulfill in order to be a valid representation. 5 -

      This point of view can serve to define not only "high-level" data objects, such as rational numbers, but lower-level objects as well. Consider the notion of a pair, which we used in order to define our rational numbers. We never actually said what a pair was, only that the language supplied procedures cons, car, and cdr for operating on pairs. But the only thing we need to know about these three operations is that if we glue two objects together using cons we can retrieve the objects using car and cdr. That is, the operations satisfy the condition that, for any objects x and y, if z is (cons x y) then (car z) is x and (cdr z) is y. Indeed, we mentioned that these three procedures are included as primitives in our language. However, any triple of procedures that satisfies the above condition can be used as the basis for implementing pairs. This point is illustrated strikingly by the fact that we could implement cons, car, and cdr without using any data structures at all but only using procedures. Here are the definitions: +

      This point of view can serve to define not only "high-level" data objects, such as rational numbers, but lower-level objects as well. Consider the notion of a pair, which we used in order to define our rational numbers. We never actually said what a pair was, only that the language supplied procedures cons, car, and cdr for operating on pairs. But the only thing we need to know about these three operations is that if we glue two objects together using cons we can retrieve the objects using car and cdr. That is, the operations satisfy the condition that, for any objects x and y, if z is (cons x y) then (car z) is x and (cdr z) is y. Indeed, we mentioned that these three procedures are included as primitives in our language. However, any triple of procedures that satisfies the above condition can be used as the basis for implementing pairs. This point is illustrated strikingly by the fact that we could implement cons, car, and cdr without using any data structures at all but only using procedures. Here are the definitions:

      (define (cons x y) @@ -338,12 +335,12 @@

      2.1.3 What Is Meant by Data?

      This use of procedures corresponds to nothing like our intuitive notion of what data should be. Nevertheless, all we need to do to show that this is a valid way to represent pairs is to verify that these procedures satisfy the condition given above. -

      The subtle point to notice is that the value returned by (cons x y) is a procedure---namely the internally defined procedure dispatch, which takes one argument and returns either x or y depending on whether the argument is 0 or 1. Correspondingly, (car z) is defined to apply z to 0. Hence, if z is the procedure formed by (cons x y), then z applied to 0 will yield x. Thus, we have shown that (car (cons x y)) yields x, as desired. Similarly, (cdr (cons x y)) applies the procedure returned by (cons x y) to 1, which returns y. Therefore, this procedural implementation of pairs is a valid implementation, and if we access pairs using only cons, car, and cdr we cannot distinguish this implementation from one that uses "real" data structures. +

      The subtle point to notice is that the value returned by (cons x y) is a procedure---namely the internally defined procedure dispatch, which takes one argument and returns either x or y depending on whether the argument is 0 or 1. Correspondingly, (car z) is defined to apply z to 0. Hence, if z is the procedure formed by (cons x y), then z applied to 0 will yield x. Thus, we have shown that (car (cons x y)) yields x, as desired. Similarly, (cdr (cons x y)) applies the procedure returned by (cons x y) to 1, which returns y. Therefore, this procedural implementation of pairs is a valid implementation, and if we access pairs using only cons, car, and cdr we cannot distinguish this implementation from one that uses "real" data structures.

      The point of exhibiting the procedural representation of pairs is not that our language works this way (Scheme, and Lisp systems in general, implement pairs directly, for efficiency reasons) but that it could work this way. The procedural representation, although obscure, is a perfectly adequate way to represent pairs, since it fulfills the only conditions that pairs need to fulfill. This example also demonstrates that the ability to manipulate procedures as objects automatically provides the ability to represent compound data. This may seem a curiosity now, but procedural representations of data will play a central role in our programming repertoire. This style of programming is often called message passing , and we will be using it as a basic tool in Chapter 3 when we address the issues of modeling and simulation. -

      -

      Exercise 2.4. Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons x y)) yields x for any objects x and y. +

      +

      Exercise 2.4. Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons x y)) yields x for any objects x and y.

      (define (cons x y) @@ -356,7 +353,7 @@

      2.1.3 What Is Meant by Data?

      C.prompt("scheme-define-cons-lambda"); -

      What is the corresponding definition of cdr? +

      What is the corresponding definition of cdr?

      (define (cdr z) @@ -390,8 +387,8 @@

      2.1.3 What Is Meant by Data?

      -

      -

      Exercise 2.5. Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair $a$ and $b$ as the integer that is the product $2^a 3^b$. Give the corresponding definitions of the procedures cons, car, and cdr. +

      +

      Exercise 2.5. Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair $a$ and $b$ as the integer that is the product $2^a 3^b$. Give the corresponding definitions of the procedures cons, car, and cdr.

      (define (cons a b) @@ -425,7 +422,7 @@

      2.1.3 What Is Meant by Data?

      -

      +

      Exercise 2.6. In case representing pairs as procedures wasn't mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative integers are concerned) by implementing $0$ and the operation of adding $1$ as

      @@ -449,11 +446,10 @@

      2.1.3 What Is Meant by Data?

      This representation is known as Church numerals , after its inventor, Alonzo Church, the logician who invented the lambda calculus. -

      Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1). +

      Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate (add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated application of add-1).

      - -

      2.1.4 Extended Exercise: Interval Arithmetic

      +

      2.1.4 Extended Exercise: Interval Arithmetic

      Alyssa P. Hacker is designing a system to help people solve engineering problems. One feature she wants to provide in her system is the ability to manipulate inexact quantities (such as measured parameters of physical devices) with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision. @@ -467,7 +463,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      Alyssa's idea is to implement "interval arithmetic" as a set of arithmetic operations for combining "intervals" (objects that represent the range of possible values of an inexact quantity). The result of adding, subtracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result. -

      Alyssa postulates the existence of an abstract object called an "interval" that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor make-interval. Alyssa first writes a procedure for adding two intervals. She reasons that the minimum value the sum could be is the sum of the two lower bounds and the maximum value it could be is the sum of the two upper bounds: +

      Alyssa postulates the existence of an abstract object called an "interval" that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor make-interval. Alyssa first writes a procedure for adding two intervals. She reasons that the minimum value the sum could be is the sum of the two lower bounds and the maximum value it could be is the sum of the two upper bounds:

      (define (add-interval x y) @@ -478,7 +474,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      C.prompt("scheme-define-add-interval"); -

      Alyssa also works out the product of two intervals by finding the minimum and the maximum of the products of the bounds and using them as the bounds of the resulting interval. (Min and max are primitives that find the minimum or maximum of any number of arguments.) +

      Alyssa also works out the product of two intervals by finding the minimum and the maximum of the products of the bounds and using them as the bounds of the resulting interval. (Min and max are primitives that find the minimum or maximum of any number of arguments.)

      (define (mul-interval x y) @@ -507,7 +503,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      +

      Exercise 2.7: Alyssa's program is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor:

      @@ -517,7 +513,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      C.prompt("scheme-define-make-interval"); -

      Define selectors upper-bound and lower-bound to complete the implementation. +

      Define selectors upper-bound and lower-bound to complete the implementation.

      (define (lower-bound interval) @@ -553,8 +549,8 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      -

      Exercise 2.8: Using reasoning analogous to Alyssa's, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval. +

      +

      Exercise 2.8: Using reasoning analogous to Alyssa's, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval.

      (define (sub-interval) @@ -588,20 +584,20 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      -

      Exercise 2.9: The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division. +

      +

      Exercise 2.9: The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.

      -

      +

      Exercise 2.10: Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans (includes) zero. Modify Alyssa's code to check for this condition and to signal an error if it occurs.

      -

      -

      Exercise 2.11: In passing, Ben also cryptically comments: "By testing the signs of the endpoints of the intervals, it is possible to break mul-interval into nine cases, only one of which requires more than two multiplications." Rewrite this procedure using Ben's suggestion. +

      +

      Exercise 2.11: In passing, Ben also cryptically comments: "By testing the signs of the endpoints of the intervals, it is possible to break mul-interval into nine cases, only one of which requires more than two multiplications." Rewrite this procedure using Ben's suggestion.

      After debugging her program, Alyssa shows it to a potential user, who complains that her program solves the wrong problem. He wants a program that can deal with numbers represented as a center value and an additive tolerance; for example, he wants to work with intervals such as 3.5 +/- 0.15 rather than [3.35, 3.65]. Alyssa returns to her desk and fixes this problem by supplying an alternate constructor and alternate selectors: @@ -624,8 +620,8 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      -

      Exercise 2.12: Define a constructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above. +

      +

      Exercise 2.12: Define a constructor make-center-percent that takes a center and a percentage tolerance and produces the desired interval. You must also define a selector percent that produces the percentage tolerance for a given interval. The center selector is the same as the one shown above.

      @@ -670,7 +666,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      +

      Exercise 2.13: Show that under the assumption of small percentage tolerances there is a simple formula for the approximate percentage tolerance of the product of two intervals in terms of the tolerances of the factors. You may simplify the problem by assuming that all numbers are positive.

      After considerable work, Alyssa P. Hacker delivers her finished system. Several years later, after she has forgotten all about it, she gets a frenzied call from an irate user, Lem E. Tweakit. It seems that Lem has noticed that the formula for parallel resistors can be written in two algebraically equivalent ways: @@ -707,19 +703,19 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      -

      +

      Exercise 2.14: Demonstrate that Lem is right. Investigate the behavior of the system on a variety of arithmetic expressions. Make some intervals A and B, and use them in computing the expressions A/A and A/B. You will get the most insight by using intervals whose width is a small percentage of the center value. Examine the results of the computation in center-percent form (see Exercise 2-12).

      -

      -

      Exercise 2.15: Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa's system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a "better" program for parallel resistances than par1. Is she right? Why? +

      +

      Exercise 2.15: Eva Lu Ator, another user, has also noticed the different intervals computed by different but algebraically equivalent expressions. She says that a formula to compute with intervals using Alyssa's system will produce tighter error bounds if it can be written in such a form that no variable that represents an uncertain number is repeated. Thus, she says, par2 is a "better" program for parallel resistances than par1. Is she right? Why?

      -

      +

      Exercise 2.16: Explain, in general, why equivalent algebraic expressions may lead to different answers. Can you devise an interval-arithmetic package that does not have this shortcoming, or is this task impossible? (Warning: This problem is very difficult.)

      @@ -729,7 +725,7 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      {{footnotes}}
      -

      2 The name cons stands for "construct." The names car and cdr derive from the original implementation of Lisp on the IBM 704. That machine had an addressing scheme that allowed one to reference the "address" and "decrement" parts of a memory location. Car stands for "Contents of Address part of Register" and cdr (pronounced "could-er") stands for "Contents of Decrement part of Register." +

      2 The name cons stands for "construct." The names car and cdr derive from the original implementation of Lisp on the IBM 704. That machine had an addressing scheme that allowed one to reference the "address" and "decrement" parts of a memory location. Car stands for "Contents of Address part of Register" and cdr (pronounced "could-er") stands for "Contents of Decrement part of Register."

      @@ -744,15 +740,15 @@

      2.1.4 Extended Exercise: Interval Arithmetic

      C.prompt("scheme-define-rat-pointless"); -

      The first definition associates the name make-rat with the value of the expression cons, which is the primitive procedure that constructs pairs. Thus make-rat and cons are names for the same primitive constructor. +

      The first definition associates the name make-rat with the value of the expression cons, which is the primitive procedure that constructs pairs. Thus make-rat and cons are names for the same primitive constructor. -

      Defining selectors and constructors in this way is efficient: Instead of make-rat calling cons, make-rat is cons, so there is only one procedure called, not two, when make-rat is called. On the other hand, doing this defeats debugging aids that trace procedure calls or put breakpoints on procedure calls: You may want to watch make-rat being called, but you certainly don't want to watch every call to cons. +

      Defining selectors and constructors in this way is efficient: Instead of make-rat calling cons, make-rat is cons, so there is only one procedure called, not two, when make-rat is called. On the other hand, doing this defeats debugging aids that trace procedure calls or put breakpoints on procedure calls: You may want to watch make-rat being called, but you certainly don't want to watch every call to cons. We have chosen not to use this style of definition in this book.

      -

      4 display is the Scheme primitive for printing data. The Scheme primitive newline starts a new line for printing. Neither of these procedures returns a useful value, so in the uses of print-rat below, we show only what print-rat prints, not what the interpreter prints as the value returned by print-rat.

      +

      4 display is the Scheme primitive for printing data. The Scheme primitive newline starts a new line for printing. Neither of these procedures returns a useful value, so in the uses of print-rat below, we show only what print-rat prints, not what the interpreter prints as the value returned by print-rat.

      5 Surprisingly, this idea is very difficult to formulate rigorously. There are two approaches to giving such a formulation. One, pioneered by C. A. R. Hoare (1972), is known as the method of abstract models. It formalizes the "procedures plus conditions" specification as outlined in the rational-number example above. Note that the condition on the rational-number representation was stated in terms of facts about integers (equality and division). In general, abstract models define new kinds of data objects in terms of previously defined types of data objects. Assertions about data objects can therefore be checked by reducing them to assertions about previously defined data objects. Another approach, introduced by Zilles at MIT, by Goguen, Thatcher, Wagner, and Wright at IBM (see Thatcher, Wagner, and Wright 1978), and by Guttag at Toronto (see Guttag 1977), is called algebraic specification. It regards the "procedures" as elements of an abstract algebraic system whose behavior is specified by axioms that correspond to our "conditions," and uses the techniques of abstract algebra to check assertions about data objects. Both methods are surveyed in the paper by Liskov and Zilles (1975). diff --git a/content/2-2-closure.content.html b/content/2-2-closure.content.html index f795fbd..3ef2db5 100644 --- a/content/2-2-closure.content.html +++ b/content/2-2-closure.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

      @@ -23,30 +23,29 @@

      Hierarchical Data and the Closure Property


      -

      As we have seen, pairs provide a primitive "glue" that we can use to construct compound data objects. Figure 2-2 shows a standard way to visualize a pair---in this case, the pair formed by (cons 1 2). In this representation, which is called box-and-pointer notation , each object is shown as a pointer to a box. The box for a primitive object contains a representation of the object. For example, the box for a number contains a numeral. The box for a pair is actually a double box, the left part containing (a pointer to) the car of the pair and the right part containing the cdr. +

      As we have seen, pairs provide a primitive "glue" that we can use to construct compound data objects. Figure 2-2 shows a standard way to visualize a pair---in this case, the pair formed by (cons 1 2). In this representation, which is called box-and-pointer notation , each object is shown as a pointer to a box. The box for a primitive object contains a representation of the object. For example, the box for a number contains a numeral. The box for a pair is actually a double box, the left part containing (a pointer to) the car of the pair and the right part containing the cdr.

      -

      Figure 2.2: Box-and-pointer representation of (cons 1 2). +

      Figure 2.2: Box-and-pointer representation of (cons 1 2). -

      We have already seen that cons can be used to combine not only numbers but pairs as well. (You made use of this fact, or should have, in doing Exercise 2-2 and Exercise 2-3.) As a consequence, pairs provide a universal building block from which we can construct all sorts of data structures. Figure 2-3 shows two ways to use pairs to combine the numbers 1, 2, 3, and 4. +

      We have already seen that cons can be used to combine not only numbers but pairs as well. (You made use of this fact, or should have, in doing Exercise 2-2 and Exercise 2-3.) As a consequence, pairs provide a universal building block from which we can construct all sorts of data structures. Figure 2-3 shows two ways to use pairs to combine the numbers 1, 2, 3, and 4.

      Figure 2.3: Two ways to combine 1, 2, 3, and 4 using pairs. -

      The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.6 Closure is the key to power in any means of combination because it permits us to create hierarchical structures---structures made up of parts, which themselves are made up of parts, and so on. +

      The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation.6 Closure is the key to power in any means of combination because it permits us to create hierarchical structures---structures made up of parts, which themselves are made up of parts, and so on. -

      From the outset of Chapter 1, we've made essential use of closure in dealing with procedures, because all but the very simplest programs rely on the fact that the elements of a combination can themselves be combinations. In this section, we take up the consequences of closure for compound data. We describe some conventional techniques for using pairs to represent sequences and trees, and we exhibit a graphics language that illustrates closure in a vivid way.7 +

      From the outset of Chapter 1, we've made essential use of closure in dealing with procedures, because all but the very simplest programs rely on the fact that the elements of a combination can themselves be combinations. In this section, we take up the consequences of closure for compound data. We describe some conventional techniques for using pairs to represent sequences and trees, and we exhibit a graphics language that illustrates closure in a vivid way.7 - -

      2.2.1 Representing Sequences

      +

      2.2.1 Representing Sequences

      Figure 2.4: The sequence 1, 2, 3, 4 represented as a chain of pairs. -

      One of the useful structures we can build with pairs is a sequence ---an ordered collection of data objects. There are, of course, many ways to represent sequences in terms of pairs. One particularly straightforward representation is illustrated in Figure 2-4, where the sequence 1, 2, 3, 4 is represented as a chain of pairs. The car of each pair is the corresponding item in the chain, and the cdr of the pair is the next pair in the chain. The cdr of the final pair signals the end of the sequence by pointing to a distinguished value that is not a pair, represented in box-and-pointer diagrams as a diagonal line and in programs as the value of the variable nil. The entire sequence is constructed by nested cons operations: +

      One of the useful structures we can build with pairs is a sequence ---an ordered collection of data objects. There are, of course, many ways to represent sequences in terms of pairs. One particularly straightforward representation is illustrated in Figure 2-4, where the sequence 1, 2, 3, 4 is represented as a chain of pairs. The car of each pair is the corresponding item in the chain, and the cdr of the pair is the next pair in the chain. The cdr of the final pair signals the end of the sequence by pointing to a distinguished value that is not a pair, represented in box-and-pointer diagrams as a diagonal line and in programs as the value of the variable nil. The entire sequence is constructed by nested cons operations:

      (cons 1 @@ -58,7 +57,7 @@

      2.2.1 Representing Sequences

      C.prompt("scheme-cons-1-2-3-4"); -

      Such a sequence of pairs, formed by nested conses, is called a list , and Scheme provides a primitive called list to help in constructing lists.8 The above sequence could be produced by (list 1 2 3 4). In general, +

      Such a sequence of pairs, formed by nested conses, is called a list , and Scheme provides a primitive called list to help in constructing lists.8 The above sequence could be produced by (list 1 2 3 4). In general,

      (list <a_1> <a_2> ... <a_n>) @@ -81,7 +80,7 @@

      2.2.1 Representing Sequences

      C.make_static("scheme-list-syntax-expand"); -

      Lisp systems conventionally print lists by printing the sequence of elements, enclosed in parentheses. Thus, the data object in Figure 2-4 is printed as (1 2 3 4): +

      Lisp systems conventionally print lists by printing the sequence of elements, enclosed in parentheses. Thus, the data object in Figure 2-4 is printed as (1 2 3 4):

      (define one-through-four (list 1 2 3 4)) @@ -99,9 +98,9 @@

      2.2.1 Representing Sequences

      C.prompt("scheme-test-one-through-four", ["scheme-define-one-through-four"]); -

      Be careful not to confuse the expression (list 1 2 3 4) with the list (1 2 3 4), which is the result obtained when the expression is evaluated. Attempting to evaluate the expression (1 2 3 4) will signal an error when the interpreter tries to apply the procedure 1 to arguments 2, 3, and 4. +

      Be careful not to confuse the expression (list 1 2 3 4) with the list (1 2 3 4), which is the result obtained when the expression is evaluated. Attempting to evaluate the expression (1 2 3 4) will signal an error when the interpreter tries to apply the procedure 1 to arguments 2, 3, and 4. -

      We can think of car as selecting the first item in the list, and of cdr as selecting the sublist consisting of all but the first item. Nested applications of car and cdr can be used to extract the second, third, and subsequent items in the list.9 The constructor cons makes a list like the original one, but with an additional item at the beginning. +

      We can think of car as selecting the first item in the list, and of cdr as selecting the sublist consisting of all but the first item. Nested applications of car and cdr can be used to extract the second, third, and subsequent items in the list.9 The constructor cons makes a list like the original one, but with an additional item at the beginning.

      (car one-through-four) @@ -146,20 +145,20 @@

      2.2.1 Representing Sequences

      C.prompt("scheme-cons-5-one-through-four", ["scheme-define-one-through-four"]); -

      The value of nil, used to terminate the chain of pairs, can be thought of as a sequence of no elements, the empty list . The word nil is a contraction of the Latin word nihil, which means "nothing".10 +

      The value of nil, used to terminate the chain of pairs, can be thought of as a sequence of no elements, the empty list . The word nil is a contraction of the Latin word nihil, which means "nothing".10

      List operations

      -

      The use of pairs to represent sequences of elements as lists is accompanied by conventional programming techniques for manipulating lists by successively "cdring down" the lists. For example, the procedure list-ref takes as arguments a list and a number n and returns the nth item of the list. It is customary to number the elements of the list beginning with 0. The method for computing list-ref is the following: +

      The use of pairs to represent sequences of elements as lists is accompanied by conventional programming techniques for manipulating lists by successively "cdring down" the lists. For example, the procedure list-ref takes as arguments a list and a number n and returns the nth item of the list. It is customary to number the elements of the list beginning with 0. The method for computing list-ref is the following:

      • -

        For n = 0, list-ref should return the car of the list. +

        For n = 0, list-ref should return the car of the list.

      • -

        Otherwise, list-ref should return the (n - 1)st item of the cdr of the list. +

        Otherwise, list-ref should return the (n - 1)st item of the cdr of the list.

      @@ -187,7 +186,7 @@

      List operations

      C.prompt("scheme-list-ref-squares-3", ["scheme-define-list-ref", "scheme-define-squares"]); -

      Often we cdr down the whole list. To aid in this, Scheme includes a primitive predicate null?, which tests whether its argument is the empty list. The procedure length, which returns the number of items in a list, illustrates this typical pattern of use: +

      Often we cdr down the whole list. To aid in this, Scheme includes a primitive predicate null?, which tests whether its argument is the empty list. The procedure length, which returns the number of items in a list, illustrates this typical pattern of use:

      (define (length items) @@ -216,12 +215,12 @@

      List operations

      -

      The length procedure implements a simple recursive plan. The reduction step is: +

      The length procedure implements a simple recursive plan. The reduction step is:

      • -

        The length of any list is 1 plus the length of the cdr of the list. +

        The length of any list is 1 plus the length of the cdr of the list.

      @@ -231,12 +230,12 @@

      List operations

      • -

        The length of the empty list is 0. +

        The length of the empty list is 0.

      -

      We could also compute length in an iterative style: +

      We could also compute length in an iterative style:

      (define (length items) @@ -250,7 +249,7 @@

      List operations

      C.prompt("scheme-define-length-iterative"); -

      Another conventional programming technique is to "cons up" an answer list while cdring down a list, as in the procedure append, which takes two lists as arguments and combines their elements to make a new list: +

      Another conventional programming technique is to "cons up" an answer list while cdring down a list, as in the procedure append, which takes two lists as arguments and combines their elements to make a new list:

      (append squares odds) @@ -268,15 +267,15 @@

      List operations

      C.prompt("scheme-append-odds-squares", ["scheme-define-squares", "scheme-define-odds"]); -

      append is also implemented using a recursive plan. To append lists list1 and list2, do the following: +

      append is also implemented using a recursive plan. To append lists list1 and list2, do the following:

      • -

        If list1 is the empty list, then the result is just list2. +

        If list1 is the empty list, then the result is just list2.

      • -

        Otherwise, append the cdr of list1 and list2, and cons the car of list1 onto the result: +

        Otherwise, append the cdr of list1 and list2, and cons the car of list1 onto the result:

      @@ -294,8 +293,8 @@

      List operations

      -

      -

      Exercise 2.17: Define a procedure last-pair that returns the list that contains only the last element of a given (nonempty) list: +

      +

      Exercise 2.17: Define a procedure last-pair that returns the list that contains only the last element of a given (nonempty) list:

      (define (last-pair l) @@ -330,8 +329,8 @@

      List operations

      -

      -

      Exercise 2.18: Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order: +

      +

      Exercise 2.18: Define a procedure reverse that takes a list as argument and returns a list of the same elements in reverse order:

      (define (reverse l) @@ -373,10 +372,10 @@

      List operations

      -

      -

      Exercise 2.19: Consider the change-counting program of section 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change. +

      +

      Exercise 2.19: Consider the change-counting program of section 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change. -

      We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency: +

      We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

      (define us-coins (list 50 25 10 5 1)) @@ -387,7 +386,7 @@

      List operations

      C.prompt("scheme-define-coins"); -

      We could then call cc as follows: +

      We could then call cc as follows:

      (cc 100 us-coins) ;292 @@ -396,7 +395,7 @@

      List operations

      C.prompt("scheme-cc-us-coins", ["scheme-define-coins", "scheme-ex-define-cc"]); -

      To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows: +

      To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

      (define (cc amount coin-values) @@ -410,7 +409,7 @@

      List operations

      coin-values)))))
      -

      Define the procedures first-denomination, except-first-denomination, and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not? +

      Define the procedures first-denomination, except-first-denomination, and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not?

      (define no-more? (lambda (x) true)) @@ -452,8 +451,8 @@

      List operations

      -

      -

      Exercise 2.20: The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation . In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter's value will be a list of any remaining arguments. For instance, given the definition +

      +

      Exercise 2.20: The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation . In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter's value will be a list of any remaining arguments. For instance, given the definition

      (define (f x y . z) <body>) @@ -462,7 +461,7 @@

      List operations

      C.no_output_frozen_prompt("scheme-dotted-tail-syntax"); -

      the procedure f can be called with two or more arguments. If we evaluate +

      the procedure f can be called with two or more arguments. If we evaluate

      (f 1 2 3 4 5 6) @@ -471,7 +470,7 @@

      List operations

      C.make_static("scheme-dotted-tail-application"); -

      then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition +

      then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition

      (define (g . w) <body>) @@ -480,7 +479,7 @@

      List operations

      C.make_static("scheme-define-dotted-tail-syntax-one"); -

      the procedure g can be called with zero or more arguments. If we evaluate +

      the procedure g can be called with zero or more arguments. If we evaluate

      (g 1 2 3 4 5 6) @@ -489,9 +488,9 @@

      List operations

      C.no_output_frozen_prompt("scheme-g"); -

      then in the body of g, w will be the list (1 2 3 4 5 6).11 +

      then in the body of g, w will be the list (1 2 3 4 5 6).11 -

      Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example, +

      Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example,

      (define (same-parity x . y) @@ -548,7 +547,7 @@

      Mapping over lists

      C.prompt("scheme-define-scale-list"); -

      We can abstract this general idea and capture it as a common pattern expressed as a higher-order procedure, just as in section 1.3. The higher-order procedure here is called map. map takes as arguments a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list:12 +

      We can abstract this general idea and capture it as a common pattern expressed as a higher-order procedure, just as in section 1.3. The higher-order procedure here is called map. map takes as arguments a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list:12

      (define (map proc items) @@ -580,7 +579,7 @@

      Mapping over lists

      C.prompt("scheme-map-square", ["scheme-define-map"]); -

      Now we can give a new definition of scale-list in terms of map: +

      Now we can give a new definition of scale-list in terms of map:

      (define (scale-list items factor) @@ -591,10 +590,10 @@

      Mapping over lists

      C.prompt("scheme-define-scale-list-with-map"); -

      map is an important construct, not only because it captures a common pattern, but because it establishes a higher level of abstraction in dealing with lists. In the original definition of scale-list, the recursive structure of the program draws attention to the element-by-element processing of the list. Defining scale-list in terms of map suppresses that level of detail and emphasizes that scaling transforms a list of elements to a list of results. The difference between the two definitions is not that the computer is performing a different process (it isn't) but that we think about the process differently. In effect, map helps establish an abstraction barrier that isolates the implementation of procedures that transform lists from the details of how the elements of the list are extracted and combined. Like the barriers shown in Figure 2-1, this abstraction gives us the flexibility to change the low-level details of how sequences are implemented, while preserving the conceptual framework of operations that transform sequences to sequences. Section 2.2.3 expands on this use of sequences as a framework for organizing programs. +

      map is an important construct, not only because it captures a common pattern, but because it establishes a higher level of abstraction in dealing with lists. In the original definition of scale-list, the recursive structure of the program draws attention to the element-by-element processing of the list. Defining scale-list in terms of map suppresses that level of detail and emphasizes that scaling transforms a list of elements to a list of results. The difference between the two definitions is not that the computer is performing a different process (it isn't) but that we think about the process differently. In effect, map helps establish an abstraction barrier that isolates the implementation of procedures that transform lists from the details of how the elements of the list are extracted and combined. Like the barriers shown in Figure 2-1, this abstraction gives us the flexibility to change the low-level details of how sequences are implemented, while preserving the conceptual framework of operations that transform sequences to sequences. Section 2.2.3 expands on this use of sequences as a framework for organizing programs. -

      -

      Exercise 2.21: The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers. +

      +

      Exercise 2.21: The procedure square-list takes a list of numbers as argument and returns a list of the squares of those numbers.

      (define (square-list items) @@ -612,7 +611,7 @@

      Mapping over lists

      C.prompt("scheme-test-square-list", ["scheme-hidden-define-square-list"]); -

      Here are two different definitions of square-list. Complete both of them by filling in the missing expressions: +

      Here are two different definitions of square-list. Complete both of them by filling in the missing expressions:

      (define (square-list items) @@ -657,8 +656,8 @@

      Mapping over lists

      -

      -

      Exercise 2.22: Louis Reasoner tries to rewrite the first square-list procedure of Exercise 2-21 so that it evolves an iterative process: +

      +

      Exercise 2.22: Louis Reasoner tries to rewrite the first square-list procedure of Exercise 2-21 so that it evolves an iterative process:

      (define (square-list items) @@ -674,9 +673,9 @@

      Mapping over lists

      C.prompt("scheme-define-louise-square-list", ["scheme-define-square"]); -

      Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why? +

      Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why? -

      Louis then tries to fix his bug by interchanging the arguments to cons: +

      Louis then tries to fix his bug by interchanging the arguments to cons:

      (define (square-list items) @@ -697,8 +696,8 @@

      Mapping over lists

      -

      -

      Exercise 2.23: The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all – for-each is used with procedures that perform an action, such as printing. For example, +

      +

      Exercise 2.23: The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all – for-each is used with procedures that perform an action, such as printing. For example,

      (for-each (lambda (x) (newline) (display x)) @@ -708,7 +707,7 @@

      Mapping over lists

      C.prompt("scheme-test-for-each", ["scheme-ex-define-for-each"]); -

      should print 57, 321 and 88. The value returned by the call to for-each should be true. Give an implementation of for-each. +

      should print 57, 321 and 88. The value returned by the call to for-each should be true. Give an implementation of for-each.

      (define (for-each proc lst) @@ -751,10 +750,9 @@

      Mapping over lists

      - -

      2.2.2 Hierarchical Structures

      +

      2.2.2 Hierarchical Structures

      -

      The representation of sequences in terms of lists generalizes naturally to represent sequences whose elements may themselves be sequences. For example, we can regard the object ((1 2) 3 4) constructed by +

      The representation of sequences in terms of lists generalizes naturally to represent sequences whose elements may themselves be sequences. For example, we can regard the object ((1 2) 3 4) constructed by

      (cons (list 1 2) (list 3 4)) @@ -763,19 +761,19 @@

      2.2.2 Hierarchical Structures

      C.prompt("scheme-show-1-2-pair-3-4"); -

      as a list of three items, the first of which is itself a list, (1 2). Indeed, this is suggested by the form in which the result is printed by the interpreter. Figure 2-5 shows the representation of this structure in terms of pairs. +

      as a list of three items, the first of which is itself a list, (1 2). Indeed, this is suggested by the form in which the result is printed by the interpreter. Figure 2-5 shows the representation of this structure in terms of pairs.

      -

      Figure 2.5: Structure formed by (cons (list 1 2) (list 3 4)). +

      Figure 2.5: Structure formed by (cons (list 1 2) (list 3 4)). -

      Another way to think of sequences whose elements are sequences is as trees . The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees. Figure 2-6 shows the structure in Figure 2-5 viewed as a tree. +

      Another way to think of sequences whose elements are sequences is as trees . The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees. Figure 2-6 shows the structure in Figure 2-5 viewed as a tree.

      Figure 2.6: The list structure in Figure 2-5 viewed as a tree. -

      Recursion is a natural tool for dealing with tree structures, since we can often reduce operations on trees to operations on their branches, which reduce in turn to operations on the branches of the branches, and so on, until we reach the leaves of the tree. As an example, compare the length procedure of section 2.2.1 with the count-leaves procedure, which returns the total number of leaves of a tree: +

      Recursion is a natural tool for dealing with tree structures, since we can often reduce operations on trees to operations on their branches, which reduce in turn to operations on the branches of the branches, and so on, until we reach the leaves of the tree. As an example, compare the length procedure of section 2.2.1 with the count-leaves procedure, which returns the total number of leaves of a tree:

      (define x (cons (list 1 2) (list 3 4))) @@ -829,46 +827,46 @@

      2.2.2 Hierarchical Structures

      C.prompt("scheme-count-leaves-list-x-x", ["scheme-define-count-leaves", "scheme-define-x"]); -

      To implement count-leaves, recall the recursive plan for computing length: +

      To implement count-leaves, recall the recursive plan for computing length:

      • -length of a list x is 1 plus length of the cdr of x. +length of a list x is 1 plus length of the cdr of x.
      • -length of the empty list is 0. +length of the empty list is 0.
      -

      count-leaves is similar. The value for the empty list is the same: +

      count-leaves is similar. The value for the empty list is the same:

      • -count-leaves of the empty list is 0. +count-leaves of the empty list is 0.
      -

      But in the reduction step, where we strip off the car of the list, we must take into account that the car may itself be a tree whose leaves we need to count. Thus, the appropriate reduction step is +

      But in the reduction step, where we strip off the car of the list, we must take into account that the car may itself be a tree whose leaves we need to count. Thus, the appropriate reduction step is

      • -count-leaves of a tree x is count-leaves of the car of x plus count-leaves of the cdr of x. +count-leaves of a tree x is count-leaves of the car of x plus count-leaves of the cdr of x.
      -

      Finally, by taking cars we reach actual leaves, so we need another base case: +

      Finally, by taking cars we reach actual leaves, so we need another base case:

      • -count-leaves of a leaf is 1. +count-leaves of a leaf is 1.
      -

      To aid in writing recursive procedures on trees, Scheme provides the primitive predicate pair?, which tests whether its argument is a pair. Here is the complete procedure:13 +

      To aid in writing recursive procedures on trees, Scheme provides the primitive predicate pair?, which tests whether its argument is a pair. Here is the complete procedure:13

      (define (count-leaves x) @@ -883,14 +881,14 @@

      2.2.2 Hierarchical Structures

      -

      -

      Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2-6). +

      +

      Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in Figure 2-6).

      -

      -

      Exercise 2.25: Give combinations of cars and cdrs that will pick 7 from each of the following lists. Represent the list as l. +

      +

      Exercise 2.25: Give combinations of cars and cdrs that will pick 7 from each of the following lists. Represent the list as l.

      (1 3 (5 7) 9) @@ -917,8 +915,8 @@

      2.2.2 Hierarchical Structures

      -

      -

      Exercise 2.26: Suppose we define x and y to be two lists: +

      +

      Exercise 2.26: Suppose we define x and y to be two lists:

      (define x (list 1 2 3)) @@ -955,8 +953,8 @@

      2.2.2 Hierarchical Structures

      -

      -

      Exercise 2.27: Modify your reverse procedure of Exercise 2-18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example, +

      +

      Exercise 2.27: Modify your reverse procedure of Exercise 2-18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

      (define (append-to-end l x) @@ -1045,8 +1043,8 @@

      2.2.2 Hierarchical Structures

      -

      -

      Exercise 2.28: Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example, +

      +

      Exercise 2.28: Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example,

      @@ -1122,8 +1120,8 @@

      2.2.2 Hierarchical Structures

      -

      -

      Exercise 2.29: A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list): +

      +

      Exercise 2.29: A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

      (define (make-mobile left right) @@ -1133,7 +1131,7 @@

      2.2.2 Hierarchical Structures

      C.prompt("scheme-define-make-mobile"); -

      A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile: +

      A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

      (define (make-branch length structure) @@ -1145,7 +1143,7 @@

      2.2.2 Hierarchical Structures

      • -

        Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch. +

        Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.

        (define (left-branch mobile) @@ -1193,7 +1191,7 @@

        2.2.2 Hierarchical Structures

      • -

        Using your selectors, define a procedure total-weight that returns the total weight of a mobile. +

        Using your selectors, define a procedure total-weight that returns the total weight of a mobile.

        (define (total-weight mobile) @@ -1237,7 +1235,7 @@

        2.2.2 Hierarchical Structures

      • -

        A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced. +

        A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.

        (define (balanced? mobile) @@ -1288,7 +1286,7 @@

        2.2.2 Hierarchical Structures

      • Suppose we change the representation of mobiles so that the constructors are -

      • +
        (define (make-mobile left right) @@ -1302,13 +1300,13 @@

        2.2.2 Hierarchical Structures

        How much do you need to change your programs to convert to the new representation? - +

      Mapping over trees

      -

      Just as map is a powerful abstraction for dealing with sequences, map together with recursion is a powerful abstraction for dealing with trees. For instance, the scale-tree procedure, analogous to scale-list of section 2.2.1, takes as arguments a numeric factor and a tree whose leaves are numbers. It returns a tree of the same shape, where each number is multiplied by the factor. The recursive plan for scale-tree is similar to the one for count-leaves: +

      Just as map is a powerful abstraction for dealing with sequences, map together with recursion is a powerful abstraction for dealing with trees. For instance, the scale-tree procedure, analogous to scale-list of section 2.2.1, takes as arguments a numeric factor and a tree whose leaves are numbers. It returns a tree of the same shape, where each number is multiplied by the factor. The recursive plan for scale-tree is similar to the one for count-leaves:

      (define (scale-tree tree factor) @@ -1331,7 +1329,7 @@

      Mapping over trees

      C.prompt("scheme-test-scale-tree", ["scheme-define-scale-tree"]); -

      Another way to implement scale-tree is to regard the tree as a sequence of sub-trees and use map. We map over the sequence, scaling each sub-tree in turn, and return the list of results. In the base case, where the tree is a leaf, we simply multiply by the factor: +

      Another way to implement scale-tree is to regard the tree as a sequence of sub-trees and use map. We map over the sequence, scaling each sub-tree in turn, and return the list of results. In the base case, where the tree is a leaf, we simply multiply by the factor:

      (define (scale-tree tree factor) @@ -1347,8 +1345,8 @@

      Mapping over trees

      Many tree operations can be implemented by similar combinations of sequence operations and recursion. -

      -

      Exercise 2.30: Define a procedure square-tree analogous to the square-list procedure of Exercise 2-21. That is, square-list should behave as follows: +

      +

      Exercise 2.30: Define a procedure square-tree analogous to the square-list procedure of Exercise 2-21. That is, square-list should behave as follows:

      (square-tree @@ -1361,7 +1359,7 @@

      Mapping over trees

      C.prompt("scheme-test-square-tree"); -

      Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion. +

      Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.

      (define (square-tree tree) @@ -1403,8 +1401,8 @@

      Mapping over trees

      -

      -

      Exercise 2.31: Abstract your answer to Exercise 2-30 to produce a procedure tree-map with the property that square-tree could be defined as +

      +

      Exercise 2.31: Abstract your answer to Exercise 2-30 to produce a procedure tree-map with the property that square-tree could be defined as

      (define (square-tree tree) (tree-map square tree)) @@ -1454,8 +1452,8 @@

      Mapping over trees

      -

      -

      Exercise 2.32: We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works: +

      +

      Exercise 2.32: We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:

      (define (subsets s) @@ -1499,12 +1497,11 @@

      Mapping over trees

      - -

      2.2.3 Sequences as Conventional Interfaces

      +

      2.2.3 Sequences as Conventional Interfaces

      -

      In working with compound data, we've stressed how data abstraction permits us to design programs without becoming enmeshed in the details of data representations, and how abstraction preserves for us the flexibility to experiment with alternative representations. In this section, we introduce another powerful design principle for working with data structures – the use of conventional interfaces . +

      In working with compound data, we've stressed how data abstraction permits us to design programs without becoming enmeshed in the details of data representations, and how abstraction preserves for us the flexibility to experiment with alternative representations. In this section, we introduce another powerful design principle for working with data structures – the use of conventional interfaces . -

      In section 1.3 we saw how program abstractions, implemented as higher-order procedures, can capture common patterns in programs that deal with numerical data. Our ability to formulate analogous operations for working with compound data depends crucially on the style in which we manipulate our data structures. Consider, for example, the following procedure, analogous to the count-leaves procedure of section 2.2.2, which takes a tree as argument and computes the sum of the squares of the leaves that are odd: +

      In section 1.3 we saw how program abstractions, implemented as higher-order procedures, can capture common patterns in programs that deal with numerical data. Our ability to formulate analogous operations for working with compound data depends crucially on the style in which we manipulate our data structures. Consider, for example, the following procedure, analogous to the count-leaves procedure of section 2.2.2, which takes a tree as argument and computes the sum of the squares of the leaves that are odd:

      (define (sum-odd-squares tree) @@ -1551,7 +1548,7 @@

      2.2.3 Sequences as Conventional Interfaces

    • -accumulates the results using +, starting with 0. +accumulates the results using +, starting with 0.

    @@ -1571,22 +1568,22 @@

    2.2.3 Sequences as Conventional Interfaces

  • -accumulates the results using cons, starting with the +accumulates the results using cons, starting with the empty list.

  • -

    A signal-processing engineer would find it natural to conceptualize these processes in terms of signals flowing through a cascade of stages, each of which implements part of the program plan, as shown in Figure 2-7. In sum-odd-squares, we begin with an enumerator , which generates a "signal" consisting of the leaves of a given tree. This signal is passed through a filter , which eliminates all but the odd elements. The resulting signal is in turn passed through a map , which is a "transducer" that applies the square procedure to each element. The output of the map is then fed to an accumulator , which combines the elements using +, starting from an initial 0. The plan for even-fibs is analogous. +

    A signal-processing engineer would find it natural to conceptualize these processes in terms of signals flowing through a cascade of stages, each of which implements part of the program plan, as shown in Figure 2-7. In sum-odd-squares, we begin with an enumerator , which generates a "signal" consisting of the leaves of a given tree. This signal is passed through a filter , which eliminates all but the odd elements. The resulting signal is in turn passed through a map , which is a "transducer" that applies the square procedure to each element. The output of the map is then fed to an accumulator , which combines the elements using +, starting from an initial 0. The plan for even-fibs is analogous.

    -

    Figure 2.7: The signal-flow plans for the procedures sum-odd-squares (top) and even-fibs (bottom) reveal the commonality between the two programs. +

    Figure 2.7: The signal-flow plans for the procedures sum-odd-squares (top) and even-fibs (bottom) reveal the commonality between the two programs. -

    Unfortunately, the two procedure definitions above fail to exhibit this signal-flow structure. For instance, if we examine the sum-odd-squares procedure, we find that the enumeration is implemented partly by thenull? and pair? tests and partly by the tree-recursive structureof the procedure. Similarly, the accumulation is found partly in the tests and partly in the addition used in the recursion. In general, there are no distinct parts of either procedure that correspond to the elements in the signal-flow description. Our two procedures decompose the computations in a different way, spreading the enumeration over the program and mingling it with the map, the filter, and the accumulation. If we could organize our programs to make the signal-flow structure manifest in the procedures we write, this would increase the conceptual clarity of the resulting code. +

    Unfortunately, the two procedure definitions above fail to exhibit this signal-flow structure. For instance, if we examine the sum-odd-squares procedure, we find that the enumeration is implemented partly by thenull? and pair? tests and partly by the tree-recursive structureof the procedure. Similarly, the accumulation is found partly in the tests and partly in the addition used in the recursion. In general, there are no distinct parts of either procedure that correspond to the elements in the signal-flow description. Our two procedures decompose the computations in a different way, spreading the enumeration over the program and mingling it with the map, the filter, and the accumulation. If we could organize our programs to make the signal-flow structure manifest in the procedures we write, this would increase the conceptual clarity of the resulting code.

    Sequence Operations

    -

    The key to organizing programs so as to more clearly reflect the signal-flow structure is to concentrate on the "signals" that flow from one stage in the process to the next. If we represent these signals as lists, then we can use list operations to implement the processing at each of the stages. For instance, we can implement the mapping stages of the signal-flow diagrams using the map procedure from section 2.2.1: +

    The key to organizing programs so as to more clearly reflect the signal-flow structure is to concentrate on the "signals" that flow from one stage in the process to the next. If we represent these signals as lists, then we can use list operations to implement the processing at each of the stages. For instance, we can implement the mapping stages of the signal-flow diagrams using the map procedure from section 2.2.1:

    (map square (list 1 2 3 4 5)) @@ -1659,7 +1656,7 @@

    Sequence Operations

    C.prompt("scheme-accumulate-cons-1-to-5", ["scheme-define-accumulate"]); -

    All that remains to implement signal-flow diagrams is to enumerate the sequence of elements to be processed. For even-fibs, we need to generate the sequence of integers in a given range, which we can do as follows: +

    All that remains to implement signal-flow diagrams is to enumerate the sequence of elements to be processed. For even-fibs, we need to generate the sequence of integers in a given range, which we can do as follows:

    (define (enumerate-interval low high) @@ -1680,7 +1677,7 @@

    Sequence Operations

    C.prompt("scheme-enumerate-interval-2-7", ["scheme-define-enumerate-interval"]); -

    To enumerate the leaves of a tree, we can use14 +

    To enumerate the leaves of a tree, we can use14

    (define (enumerate-tree tree) @@ -1703,7 +1700,7 @@

    Sequence Operations

    C.prompt("scheme-enumerate-tree-nested-1-5", ["scheme-define-enumerate-tree"]); -

    Now we can reformulate sum-odd-squares and even-fibs as in the signal-flow diagrams. For sum-odd-squares, we enumerate the sequence of leaves of the tree, filter this to keep only the odd numbers in the sequence, square each element, and sum the results: +

    Now we can reformulate sum-odd-squares and even-fibs as in the signal-flow diagrams. For sum-odd-squares, we enumerate the sequence of leaves of the tree, filter this to keep only the odd numbers in the sequence, square each element, and sum the results:

    (define (sum-odd-squares tree) @@ -1730,7 +1727,7 @@

    Sequence Operations

    C.hidden_prompt("scheme-hidden-define-fib"); -

    For even-fibs, we enumerate the integers from 0 to n, generate the Fibonacci number for each of these integers, filter the resulting sequence to keep only the even elements, and accumulate the results into a list: +

    For even-fibs, we enumerate the integers from 0 to n, generate the Fibonacci number for each of these integers, filter the resulting sequence to keep only the even elements, and accumulate the results into a list:

    (define (even-fibs n) @@ -1746,7 +1743,7 @@

    Sequence Operations

    The value of expressing programs as sequence operations is that this helps us make program designs that are modular, that is, designs that are constructed by combining relatively independent pieces. We can encourage modular design by providing a library of standard components together with a conventional interface for connecting the components in flexible ways. -

    Modular construction is a powerful strategy for controlling complexity in engineering design. In real signal-processing applications, for example, designers regularly build systems by cascading elements selected from standardized families of filters and transducers. Similarly, sequence operations provide a library of standard program elements that we can mix and match. For instance, we can reuse pieces from the sum-odd-squares and even-fibs procedures in a program that constructs a list of the squares of the first $n + 1$ Fibonacci numbers: +

    Modular construction is a powerful strategy for controlling complexity in engineering design. In real signal-processing applications, for example, designers regularly build systems by cascading elements selected from standardized families of filters and transducers. Similarly, sequence operations provide a library of standard program elements that we can mix and match. For instance, we can reuse pieces from the sum-odd-squares and even-fibs procedures in a program that constructs a list of the squares of the first $n + 1$ Fibonacci numbers:

    (define (list-fib-squares n) @@ -1791,7 +1788,7 @@

    Sequence Operations

    C.prompt("scheme-product-of-squares-of-odd-elements-1-to-5", ["scheme-define-product-of-squares-of-odd-elements"]); -

    We can also formulate conventional data-processing applications in terms of sequence operations. Suppose we have a sequence of personnel records and we want to find the salary of the highest-paid programmer. Assume that we have a selector salary that returns the salary of a record, and a predicate programmer? that tests if a record is for a programmer. Then we can write +

    We can also formulate conventional data-processing applications in terms of sequence operations. Suppose we have a sequence of personnel records and we want to find the salary of the highest-paid programmer. Assume that we have a selector salary that returns the salary of a record, and a predicate programmer? that tests if a record is for a programmer. Then we can write

    (define (salary-of-highest-paid-programmer records) @@ -1804,11 +1801,11 @@

    Sequence Operations

    C.prompt("scheme-define-salary-of-highest-paid-programmer"); -

    These examples give just a hint of the vast range of operations that can be expressed as sequence operations.15 +

    These examples give just a hint of the vast range of operations that can be expressed as sequence operations.15

    Sequences, implemented here as lists, serve as a conventional interface that permits us to combine processing modules. Additionally, when we uniformly represent structures as sequences, we have localized the data-structure dependencies in our programs to a small number of sequence operations. By changing these, we can experiment with alternative representations of sequences, while leaving the overall design of our programs intact. We will exploit this capability in section 3.5, when we generalize the sequence-processing paradigm to admit infinite sequences. -

    +

    Exercise 2.33: Fill in the missing expressions to complete the following definitions of some basic list-manipulation operations as accumulations:

    @@ -1914,7 +1911,7 @@

    Sequence Operations

    -

    +

    Exercise 2.34: Evaluating a polynomial in x at a given value of x can be formulated as an accumulation. We evaluate the polynomial $$ @@ -1922,20 +1919,20 @@

    Sequence Operations

    $$ -

    using a well-known algorithm called Horner's rule , which structures the computation as +

    using a well-known algorithm called Horner's rule , which structures the computation as $$ (\cdots (a_n r + a_{n-1}) r + \cdots + a_1) r + a_0 $$ -

    In other words, we start with $a_n$, multiply by $x$, add $a_{n-1}$, multiply by $x$, and so on, until we reach $a_0$.16 +

    In other words, we start with $a_n$, multiply by $x$, add $a_{n-1}$, multiply by $x$, and so on, until we reach $a_0$.16

    Fill in the following template to produce a procedure that evaluates a polynomial using Horner's rule. Assume that the coefficients of the polynomial are arranged in a sequence, from $a_0$ through $a_n$.

    (define (horner-eval x coefficient-sequence) - (accumulate (lambda (this-coeff higher-terms) ) + (accumulate (lambda (this-coeff higher-terms) <??>) 0 coefficient-sequence))
    @@ -1982,8 +1979,8 @@

    Sequence Operations

    -

    -

    Exercise 2.35: Redefine count-leaves from +

    +

    Exercise 2.35: Redefine count-leaves from section 2.2.2 as an accumulation:

    @@ -2025,8 +2022,8 @@

    Sequence Operations

    -

    -

    Exercise 2.36: The procedure accumulate-n is similar to accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)), then the value of (accumulate-n + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of accumulate-n: +

    +

    Exercise 2.36: The procedure accumulate-n is similar to accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)), then the value of (accumulate-n + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of accumulate-n:

    (define (accumulate-n op init seqs) @@ -2083,24 +2080,24 @@

    Sequence Operations

    $$ -

    is represented as the sequence ((1 2 3 4) (4 5 6 6) (6 7 8 9)). With this representation, we can use sequence operations to concisely express the basic matrix and vector operations. These operations (which are described in any book on matrix algebra) are the following: +

    is represented as the sequence ((1 2 3 4) (4 5 6 6) (6 7 8 9)). With this representation, we can use sequence operations to concisely express the basic matrix and vector operations. These operations (which are described in any book on matrix algebra) are the following:

    • -(dot-product v w) returns the sum $\sum_i v_i w_i$ +(dot-product v w) returns the sum $\sum_i v_i w_i$
    • -(matrix-*-vector m w) returns the vector $t$, where $t_i = \sum_j m_{ij} v_j$ +(matrix-*-vector m w) returns the vector $t$, where $t_i = \sum_j m_{ij} v_j$
    • -(matrix-*-matrix m n) returns the matrix $p$, where $p_{ij} = \sum_k m_{ik} n_{kj}$ +(matrix-*-matrix m n) returns the matrix $p$, where $p_{ij} = \sum_k m_{ik} n_{kj}$
    • -(transpose m) returns the matrix $n$, where $n_{ij} = m_{ji}$ +(transpose m) returns the matrix $n$, where $n_{ij} = m_{ji}$
    -

    We can define the dot product as17 +

    We can define the dot product as17

    (define (dot-product v w) @@ -2111,7 +2108,7 @@

    Sequence Operations

    C.prompt("scheme-define-dot-product"); -

    Fill in the missing expressions in the following procedures for computing the other matrix operations. (The procedure accumulate-n is defined in Exercise 2-36.) +

    Fill in the missing expressions in the following procedures for computing the other matrix operations. (The procedure accumulate-n is defined in Exercise 2-36.)

    (define (matrix-*-vector m v) @@ -2129,8 +2126,8 @@

    Sequence Operations

    -
    -

    Exercise 2.38: The accumulate procedure is also known as fold-right, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction: +

    +

    Exercise 2.38: The accumulate procedure is also known as fold-right, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction:

    (define (fold-left op initial sequence) @@ -2175,20 +2172,20 @@

    Sequence Operations

    C.answer("scheme-ex-foldl-list", "(((() 1) 2) 3)"); -

    Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence. +

    Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence.

    -

    -

    Exercise 2.39: Complete the following definitions of reverse (Exercise 2-18) in terms of fold-right and fold-left from Exercise 2-38: +

    +

    Exercise 2.39: Complete the following definitions of reverse (Exercise 2-18) in terms of fold-right and fold-left from Exercise 2-38:

    (define (reverse sequence) - (fold-right (lambda (x y) ) nil sequence)) + (fold-right (lambda (x y) <??>) nil sequence)) (define (reverse sequence) - (fold-left (lambda (x y) ) nil sequence)) + (fold-left (lambda (x y) <??>) nil sequence))
    -

    The combination of mapping and accumulating with append is so common in this sort of program that we will isolate it as a separate procedure: +

    The combination of mapping and accumulating with append is so common in this sort of program that we will isolate it as a separate procedure:

    (define (flatmap proc seq) @@ -2290,7 +2287,7 @@

    Nested Mappings

    C.prompt("scheme-define-prime-sum-pairs", ["scheme-define-map", "scheme-define-filter", "scheme-define-flatmap", "scheme-define-enumerate-interval"]); -

    Nested mappings are also useful for sequences other than those that enumerate intervals. Suppose we wish to generate all the permutations of a set $S$; that is, all the ways of ordering the items in the set. For instance, the permutations of $\{1,2,3\}$ are $\{1,2,3\}$, $\{1,3,2\}$, $\{2,1,3\}$, $\{2,3,1\}$, $\{3,1,2\}$, and $\{3,2,1\}$. Here is a plan for generating the permutations of $S$: For each item $x$ in $S$, recursively generate the sequence of permutations of $S - x$,20 and adjoin $x$ to the front of each one. This yields, for each x in S, the sequence of permutations of S that begin with $x$. Combining these sequences for all $x$ gives all the permutations of $S$:21 +

    Nested mappings are also useful for sequences other than those that enumerate intervals. Suppose we wish to generate all the permutations of a set $S$; that is, all the ways of ordering the items in the set. For instance, the permutations of $\{1,2,3\}$ are $\{1,2,3\}$, $\{1,3,2\}$, $\{2,1,3\}$, $\{2,3,1\}$, $\{3,1,2\}$, and $\{3,2,1\}$. Here is a plan for generating the permutations of $S$: For each item $x$ in $S$, recursively generate the sequence of permutations of $S - x$,20 and adjoin $x$ to the front of each one. This yields, for each x in S, the sequence of permutations of S that begin with $x$. Combining these sequences for all $x$ gives all the permutations of $S$:21

    (define (permutations s) @@ -2305,7 +2302,7 @@

    Nested Mappings

    C.prompt("scheme-define-permutations", ["scheme-define-map", "scheme-define-flatmap", "scheme-define-remove"]); -

    Notice how this strategy reduces the problem of generating permutations of S to the problem of generating the permutations of sets with fewer elements than S. In the terminal case, we work our way down to the empty list, which represents a set of no elements. For this, we generate (list nil), which is a sequence with one item, namely the set with no elements. The remove procedure used in permutations returns all the items in a given sequence except for a given item. This can be expressed as a simple filter: +

    Notice how this strategy reduces the problem of generating permutations of S to the problem of generating the permutations of sets with fewer elements than S. In the terminal case, we work our way down to the empty list, which represents a set of no elements. For this, we generate (list nil), which is a sequence with one item, namely the set with no elements. The remove procedure used in permutations returns all the items in a given sequence except for a given item. This can be expressed as a simple filter:

    (define (remove item sequence) @@ -2318,20 +2315,19 @@

    Nested Mappings

    -

    -

    Exercise 2.40: Define a procedure unique-pairs that, given an integer $n$, generates the sequence of pairs $(i,j)$ with $1 ≤ j< i ≤ n$. Use unique-pairs to simplify the definition of prime-sum-pairs given above. +

    +

    Exercise 2.40: Define a procedure unique-pairs that, given an integer $n$, generates the sequence of pairs $(i,j)$ with $1 ≤ j< i ≤ n$. Use unique-pairs to simplify the definition of prime-sum-pairs given above.

    -

    +

    Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers $i$, $j$, and $k$ less than or equal to a given integer $n$ that sum to a given integer $s$.

    -

    - +

    Exercise 2.42: The "eight-queens puzzle" asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal).

    @@ -2340,7 +2336,7 @@

    Nested Mappings

    One possible solution is shown in Figure 2-8. One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed $k - 1$ queens, we must place the $k^{th}$ queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to place $k - 1$ queens in the first $k - 1$ columns of the board. For each of these ways, generate an extended set of positions by placing a queen in each row of the kth column. Now filter these, keeping only the positions for which the queen in the $k^{th}$ column is safe with respect to the other queens. This produces the sequence of all ways to place $k$ queens in the first $k$ columns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle. -

    We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on an $n \times n$ chessboard. queens has an internal procedure queen-cols that returns the sequence of all ways to place queens in the first k columns of the board. +

    We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on an $n \times n$ chessboard. queens has an internal procedure queen-cols that returns the sequence of all ways to place queens in the first k columns of the board.

    (define (queens board-size) @@ -2361,13 +2357,13 @@

    Nested Mappings

    C.prompt("scheme-define-queens", ["scheme-define-filter", "scheme-define-map", "scheme-define-flatmap"]); -

    In this procedure rest-of-queens is a way to place $k - 1$ queens in the first $k - 1$ columns, and new-row is a proposed row in which to place the queen for the $k^{th}$ column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoin-position, which adjoins a new row-column position to a set of positions, and empty-board, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in the kth column is safe with respect to the others. (Note that we need only check whether the new queen is safe---the other queens are already guaranteed safe with respect to each other.) +

    In this procedure rest-of-queens is a way to place $k - 1$ queens in the first $k - 1$ columns, and new-row is a proposed row in which to place the queen for the $k^{th}$ column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoin-position, which adjoins a new row-column position to a set of positions, and empty-board, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in the kth column is safe with respect to the others. (Note that we need only check whether the new queen is safe---the other queens are already guaranteed safe with respect to each other.)

    -

    -

    Exercise 2.43: Louis Reasoner is having a terrible time doing Exercise 2-42. His queens procedure seems to work, but it runs extremely slowly. (Louis never does manage to wait long enough for it to solve even the $6 \times 6$ case.) When Louis asks Eva Lu Ator for help, she points out that he has interchanged the order of the nested mappings in the flatmap, writing it as +

    +

    Exercise 2.43: Louis Reasoner is having a terrible time doing Exercise 2-42. His queens procedure seems to work, but it runs extremely slowly. (Louis never does manage to wait long enough for it to solve even the $6 \times 6$ case.) When Louis asks Eva Lu Ator for help, she points out that he has interchanged the order of the nested mappings in the flatmap, writing it as

    (flatmap @@ -2385,12 +2381,11 @@

    Nested Mappings

    - -

    2.2.4 Example: A Picture Language

    +

    2.2.4 Example: A Picture Language

    TODO: eventually I hope to implement this picture language in javascript. However it is not done yet. -

    This section presents a simple language for drawing pictures that illustrates the power of data abstraction and closure, and also exploits higher-order procedures in an essential way. The language is designed to make it easy to experiment with patterns such as the ones in Figure 2-9, which are composed of repeated elements that are shifted and scaled.22 In this language, the data objects being combined are represented as procedures rather than as list structure. Just as cons, which satisfies the closure property, allowed us to easily build arbitrarily complicated list structure, the operations in this language, which also satisfy the closure property, allow us to easily build arbitrarily complicated patterns. +

    This section presents a simple language for drawing pictures that illustrates the power of data abstraction and closure, and also exploits higher-order procedures in an essential way. The language is designed to make it easy to experiment with patterns such as the ones in Figure 2-9, which are composed of repeated elements that are shifted and scaled.22 In this language, the data objects being combined are represented as procedures rather than as list structure. Just as cons, which satisfies the closure property, allowed us to easily build arbitrarily complicated list structure, the operations in this language, which also satisfy the closure property, allow us to easily build arbitrarily complicated patterns.

    @@ -2405,9 +2400,9 @@

    The picture language

    When we began our study of programming in section 1.1, we emphasized the importance of describing a language by focusing on the language's primitives, its means of combination, and its means of abstraction. We'll follow that framework here. -

    Part of the elegance of this picture language is that there is only one kind of element, called a painter . A painter draws an image that is shifted and scaled to fit within a designated parallelogram-shaped frame. For example, there's a primitive painter we'll call wave that makes a crude line drawing, as shown in Figure 2-10. The actual shape of the drawing depends on the frame---all four images in Figure 2-10 are produced by the same wave painter, but with respect to four different frames. Painters can be more elaborate than this: The primitive painter called rogers paints a picture of MIT's founder, William Barton Rogers, as shown in Figure 2-11.23 The four images in Figure 2-11 are drawn with respect to the same four frames as the wave images in Figure 2-10. +

    Part of the elegance of this picture language is that there is only one kind of element, called a painter . A painter draws an image that is shifted and scaled to fit within a designated parallelogram-shaped frame. For example, there's a primitive painter we'll call wave that makes a crude line drawing, as shown in Figure 2-10. The actual shape of the drawing depends on the frame---all four images in Figure 2-10 are produced by the same wave painter, but with respect to four different frames. Painters can be more elaborate than this: The primitive painter called rogers paints a picture of MIT's founder, William Barton Rogers, as shown in Figure 2-11.23 The four images in Figure 2-11 are drawn with respect to the same four frames as the wave images in Figure 2-10. -

    To combine images, we use various operations that construct new painters from given painters. For example, the beside operation takes two painters and produces a new, compound painter that draws the first painter's image in the left half of the frame and the second painter's image in the right half of the frame. Similarly, below takes two painters and produces a compound painter that draws the first painter's image below the second painter's image. Some operations transform a single painter to produce a new painter. For example, flip-vert takes a painter and produces a painter that draws its image upside-down, and flip-horiz produces a painter that draws the original painter's image left-to-right reversed. +

    To combine images, we use various operations that construct new painters from given painters. For example, the beside operation takes two painters and produces a new, compound painter that draws the first painter's image in the left half of the frame and the second painter's image in the right half of the frame. Similarly, below takes two painters and produces a compound painter that draws the first painter's image below the second painter's image. Some operations transform a single painter to produce a new painter. For example, flip-vert takes a painter and produces a painter that draws its image upside-down, and flip-horiz produces a painter that draws the original painter's image left-to-right reversed.

    @@ -2415,7 +2410,7 @@

    The picture language

    -

    Figure 2.10: Images produced by the wave painter, with respect to four different frames. The frames, shown with dotted lines, are not part of the images. +

    Figure 2.10: Images produced by the wave painter, with respect to four different frames. The frames, shown with dotted lines, are not part of the images.

    @@ -2429,7 +2424,7 @@

    The picture language

    Figure 2.11: Images of William Barton Rogers, founder and first president of MIT, painted with respect to the same four frames as in Figure 2-10 (original image reprinted with the permission of the MIT Museum).

    -

    Figure 2-12 shows the drawing of a painter called wave4 that is built up in two stages starting from wave: +

    Figure 2-12 shows the drawing of a painter called wave4 that is built up in two stages starting from wave:

    (define wave2 (beside wave (flip-vert wave))) @@ -2443,12 +2438,12 @@

    The picture language

    -

    Figure 2.12: Creating a complex figure, starting from the wave painter of Figure 2-10. +

    Figure 2.12: Creating a complex figure, starting from the wave painter of Figure 2-10.

    -

    In building up a complex image in this manner we are exploiting the fact that painters are closed under the language's means of combination. The beside or below of two painters is itself a painter; therefore, we can use it as an element in making more complex painters. As with building up list structure using cons, the closure of our data under the means of combination is crucial to the ability to create complex structures while using only a few operations. +

    In building up a complex image in this manner we are exploiting the fact that painters are closed under the language's means of combination. The beside or below of two painters is itself a painter; therefore, we can use it as an element in making more complex painters. As with building up list structure using cons, the closure of our data under the means of combination is crucial to the ability to create complex structures while using only a few operations. -

    Once we can combine painters, we would like to be able to abstract typical patterns of combining painters. We will implement the painter operations as Scheme procedures. This means that we don't need a special abstraction mechanism in the picture language: Since the means of combination are ordinary Scheme procedures, we automatically have the capability to do anything with painter operations that we can do with procedures. For example, we can abstract the pattern in wave4 as +

    Once we can combine painters, we would like to be able to abstract typical patterns of combining painters. We will implement the painter operations as Scheme procedures. This means that we don't need a special abstraction mechanism in the picture language: Since the means of combination are ordinary Scheme procedures, we automatically have the capability to do anything with painter operations that we can do with procedures. For example, we can abstract the pattern in wave4 as

    (define (flipped-pairs painter) @@ -2459,7 +2454,7 @@

    The picture language

    C.prompt("scheme-define-flipped-pairs"); -

    and define wave4 as an instance of this pattern: +

    and define wave4 as an instance of this pattern:

    (define wave4 (flipped-pairs wave)) @@ -2487,7 +2482,7 @@

    The picture language

    -

    Figure 2.13: Recursive plans for right-split and corner-split. +

    Figure 2.13: Recursive plans for right-split and corner-split.

    We can produce balanced patterns by branching upwards as well as towards the right (see Exercise 2-44 and figures Figure 2-13 and Figure 2-14): @@ -2529,9 +2524,9 @@

    The picture language

    -

    Figure 2.14: The recursive operations right-split and corner-split applied to the painters wave and rogers. Combining four corner-split figures produces symmetric square-limit designs as shown in Figure 2-9. +

    Figure 2.14: The recursive operations right-split and corner-split applied to the painters wave and rogers. Combining four corner-split figures produces symmetric square-limit designs as shown in Figure 2-9. -

    By placing four copies of a corner-split appropriately, we obtain a pattern called square-limit, whose application to wave and rogers is shown in Figure 2-9: +

    By placing four copies of a corner-split appropriately, we obtain a pattern called square-limit, whose application to wave and rogers is shown in Figure 2-9:

    (define (square-limit painter n) @@ -2545,15 +2540,15 @@

    The picture language

    -

    -

    Exercise 2.44: Define the procedure up-split used by corner-split. It is similar to right-split, except that it switches the roles of below and beside. +

    +

    Exercise 2.44: Define the procedure up-split used by corner-split. It is similar to right-split, except that it switches the roles of below and beside.

    Higher-order operations

    In addition to abstracting patterns of combining painters, we can work at a higher level, abstracting patterns of combining painter operations. That is, we can view the painter operations as elements to manipulate and can write means of combination for these elements---procedures that take painter operations as arguments and create new painter operations. -

    For example, flipped-pairs and square-limit each arrange four copies of a painter's image in a square pattern; they differ only in how they orient the copies. One way to abstract this pattern of painter combination is with the following procedure, which takes four one-argument painter operations and produces a painter operation that transforms a given painter with those four operations and arranges the results in a square. Tl, tr, bl, and br are the transformations to apply to the top left copy, the top right copy, the bottom left copy, and the bottom right copy, respectively. +

    For example, flipped-pairs and square-limit each arrange four copies of a painter's image in a square pattern; they differ only in how they orient the copies. One way to abstract this pattern of painter combination is with the following procedure, which takes four one-argument painter operations and produces a painter operation that transforms a given painter with those four operations and arranges the results in a square. Tl, tr, bl, and br are the transformations to apply to the top left copy, the top right copy, the bottom left copy, and the bottom right copy, respectively.

    (define (square-of-four tl tr bl br) @@ -2566,7 +2561,7 @@

    Higher-order operations

    C.prompt("scheme-define-square-of-four"); -

    Then flipped-pairs can be defined in terms of square-of-four as follows:24 +

    Then flipped-pairs can be defined in terms of square-of-four as follows:24

    (define (flipped-pairs painter) @@ -2579,7 +2574,7 @@

    Higher-order operations

    -

    and square-limit can be expressed as25 +

    and square-limit can be expressed as25

    (define (square-limit painter n) @@ -2591,8 +2586,8 @@

    Higher-order operations

    C.prompt("scheme-define-square-limit-with-square-of-four"); -
    -

    Exercise 2.45: right-split and up-split can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating +

    +

    Exercise 2.45: right-split and up-split can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating

    (define right-split (split beside below)) @@ -2602,28 +2597,28 @@

    Higher-order operations

    C.prompt("scheme-define-right-up-split-with-split"); -

    produces procedures right-split and up-split with the same behaviors as the ones already defined. +

    produces procedures right-split and up-split with the same behaviors as the ones already defined.

    Frames

    Before we can show how to implement painters and their means of combination, we must first consider frames. A frame can be described by three vectors---an origin vector and two edge vectors. The origin vector specifies the offset of the frame's origin from some absolute origin in the plane, and the edge vectors specify the offsets of the frame's corners from its origin. If the edges are perpendicular, the frame will be rectangular. Otherwise the frame will be a more general parallelogram. -

    Figure 2-15 shows a frame and its associated vectors. In accordance with data abstraction, we need not be specific yet about how frames are represented, other than to say that there is a constructor make-frame, which takes three vectors and produces a frame, and three corresponding selectors origin-frame, edge1-frame, and edge2-frame (see Exercise 2-47). +

    Figure 2-15 shows a frame and its associated vectors. In accordance with data abstraction, we need not be specific yet about how frames are represented, other than to say that there is a constructor make-frame, which takes three vectors and produces a frame, and three corresponding selectors origin-frame, edge1-frame, and edge2-frame (see Exercise 2-47).

    Figure 2.15: A frame is described by three vectors -- an origin and two edges.

    -

    We will use coordinates in the unit square $(0 ≤ x,y ≤ 1)$ to specify images. With each frame, we associate a frame coordinate map , which will be used to shift and scale images to fit the frame. The map transforms the unit square into the frame by mapping the vector v = (x,y) to the vector sum +

    We will use coordinates in the unit square $(0 ≤ x,y ≤ 1)$ to specify images. With each frame, we associate a frame coordinate map , which will be used to shift and scale images to fit the frame. The map transforms the unit square into the frame by mapping the vector v = (x,y) to the vector sum $$ \text{Origin}(\text{Frame}) + x \cdot \text{Edge}_1(\text{Frame}) + y \cdot \text{Edge}_2(\text{Frame}) $$ -

    For example, (0,0) is mapped to the origin of the frame, (1,1) to the vertex diagonally opposite the origin, and (0.5,0.5) to the center of the frame. We can create a frame's coordinate map with the following procedure:26 +

    For example, (0,0) is mapped to the origin of the frame, (1,1) to the vertex diagonally opposite the origin, and (0.5,0.5) to the center of the frame. We can create a frame's coordinate map with the following procedure:26

    (define (frame-coord-map frame) @@ -2639,7 +2634,7 @@

    Frames

    C.prompt("scheme-define-frame-coord-map"); -

    Observe that applying frame-coord-map to a frame returns a procedure that, given a vector, returns a vector. If the argument vector is in the unit square, the result vector will be in the frame. For example, +

    Observe that applying frame-coord-map to a frame returns a procedure that, given a vector, returns a vector. If the argument vector is in the unit square, the result vector will be in the frame. For example,

    ((frame-coord-map a-frame) (make-vect 0 0)) @@ -2657,8 +2652,8 @@

    Frames

    C.prompt("scheme-origin-frame-a-frame"); -
    -

    Exercise 2.46: A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x-coordinate and a y-coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar: +

    +

    Exercise 2.46: A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x-coordinate and a y-coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar: \begin{align} (x_1, y_1) + (x_2, y_2) &= (x_1 + x_2, y_1 + y_2) \\ @@ -2669,7 +2664,7 @@

    Frames

    -

    +

    Exercise 2.47: Here are two possible constructors for frames:

    @@ -2688,9 +2683,9 @@

    Frames

    Painters

    -

    A painter is represented as a procedure that, given a frame as argument, draws a particular image shifted and scaled to fit the frame. That is to say, if p is a painter and f is a frame, then we produce p's image in f by calling p with f as argument. +

    A painter is represented as a procedure that, given a frame as argument, draws a particular image shifted and scaled to fit the frame. That is to say, if p is a painter and f is a frame, then we produce p's image in f by calling p with f as argument. -

    The details of how primitive painters are implemented depend on the particular characteristics of the graphics system and the type of image to be drawn. For instance, suppose we have a procedure draw-line that draws a line on the screen between two specified points. Then we can create painters for line drawings, such as the wave painter in Figure 2-10, from lists of line segments as follows:27 +

    The details of how primitive painters are implemented depend on the particular characteristics of the graphics system and the type of image to be drawn. For instance, suppose we have a procedure draw-line that draws a line on the screen between two specified points. Then we can create painters for line drawings, such as the wave painter in Figure 2-10, from lists of line segments as follows:27

    (define (segments->painter segment-list) @@ -2708,16 +2703,16 @@

    Painters

    The segments are given using coordinates with respect to the unit square. For each segment in the list, the painter transforms the segment endpoints with the frame coordinate map and draws a line between the transformed points. -

    Representing painters as procedures erects a powerful abstraction barrier in the picture language. We can create and intermix all sorts of primitive painters, based on a variety of graphics capabilities. The details of their implementation do not matter. Any procedure can serve as a painter, provided that it takes a frame as argument and draws something scaled to fit the frame.28 +

    Representing painters as procedures erects a powerful abstraction barrier in the picture language. We can create and intermix all sorts of primitive painters, based on a variety of graphics capabilities. The details of their implementation do not matter. Any procedure can serve as a painter, provided that it takes a frame as argument and draws something scaled to fit the frame.28 -

    -

    Exercise 2.48: A directed line segment in the plane can be represented as a pair of vectors---the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from Exercise 2-46 to define a representation for segments with a constructor make-segment and selectors start-segment and end-segment. +

    +

    Exercise 2.48: A directed line segment in the plane can be represented as a pair of vectors---the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from Exercise 2-46 to define a representation for segments with a constructor make-segment and selectors start-segment and end-segment.

    -

    -

    Exercise 2.49: Use segments->painter to define the following primitive painters: +

    +

    Exercise 2.49: Use segments->painter to define the following primitive painters:

    • @@ -2734,16 +2729,16 @@

      Painters

    • -The wave painter. +The wave painter.

    Transforming and combining painters

    -

    An operation on painters (such as flip-vert or beside) works by creating a painter that invokes the original painters with respect to frames derived from the argument frame. Thus, for example, flip-vert doesn't have to know how a painter works in order to flip it---it just has to know how to turn a frame upside down: The flipped painter just uses the original painter, but in the inverted frame. +

    An operation on painters (such as flip-vert or beside) works by creating a painter that invokes the original painters with respect to frames derived from the argument frame. Thus, for example, flip-vert doesn't have to know how a painter works in order to flip it---it just has to know how to turn a frame upside down: The flipped painter just uses the original painter, but in the inverted frame. -

    Painter operations are based on the procedure transform-painter, which takes as arguments a painter and information on how to transform a frame and produces a new painter. The transformed painter, when called on a frame, transforms the frame and calls the original painter on the transformed frame. The arguments to transform-painter are points (represented as vectors) that specify the corners of the new frame: When mapped into the frame, the first point specifies the new frame's origin and the other two specify the ends of its edge vectors. Thus, arguments within the unit square specify a frame contained within the original frame. +

    Painter operations are based on the procedure transform-painter, which takes as arguments a painter and information on how to transform a frame and produces a new painter. The transformed painter, when called on a frame, transforms the frame and calls the original painter on the transformed frame. The arguments to transform-painter are points (represented as vectors) that specify the corners of the new frame: When mapped into the frame, the first point specifies the new frame's origin and the other two specify the ends of its edge vectors. Thus, arguments within the unit square specify a frame contained within the original frame.

    (define (transform-painter painter origin corner1 corner2) @@ -2764,15 +2759,15 @@

    Transforming and combining painters

    (define (flip-vert painter) (transform-painter painter - (make-vect 0.0 1.0) ; new origin - (make-vect 1.0 1.0) ; new end of edge1 - (make-vect 0.0 0.0))) ; new end of edge2 + (make-vect 0.0 1.0) ; new origin + (make-vect 1.0 1.0) ; new end of edge1 + (make-vect 0.0 0.0))) ; new end of edge2
    -

    Using transform-painter, we can easily define new transformations. For example, we can define a painter that shrinks its image to the upper-right quarter of the frame it is given: +

    Using transform-painter, we can easily define new transformations. For example, we can define a painter that shrinks its image to the upper-right quarter of the frame it is given:

    (define (shrink-to-upper-right painter) @@ -2785,7 +2780,7 @@

    Transforming and combining painters

    C.prompt("scheme-define-shrink-to-upper-right"); -

    Other transformations rotate images counterclockwise by 90 degrees29 +

    Other transformations rotate images counterclockwise by 90 degrees29

    (define (rotate90 painter) @@ -2798,7 +2793,7 @@

    Transforming and combining painters

    C.prompt("scheme-define-rotate90"); -

    or squash images towards the center of the frame:30 +

    or squash images towards the center of the frame:30

    (define (squash-inwards painter) @@ -2811,7 +2806,7 @@

    Transforming and combining painters

    C.prompt("scheme-define-squash-inwards"); -

    Frame transformation is also the key to defining means of combining two or more painters. The beside procedure, for example, takes two painters, transforms them to paint in the left and right halves of an argument frame respectively, and produces a new, compound painter. When the compound painter is given a frame, it calls the first transformed painter to paint in the left half of the frame and calls the second transformed painter to paint in the right half of the frame: +

    Frame transformation is also the key to defining means of combining two or more painters. The beside procedure, for example, takes two painters, transforms them to paint in the left and right halves of an argument frame respectively, and produces a new, compound painter. When the compound painter is given a frame, it calls the first transformed painter to paint in the left half of the frame and calls the second transformed painter to paint in the right half of the frame:

    (define (beside painter1 painter2) @@ -2834,44 +2829,44 @@

    Transforming and combining painters

    C.prompt("scheme-define-beside"); -

    Observe how the painter data abstraction, and in particular the representation of painters as procedures, makes beside easy to implement. The beside procedure need not know anything about the details of the component painters other than that each painter will draw something in its designated frame. +

    Observe how the painter data abstraction, and in particular the representation of painters as procedures, makes beside easy to implement. The beside procedure need not know anything about the details of the component painters other than that each painter will draw something in its designated frame. -

    -

    Exercise 2.50: Define the transformation flip-horiz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees. +

    +

    Exercise 2.50: Define the transformation flip-horiz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees.

    -

    -

    Exercise 2.51: Define the below operation for painters. Below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame and with the second painter in the top. Define below in two different ways---first by writing a procedure that is analogous to the beside procedure given above, and again in terms of beside and suitable rotation operations (from Exercise 2-50). +

    +

    Exercise 2.51: Define the below operation for painters. Below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame and with the second painter in the top. Define below in two different ways---first by writing a procedure that is analogous to the beside procedure given above, and again in terms of beside and suitable rotation operations (from Exercise 2-50).

    Levels of language for robust design

    The picture language exercises some of the critical ideas we've introduced about abstraction with procedures and data. The fundamental data abstractions, painters, are implemented using procedural representations, which enables the language to handle different basic drawing capabilities in a uniform way. The means of combination satisfy the closure property, which permits us to easily build up complex designs. Finally, all the tools for abstracting procedures are available to us for abstracting means of combination for painters. -

    We have also obtained a glimpse of another crucial idea about languages and program design. This is the approach of stratified design , the notion that a complex system should be structured as a sequence of levels that are described using a sequence of languages. Each level is constructed by combining parts that are regarded as primitive at that level, and the parts constructed at each level are used as primitives at the next level. The language used at each level of a stratified design has primitives, means of combination, and means of abstraction appropriate to that level of detail. +

    We have also obtained a glimpse of another crucial idea about languages and program design. This is the approach of stratified design , the notion that a complex system should be structured as a sequence of levels that are described using a sequence of languages. Each level is constructed by combining parts that are regarded as primitive at that level, and the parts constructed at each level are used as primitives at the next level. The language used at each level of a stratified design has primitives, means of combination, and means of abstraction appropriate to that level of detail. -

    Stratified design pervades the engineering of complex systems. For example, in computer engineering, resistors and transistors are combined (and described using a language of analog circuits) to produce parts such as and-gates and or-gates, which form the primitives of a language for digital-circuit design.31 These parts are combined to build processors, bus structures, and memory systems, which are in turn combined to form computers, using languages appropriate to computer architecture. Computers are combined to form distributed systems, using languages appropriate for describing network interconnections, and so on. +

    Stratified design pervades the engineering of complex systems. For example, in computer engineering, resistors and transistors are combined (and described using a language of analog circuits) to produce parts such as and-gates and or-gates, which form the primitives of a language for digital-circuit design.31 These parts are combined to build processors, bus structures, and memory systems, which are in turn combined to form computers, using languages appropriate to computer architecture. Computers are combined to form distributed systems, using languages appropriate for describing network interconnections, and so on. -

    As a tiny example of stratification, our picture language uses primitive elements (primitive painters) that are created using a language that specifies points and lines to provide the lists of line segments for segments->painter, or the shading details for a painter like rogers. The bulk of our description of the picture language focused on combining these primitives, using geometric combiners such as beside and below. We also worked at a higher level, regarding beside and below as primitives to be manipulated in a language whose operations, such as square-of-four, capture common patterns of combining geometric combiners. +

    As a tiny example of stratification, our picture language uses primitive elements (primitive painters) that are created using a language that specifies points and lines to provide the lists of line segments for segments->painter, or the shading details for a painter like rogers. The bulk of our description of the picture language focused on combining these primitives, using geometric combiners such as beside and below. We also worked at a higher level, regarding beside and below as primitives to be manipulated in a language whose operations, such as square-of-four, capture common patterns of combining geometric combiners. -

    Stratified design helps make programs robust , that is, it makes it likely that small changes in a specification will require correspondingly small changes in the program. For instance, suppose we wanted to change the image based on wave shown in Figure 2-9. We could work at the lowest level to change the detailed appearance of the wave element; we could work at the middle level to change the way corner-split replicates the wave; we could work at the highest level to change how square-limit arranges the four copies of the corner. In general, each level of a stratified design provides a different vocabulary for expressing the characteristics of the system, and a different kind of ability to change it. +

    Stratified design helps make programs robust , that is, it makes it likely that small changes in a specification will require correspondingly small changes in the program. For instance, suppose we wanted to change the image based on wave shown in Figure 2-9. We could work at the lowest level to change the detailed appearance of the wave element; we could work at the middle level to change the way corner-split replicates the wave; we could work at the highest level to change how square-limit arranges the four copies of the corner. In general, each level of a stratified design provides a different vocabulary for expressing the characteristics of the system, and a different kind of ability to change it. -

    -

    Exercise 2.52: Make changes to the square limit of wave shown in Figure 2-9 by working at each of the levels described above. In particular: +

    +

    Exercise 2.52: Make changes to the square limit of wave shown in Figure 2-9 by working at each of the levels described above. In particular:

    • -Add some segments to the primitive wave painter of Exercise 2-49 (to add a smile, for example). +Add some segments to the primitive wave painter of Exercise 2-49 (to add a smile, for example).
    • -Change the pattern constructed by corner-split (for example, by using only one copy of the up-split and right-split images instead of two). +Change the pattern constructed by corner-split (for example, by using only one copy of the up-split and right-split images instead of two).
    • -Modify the version of square-limit that uses square-of-four so as to assemble the corners in a different pattern. (For example, you might make the big Mr. Rogers look outward from each corner of the square.) +Modify the version of square-limit that uses square-of-four so as to assemble the corners in a different pattern. (For example, you might make the big Mr. Rogers look outward from each corner of the square.)
    @@ -2906,7 +2901,7 @@

    Levels of language for robust design

    -

    11 To define f and g using lambda we would write +

    11 To define f and g using lambda we would write

    (define f (lambda (x y . z) <body>)) @@ -2918,7 +2913,7 @@

    Levels of language for robust design

    -

    12 Scheme standardly provides a map procedure that is more general than the one described here. This more general map takes a procedure of n arguments, together with n lists, and applies the procedure to all the first elements of the lists, all the second elements of the lists, and so on, returning a list of the results. For example: +

    12 Scheme standardly provides a map procedure that is more general than the one described here. This more general map takes a procedure of n arguments, together with n lists, and applies the procedure to all the first elements of the lists, all the second elements of the lists, and so on, returning a list of the results. For example:

    (map + (list 1 2 3) (list 40 50 60) (list 700 800 900)) @@ -2935,7 +2930,7 @@

    Levels of language for robust design

    -

    13 The order of the first two clauses in the cond matters, since the empty list satisfies null? and also is not a pair.

    +

    13 The order of the first two clauses in the cond matters, since the empty list satisfies null? and also is not a pair.

    14 This is, in fact, precisely the fringe procedure from exercise 2.28. Here we've renamed it to emphasize that it is part of a family of general sequence-manipulation procedures. @@ -2954,7 +2949,7 @@

    Levels of language for robust design

    -

    18 This approach to nested mappings was shown to us by David Turner, whose languages KRC and Miranda provide elegant formalisms for dealing with these constructs. The examples in this section (see also exercise 2.42) are adapted from Turner 1981. In section 3.5.3, we'll see how this approach generalizes to infinite sequences. +

    18 This approach to nested mappings was shown to us by David Turner, whose languages KRC and Miranda provide elegant formalisms for dealing with these constructs. The examples in this section (see also exercise 2.42) are adapted from Turner 1981. In section 3.5.3, we'll see how this approach generalizes to infinite sequences.

    @@ -3018,7 +3013,7 @@

    Levels of language for robust design

    -

    28 For example, the rogers painter of figure 2.11 was constructed from a gray-level image. For each point in a given frame, the rogers painter determines the point in the image that is mapped to it under the frame coordinate map, and shades it accordingly. By allowing different types of painters, we are capitalizing on the abstract data idea discussed in section 2.1.3, where we argued that a rational-number representation could be anything at all that satisfies an appropriate condition. Here we're using the fact that a painter can be implemented in any way at all, so long as it draws something in the designated frame. Section 2.1.3 also showed how pairs could be implemented as procedures. Painters are our second example of a procedural representation for data. +

    28 For example, the rogers painter of figure 2.11 was constructed from a gray-level image. For each point in a given frame, the rogers painter determines the point in the image that is mapped to it under the frame coordinate map, and shades it accordingly. By allowing different types of painters, we are capitalizing on the abstract data idea discussed in section 2.1.3, where we argued that a rational-number representation could be anything at all that satisfies an appropriate condition. Here we're using the fact that a painter can be implemented in any way at all, so long as it draws something in the designated frame. Section 2.1.3 also showed how pairs could be implemented as procedures. Painters are our second example of a procedural representation for data.

    @@ -3030,6 +3025,6 @@

    Levels of language for robust design

    -

    31 Section 3.3.4 describes one such language. +

    31 Section 3.3.4 describes one such language.

    @@ diff --git a/content/2-3-symbolic.content.html b/content/2-3-symbolic.content.html index be6a273..065f178 100644 --- a/content/2-3-symbolic.content.html +++ b/content/2-3-symbolic.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

    @@ -16,8 +16,7 @@

    Symbolic Data

    All the compound data objects we have used so far were constructed ultimately from numbers. In this section we extend the representational capability of our language by introducing the ability to work with arbitrary symbols as data. - -

    2.3.1 Quotation

    +

    2.3.1 Quotation

    If we can form compound data using symbols, we can have lists such as @@ -41,9 +40,9 @@

    2.3.1 Quotation

    C.no_output_prompt("scheme-list-exp-eg"); -

    In order to manipulate symbols we need a new element in our language: the ability to quote a data object. Suppose we want to construct the list (a b). We can't accomplish this with (list a b), because this expression constructs a list of the values of a and b rather than the symbols themselves. This issue is well known in the context of natural languages, where words and sentences may be regarded either as semantic entities or as character strings (syntactic entities). The common practice in natural languages is to use quotation marks to indicate that a word or a sentence is to be treated literally as a string of characters. For instance, the first letter of "John" is clearly "J." If we tell somebody "say your name aloud," we expect to hear that person's name. However, if we tell somebody "say `your name' aloud," we expect to hear the words "your name."" Note that we are forced to nest quotation marks to describe what somebody else might say.32 +

    In order to manipulate symbols we need a new element in our language: the ability to quote a data object. Suppose we want to construct the list (a b). We can't accomplish this with (list a b), because this expression constructs a list of the values of a and b rather than the symbols themselves. This issue is well known in the context of natural languages, where words and sentences may be regarded either as semantic entities or as character strings (syntactic entities). The common practice in natural languages is to use quotation marks to indicate that a word or a sentence is to be treated literally as a string of characters. For instance, the first letter of "John" is clearly "J." If we tell somebody "say your name aloud," we expect to hear that person's name. However, if we tell somebody "say `your name' aloud," we expect to hear the words "your name."" Note that we are forced to nest quotation marks to describe what somebody else might say.32 -

    We can follow this same practice to identify lists and symbols that are to be treated as data objects rather than as expressions to be evaluated. However, our format for quoting differs from that of natural languages in that we place a quotation mark (traditionally, the single quote symbol ') only at the beginning of the object to be quoted. We can get away with this in Scheme syntax because we rely on blanks and parentheses to delimit objects. Thus, the meaning of the single quote character is to quote the next object.33 +

    We can follow this same practice to identify lists and symbols that are to be treated as data objects rather than as expressions to be evaluated. However, our format for quoting differs from that of natural languages in that we place a quotation mark (traditionally, the single quote symbol ') only at the beginning of the object to be quoted. We can get away with this in Scheme syntax because we rely on blanks and parentheses to delimit objects. Thus, the meaning of the single quote character is to quote the next object.33

    Now we can distinguish between symbols and their values: @@ -82,7 +81,7 @@

    2.3.1 Quotation

    C.prompt("scheme-list-qa-b", ["scheme-define-a-b"]); -

    Quotation also allows us to type in compound objects, using the conventional printed representation for lists:34 +

    Quotation also allows us to type in compound objects, using the conventional printed representation for lists:34

    (car '(a b c)) @@ -100,9 +99,9 @@

    2.3.1 Quotation

    C.prompt("scheme-cdr-qabc"); -

    In keeping with this, we can obtain the empty list by evaluating '(), and thus dispense with the variable nil. +

    In keeping with this, we can obtain the empty list by evaluating '(), and thus dispense with the variable nil. -

    One additional primitive used in manipulating symbols is eq?, which takes two symbols as arguments and tests whether they are the same.35 Using eq?, we can implement a useful procedure called memq. This takes two arguments, a symbol and a list. If the symbol is not contained in the list (i.e., is not eq? to any item in the list), then memq returns false. Otherwise, it returns the sublist of the list beginning with the first occurrence of the symbol: +

    One additional primitive used in manipulating symbols is eq?, which takes two symbols as arguments and tests whether they are the same.35 Using eq?, we can implement a useful procedure called memq. This takes two arguments, a symbol and a list. If the symbol is not contained in the list (i.e., is not eq? to any item in the list), then memq returns false. Otherwise, it returns the sublist of the list beginning with the first occurrence of the symbol:

    (define (memq item x) @@ -132,9 +131,9 @@

    2.3.1 Quotation

    C.prompt("scheme-memq-apple-apple-sauce-apple-pear", ["scheme-define-memq"]); -

    is (apple pear). +

    is (apple pear). -

    +

    Exercise 2.53: What would the interpreter print in response to evaluating each of the following expressions?

    @@ -159,8 +158,8 @@

    2.3.1 Quotation

    -

    -

    Exercise 2.54: Two lists are said to be equal? if they contain equal elements arranged in the same order. For example, +

    +

    Exercise 2.54: Two lists are said to be equal? if they contain equal elements arranged in the same order. For example,

    (equal? '(this is a list) '(this is a list)) @@ -179,12 +178,12 @@

    2.3.1 Quotation

    -

    is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and the symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this idea, implement equal? as a procedure.36 +

    is false. To be more precise, we can define equal? recursively in terms of the basic eq? equality of symbols by saying that a and b are equal? if they are both symbols and the symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) is equal? to (cdr b). Using this idea, implement equal? as a procedure.36

    -

    +

    Exercise 2.55: Eva Lu Ator types to the interpreter the expression

    @@ -194,11 +193,10 @@

    2.3.1 Quotation

    C.no_output_frozen_prompt("scheme-car-qqabracadabra"); -

    To her surprise, the interpreter prints back quote. Explain. +

    To her surprise, the interpreter prints back quote. Explain.

    - -

    2.3.2 Example: Symbolic Differentiation

    +

    2.3.2 Example: Symbolic Differentiation

    As an illustration of symbol manipulation and a further illustration of data abstraction, consider the design of a procedure that performs symbolic differentiation of algebraic expressions. We would like the procedure to take as arguments an algebraic expression and a variable and to return the derivative of the expression with respect to the variable. For example, if the arguments to the procedure are ax^2 + bx + c and x, the procedure should return 2ax + b. Symbolic differentiation is of special historical significance in Lisp. It was one of the motivating examples behind the development of a computer language for symbol manipulation. Furthermore, it marked the beginning of the line of research that led to the development of powerful systems for symbolic mathematical work, which are currently being used by a growing number of applied mathematicians and physicists. @@ -220,19 +218,19 @@

    The differentiation program with abstract data

    To embody these rules in a procedure we indulge in a little wishful thinking, as we did in designing the rational-number implementation. If we had a means for representing algebraic expressions, we should be able to tell whether an expression is a sum, a product, a constant, or a variable. We should be able to extract the parts of an expression. For a sum, for example we want to be able to extract the addend (first term) and the augend (second term). We should also be able to construct expressions from parts. Let us assume that we already have procedures to implement the following selectors, constructors, and predicates:

    -(variable? e)          Is e a variable?
    -(same-variable? v1 v2) Are v1 and v2 the same variable?
    -(sum? e)               Is e a sum?
    -(addend e)             Addend of the sum e.
    -(augend e)             Augend of the sum e.
    -(make-sum a1 a2)       Construct the sum of a1 and a2.
    -(product? e)           Is e a product?
    -(multiplier e)         Multiplier of the product e.
    -(multiplicand e)       Multiplicand of the product e.
    -(make-product m1 m2)   Construct the product of m1 and m2.
    +(variable? e)          Is e a variable?
    +(same-variable? v1 v2) Are v1 and v2 the same variable?
    +(sum? e)               Is e a sum?
    +(addend e)             Addend of the sum e.
    +(augend e)             Augend of the sum e.
    +(make-sum a1 a2)       Construct the sum of a1 and a2.
    +(product? e)           Is e a product?
    +(multiplier e)         Multiplier of the product e.
    +(multiplicand e)       Multiplicand of the product e.
    +(make-product m1 m2)   Construct the product of m1 and m2.
     
    -

    Using these, and the primitive predicate number?, which identifies numbers, we can express the differentiation rules as the following procedure: +

    Using these, and the primitive predicate number?, which identifies numbers, we can express the differentiation rules as the following procedure:

    (define (deriv exp var) @@ -265,17 +263,17 @@

    The differentiation program with abstract data

    ]); -

    This deriv procedure incorporates the complete differentiation algorithm. Since it is expressed in terms of abstract data, it will work no matter how we choose to represent algebraic expressions, as long as we design a proper set of selectors and constructors. This is the issue we must address next. +

    This deriv procedure incorporates the complete differentiation algorithm. Since it is expressed in terms of abstract data, it will work no matter how we choose to represent algebraic expressions, as long as we design a proper set of selectors and constructors. This is the issue we must address next.

    Representing algebraic expressions

    -

    We can imagine many ways to use list structure to represent algebraic expressions. For example, we could use lists of symbols that mirror the usual algebraic notation, representing $ax + b$ as the list (a * x + b) . However, one especially straightforward choice is to use the same parenthesized prefix notation that Lisp uses for combinations; that is, to represent $ax + b$ as (+ (* a x) b). Then our data representation for the differentiation problem is as follows: +

    We can imagine many ways to use list structure to represent algebraic expressions. For example, we could use lists of symbols that mirror the usual algebraic notation, representing $ax + b$ as the list (a * x + b) . However, one especially straightforward choice is to use the same parenthesized prefix notation that Lisp uses for combinations; that is, to represent $ax + b$ as (+ (* a x) b). Then our data representation for the differentiation problem is as follows:

    • The variables are symbols. They are identified by the primitive predicate -symbol?: +symbol?:

      (define (variable? x) (symbol? x)) @@ -286,7 +284,7 @@

      Representing algebraic expressions

    • -

      Two variables are the same if the symbols representing them are eq?: +

      Two variables are the same if the symbols representing them are eq?:

      (define (same-variable? v1 v2) @@ -311,7 +309,7 @@

      Representing algebraic expressions

    • -

      A sum is a list whose first element is the symbol +: +

      A sum is a list whose first element is the symbol +:

      (define (sum? x) @@ -345,7 +343,7 @@

      Representing algebraic expressions

    • -

      A product is a list whose first element is the symbol *: +

      A product is a list whose first element is the symbol *:

      (define (product? x) @@ -380,7 +378,7 @@

      Representing algebraic expressions

    -

    Thus, we need only combine these with the algorithm as embodied by deriv in order to have a working symbolic-differentiation program. Let us look at some examples of its behavior: +

    Thus, we need only combine these with the algorithm as embodied by deriv in order to have a working symbolic-differentiation program. Let us look at some examples of its behavior:

    (deriv '(+ x 3) 'x) @@ -414,9 +412,9 @@

    Representing algebraic expressions

    $$ -

    but we would like the program to know that $x * 0 = 0$, $1 * y = y$, and $0 + y = y$. The answer for the second example should have been simply y. As the third example shows, this becomes a serious issue when the expressions are complex. +

    but we would like the program to know that $x * 0 = 0$, $1 * y = y$, and $0 + y = y$. The answer for the second example should have been simply y. As the third example shows, this becomes a serious issue when the expressions are complex. -

    Our difficulty is much like the one we encountered with the rational-number implementation: we haven't reduced answers to simplest form. To accomplish the rational-number reduction, we needed to change only the constructors and the selectors of the implementation. We can adopt a similar strategy here. We won't change deriv at all. Instead, we will change make-sum so that if both summands are numbers, make-sum will add them and return their sum. Also, if one of the summands is 0, then make-sum will return the other summand. +

    Our difficulty is much like the one we encountered with the rational-number implementation: we haven't reduced answers to simplest form. To accomplish the rational-number reduction, we needed to change only the constructors and the selectors of the implementation. We can adopt a similar strategy here. We won't change deriv at all. Instead, we will change make-sum so that if both summands are numbers, make-sum will add them and return their sum. Also, if one of the summands is 0, then make-sum will return the other summand.

    (define (make-sum a1 a2) @@ -429,7 +427,7 @@

    Representing algebraic expressions

    C.prompt("scheme-define-simplifying-make-sum", ["scheme-define-equal-numberp"]); -

    This uses the procedure =number?, which checks whether an expression is equal to a given number: +

    This uses the procedure =number?, which checks whether an expression is equal to a given number:

    (define (=number? exp num) @@ -439,7 +437,7 @@

    Representing algebraic expressions

    C.prompt("scheme-define-equal-numberp"); -

    Similarly, we will change make-product to build in the rules that 0 times anything is 0 and 1 times anything is the thing itself: +

    Similarly, we will change make-product to build in the rules that 0 times anything is 0 and 1 times anything is the thing itself:

    (define (make-product m1 m2) @@ -514,19 +512,19 @@

    Representing algebraic expressions

    Although this is quite an improvement, the third example shows that there is still a long way to go before we get a program that puts expressions into a form that we might agree is ``simplest.'' The problem of algebraic simplification is complex because, among other reasons, a form that may be simplest for one purpose may not be for another. -

    +

    Exercise 2.56: Show how to extend the basic differentiator to handle more kinds of expressions. For instance, implement the differentiation rule $$ \frac{d(u^n)}{dx} = nu^{n-1}\left(\frac{du}{dx}\right) $$ -by adding a new clause to the deriv program and defining appropriate procedures exponentiation?, base, exponent, and make-exponentiation. (You may use the symbol ** to denote exponentiation.) Build in the rules that anything raised to the power $0$ is $1$ and anything raised to the power $1$ is the thing itself. +by adding a new clause to the deriv program and defining appropriate procedures exponentiation?, base, exponent, and make-exponentiation. (You may use the symbol ** to denote exponentiation.) Build in the rules that anything raised to the power $0$ is $1$ and anything raised to the power $1$ is the thing itself.

    -

    +

    Exercise 2.57: Extend the differentiation program to handle sums and products of arbitrary numbers of (two or more) terms. Then the last example above could be expressed as

    @@ -536,35 +534,34 @@

    Representing algebraic expressions

    C.no_output_frozen_prompt("scheme-ex-test-deriv-sum"); -

    Try to do this by changing only the representation for sums and products, without changing the deriv procedure at all. For example, the addend of a sum would be the first term, and the augend would be the sum of the rest of the terms. +

    Try to do this by changing only the representation for sums and products, without changing the deriv procedure at all. For example, the addend of a sum would be the first term, and the augend would be the sum of the rest of the terms.

    -

    -

    Exercise 2.58: Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined in terms of abstract data, we can modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic expressions on which the differentiator is to operate. +

    +

    Exercise 2.58: Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined in terms of abstract data, we can modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic expressions on which the differentiator is to operate.

    • -

      Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that expressions are fully parenthesized. +

      Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that expressions are fully parenthesized.

    • -

      The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition. Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works? +

      The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition. Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works?

    - -

    2.3.3 Example: Representing Sets

    +

    2.3.3 Example: Representing Sets

    In the previous examples we built representations for two kinds of compound data objects: rational numbers and algebraic expressions. In one of these examples we had the choice of simplifying (reducing) the expressions at either construction time or selection time, but other than that the choice of a representation for these structures in terms of lists was straightforward. When we turn to the representation of sets, the choice of a representation is not so obvious. Indeed, there are a number of possible representations, and they differ significantly from one another in several ways. -

    Informally, a set is simply a collection of distinct objects. To give a more precise definition we can employ the method of data abstraction. That is, we define ``set'' by specifying the operations that are to be used on sets. These are union-set, intersection-set, element-of-set?, and adjoin-set. Element-of-set? is a predicate that determines whether a given element is a member of a set. Adjoin-set takes an object and a set as arguments and returns a set that contains the elements of the original set and also the adjoined element. Union-set computes the union of two sets, which is the set containing each element that appears in either argument. Intersection-set computes the intersection of two sets, which is the set containing only elements that appear in both arguments. From the viewpoint of data abstraction, we are free to design any representation that implements these operations in a way consistent with the interpretations given above.37 +

    Informally, a set is simply a collection of distinct objects. To give a more precise definition we can employ the method of data abstraction. That is, we define ``set'' by specifying the operations that are to be used on sets. These are union-set, intersection-set, element-of-set?, and adjoin-set. Element-of-set? is a predicate that determines whether a given element is a member of a set. Adjoin-set takes an object and a set as arguments and returns a set that contains the elements of the original set and also the adjoined element. Union-set computes the union of two sets, which is the set containing each element that appears in either argument. Intersection-set computes the intersection of two sets, which is the set containing only elements that appear in both arguments. From the viewpoint of data abstraction, we are free to design any representation that implements these operations in a way consistent with the interpretations given above.37

    Sets as unordered lists

    -

    One way to represent a set is as a list of its elements in which no element appears more than once. The empty set is represented by the empty list. In this representation, element-of-set? is similar to the procedure memq of section 2.3.1. It uses equal? instead of eq? so that the set elements need not be symbols: +

    One way to represent a set is as a list of its elements in which no element appears more than once. The empty set is represented by the empty list. In this representation, element-of-set? is similar to the procedure memq of section 2.3.1. It uses equal? instead of eq? so that the set elements need not be symbols:

    (define (element-of-set? x set) @@ -576,7 +573,7 @@

    Sets as unordered lists

    C.prompt("scheme-define-element-of-setp"); -

    Using this, we can write adjoin-set. If the object to be adjoined is already in the set, we just return the set. Otherwise, we use cons to add the object to the list that represents the set: +

    Using this, we can write adjoin-set. If the object to be adjoined is already in the set, we just return the set. Otherwise, we use cons to add the object to the list that represents the set:

    (define (adjoin-set x set) @@ -588,7 +585,7 @@

    Sets as unordered lists

    C.prompt("scheme-define-adjoin-set"); -

    For intersection-set we can use a recursive strategy. If we know how to form the intersection of set2 and the cdr of set1, we only need to decide whether to include the car of set1 in this. But this depends on whether (car set1) is also in set2. Here is the resulting procedure: +

    For intersection-set we can use a recursive strategy. If we know how to form the intersection of set2 and the cdr of set1, we only need to decide whether to include the car of set1 in this. But this depends on whether (car set1) is also in set2. Here is the resulting procedure:

    (define (intersection-set set1 set2) @@ -602,23 +599,23 @@

    Sets as unordered lists

    C.prompt("scheme-define-intersection-set"); -

    In designing a representation, one of the issues we should be concerned with is efficiency. Consider the number of steps required by our set operations. Since they all use element-of-set?, the speed of this operation has a major impact on the efficiency of the set implementation as a whole. Now, in order to check whether an object is a member of a set, element-of-set? may have to scan the entire set. (In the worst case, the object turns out not to be in the set.) Hence, if the set has n elements, element-of-set? might take up to n steps. Thus, the number of steps required grows as $\Theta(n)$. The number of steps required by adjoin-set, which uses this operation, also grows as $\Theta(n)$. For intersection-set, which does an element-of-set? check for each element of set1, the number of steps required grows as the product of the sizes of the sets involved, or $\Theta(n^2)$ for two sets of size n. The same will be true of union-set. +

    In designing a representation, one of the issues we should be concerned with is efficiency. Consider the number of steps required by our set operations. Since they all use element-of-set?, the speed of this operation has a major impact on the efficiency of the set implementation as a whole. Now, in order to check whether an object is a member of a set, element-of-set? may have to scan the entire set. (In the worst case, the object turns out not to be in the set.) Hence, if the set has n elements, element-of-set? might take up to n steps. Thus, the number of steps required grows as $\Theta(n)$. The number of steps required by adjoin-set, which uses this operation, also grows as $\Theta(n)$. For intersection-set, which does an element-of-set? check for each element of set1, the number of steps required grows as the product of the sizes of the sets involved, or $\Theta(n^2)$ for two sets of size n. The same will be true of union-set. -

    -

    Exercise 2.59: Implement the union-set operation for the unordered-list representation of sets. +

    +

    Exercise 2.59: Implement the union-set operation for the unordered-list representation of sets.

    -

    -

    Exercise 2.60: We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1,2,3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one? +

    +

    Exercise 2.60: We specified that a set would be represented as a list with no duplicates. Now suppose we allow duplicates. For instance, the set {1,2,3} could be represented as the list (2 3 2 1 3 2 2). Design procedures element-of-set?, adjoin-set, union-set, and intersection-set that operate on this representation. How does the efficiency of each compare with the corresponding procedure for the non-duplicate representation? Are there applications for which you would use this representation in preference to the non-duplicate one?

    Sets as ordered lists

    -

    One way to speed up our set operations is to change the representation so that the set elements are listed in increasing order. To do this, we need some way to compare two objects so that we can say which is bigger. For example, we could compare symbols lexicographically, or we could agree on some method for assigning a unique number to an object and then compare the elements by comparing the corresponding numbers. To keep our discussion simple, we will consider only the case where the set elements are numbers, so that we can compare elements using > and <. We will represent a set of numbers by listing its elements in increasing order. Whereas our first representation above allowed us to represent the set {1,3,6,10} by listing the elements in any order, our new representation allows only the list (1 3 6 10). +

    One way to speed up our set operations is to change the representation so that the set elements are listed in increasing order. To do this, we need some way to compare two objects so that we can say which is bigger. For example, we could compare symbols lexicographically, or we could agree on some method for assigning a unique number to an object and then compare the elements by comparing the corresponding numbers. To keep our discussion simple, we will consider only the case where the set elements are numbers, so that we can compare elements using > and <. We will represent a set of numbers by listing its elements in increasing order. Whereas our first representation above allowed us to represent the set {1,3,6,10} by listing the elements in any order, our new representation allows only the list (1 3 6 10). -

    One advantage of ordering shows up in element-of-set?: In checking for the presence of an item, we no longer have to scan the entire set. If we reach a set element that is larger than the item we are looking for, then we know that the item is not in the set: +

    One advantage of ordering shows up in element-of-set?: In checking for the presence of an item, we no longer have to scan the entire set. If we reach a set element that is larger than the item we are looking for, then we know that the item is not in the set:

    (define (element-of-set? x set) @@ -633,7 +630,7 @@

    Sets as ordered lists

    How many steps does this save? In the worst case, the item we are looking for may be the largest one in the set, so the number of steps is the same as for the unordered representation. On the other hand, if we search for items of many different sizes we can expect that sometimes we will be able to stop searching at a point near the beginning of the list and that other times we will still need to examine most of the list. On the average we should expect to have to examine about half of the items in the set. Thus, the average number of steps required will be about $\frac{n}{2}$. This is still $\Theta(n)$ growth, but it does save us, on the average, a factor of 2 in number of steps over the previous implementation. -

    We obtain a more impressive speedup with intersection-set. In the unordered representation this operation required $\Theta(n^2)$ steps, because we performed a complete scan of set2 for each element of set1. But with the ordered representation, we can use a more clever method. Begin by comparing the initial elements, x1 and x2, of the two sets. If x1 equals x2, then that gives an element of the intersection, and the rest of the intersection is the intersection of the cdrs of the two sets. Suppose, however, that x1 is less than x2. Since x2 is the smallest element in set2, we can immediately conclude that x1 cannot appear anywhere in set2 and hence is not in the intersection. Hence, the intersection is equal to the intersection of set2 with the cdr of set1. Similarly, if x2 is less than x1, then the intersection is given by the intersection of set1 with the cdr of set2. Here is the procedure: +

    We obtain a more impressive speedup with intersection-set. In the unordered representation this operation required $\Theta(n^2)$ steps, because we performed a complete scan of set2 for each element of set1. But with the ordered representation, we can use a more clever method. Begin by comparing the initial elements, x1 and x2, of the two sets. If x1 equals x2, then that gives an element of the intersection, and the rest of the intersection is the intersection of the cdrs of the two sets. Suppose, however, that x1 is less than x2. Since x2 is the smallest element in set2, we can immediately conclude that x1 cannot appear anywhere in set2 and hence is not in the intersection. Hence, the intersection is equal to the intersection of set2 with the cdr of set1. Similarly, if x2 is less than x1, then the intersection is given by the intersection of set1 with the cdr of set2. Here is the procedure:

    (define (intersection-set set1 set2) @@ -653,30 +650,30 @@

    Sets as ordered lists

    C.prompt("scheme-define-ordered-intersection-set"); -

    To estimate the number of steps required by this process, observe that at each step we reduce the intersection problem to computing intersections of smaller sets---removing the first element from set1 or set2 or both. Thus, the number of steps required is at most the sum of the sizes of set1 and set2, rather than the product of the sizes as with the unordered representation. This is $\Theta(n)$ growth rather than $\Theta(n^2)$ – a considerable speedup, even for sets of moderate size. +

    To estimate the number of steps required by this process, observe that at each step we reduce the intersection problem to computing intersections of smaller sets---removing the first element from set1 or set2 or both. Thus, the number of steps required is at most the sum of the sizes of set1 and set2, rather than the product of the sizes as with the unordered representation. This is $\Theta(n)$ growth rather than $\Theta(n^2)$ – a considerable speedup, even for sets of moderate size. -

    -

    Exercise 2.61: Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation. +

    +

    Exercise 2.61: Give an implementation of adjoin-set using the ordered representation. By analogy with element-of-set? show how to take advantage of the ordering to produce a procedure that requires on the average about half as many steps as with the unordered representation.

    -

    -

    Exercise 2.62: Give a $\Theta(n)$ implementation of union-set for sets represented as ordered lists. +

    +

    Exercise 2.62: Give a $\Theta(n)$ implementation of union-set for sets represented as ordered lists.

    Sets as binary trees

    -

    We can do better than the ordered-list representation by arranging the set elements in the form of a tree. Each node of the tree holds one element of the set, called the ``entry'' at that node, and a link to each of two other (possibly empty) nodes. The ``left'' link points to elements smaller than the one at the node, and the ``right'' link to elements greater than the one at the node. Figure 2-16 shows some trees that represent the set {1,3,5,7,9,11}. The same set may be represented by a tree in a number of different ways. The only thing we require for a valid representation is that all elements in the left subtree be smaller than the node entry and that all elements in the right subtree be larger. +

    We can do better than the ordered-list representation by arranging the set elements in the form of a tree. Each node of the tree holds one element of the set, called the ``entry'' at that node, and a link to each of two other (possibly empty) nodes. The ``left'' link points to elements smaller than the one at the node, and the ``right'' link to elements greater than the one at the node. Figure 2-16 shows some trees that represent the set {1,3,5,7,9,11}. The same set may be represented by a tree in a number of different ways. The only thing we require for a valid representation is that all elements in the left subtree be smaller than the node entry and that all elements in the right subtree be larger.

    Figure 2.16: Various binary trees that represent the set $\{1,3,5,7,9,11\}$.

    -

    The advantage of the tree representation is this: Suppose we want to check whether a number x is contained in a set. We begin by comparing x with the entry in the top node. If x is less than this, we know that we need only search the left subtree; if x is greater, we need only search the right subtree. Now, if the tree is ``balanced,'' each of these subtrees will be about half the size of the original. Thus, in one step we have reduced the problem of searching a tree of size n to searching a tree of size n/2. Since the size of the tree is halved at each step, we should expect that the number of steps needed to search a tree of size n grows as $\Theta(\log(n))$.38 For large sets, this will be a significant speedup over the previous representations. +

    The advantage of the tree representation is this: Suppose we want to check whether a number x is contained in a set. We begin by comparing x with the entry in the top node. If x is less than this, we know that we need only search the left subtree; if x is greater, we need only search the right subtree. Now, if the tree is ``balanced,'' each of these subtrees will be about half the size of the original. Thus, in one step we have reduced the problem of searching a tree of size n to searching a tree of size n/2. Since the size of the tree is halved at each step, we should expect that the number of steps needed to search a tree of size n grows as $\Theta(\log(n))$.38 For large sets, this will be a significant speedup over the previous representations. -

    We can represent trees by using lists. Each node will be a list of three items: the entry at the node, the left subtree, and the right subtree. A left or a right subtree of the empty list will indicate that there is no subtree connected there. We can describe this representation by the following procedures:39 +

    We can represent trees by using lists. Each node will be a list of three items: the entry at the node, the left subtree, and the right subtree. A left or a right subtree of the empty list will indicate that there is no subtree connected there. We can describe this representation by the following procedures:39

    (define (entry tree) (car tree)) @@ -692,7 +689,7 @@

    Sets as binary trees

    C.prompt("scheme-define-bst-accessors"); -

    Now we can write the element-of-set? procedure using the strategy described above: +

    Now we can write the element-of-set? procedure using the strategy described above:

    (define (element-of-set? x set) @@ -726,14 +723,14 @@

    Sets as binary trees

    C.prompt("scheme-define-binary-adjoin-set", ["scheme-define-bst-accessors"]); -

    The above claim that searching the tree can be performed in a logarithmic number of steps rests on the assumption that the tree is ``balanced,'' i.e., that the left and the right subtree of every tree have approximately the same number of elements, so that each subtree contains about half the elements of its parent. But how can we be certain that the trees we construct will be balanced? Even if we start with a balanced tree, adding elements with adjoin-set may produce an unbalanced result. Since the position of a newly adjoined element depends on how the element compares with the items already in the set, we can expect that if we add elements ``randomly'' the tree will tend to be balanced on the average. But this is not a guarantee. For example, if we start with an empty set and adjoin the numbers 1 through 7 in sequence we end up with the highly unbalanced tree shown in Figure 2-17. In this tree all the left subtrees are empty, so it has no advantage over a simple ordered list. One way to solve this problem is to define an operation that transforms an arbitrary tree into a balanced tree with the same elements. Then we can perform this transformation after every few adjoin-set operations to keep our set in balance. There are also other ways to solve this problem, most of which involve designing new data structures for which searching and insertion both can be done in $\Theta(\log(n))$ steps.40 +

    The above claim that searching the tree can be performed in a logarithmic number of steps rests on the assumption that the tree is ``balanced,'' i.e., that the left and the right subtree of every tree have approximately the same number of elements, so that each subtree contains about half the elements of its parent. But how can we be certain that the trees we construct will be balanced? Even if we start with a balanced tree, adding elements with adjoin-set may produce an unbalanced result. Since the position of a newly adjoined element depends on how the element compares with the items already in the set, we can expect that if we add elements ``randomly'' the tree will tend to be balanced on the average. But this is not a guarantee. For example, if we start with an empty set and adjoin the numbers 1 through 7 in sequence we end up with the highly unbalanced tree shown in Figure 2-17. In this tree all the left subtrees are empty, so it has no advantage over a simple ordered list. One way to solve this problem is to define an operation that transforms an arbitrary tree into a balanced tree with the same elements. Then we can perform this transformation after every few adjoin-set operations to keep our set in balance. There are also other ways to solve this problem, most of which involve designing new data structures for which searching and insertion both can be done in $\Theta(\log(n))$ steps.40

    Figure 2.17: Unbalanced tree produced by adjoining 1 through 7 in sequence.

    -
    +

    Exercise 2.63: Each of the following two procedures converts a binary tree to a list.

    @@ -771,8 +768,8 @@

    Sets as binary trees

    -

    -

    Exercise 2.64: The following procedure list->tree converts an ordered list to a balanced binary tree. The helper procedure partial-tree takes as arguments an integer n and list of at least n elements and constructs a balanced tree containing the first n elements of the list. The result returned by partial-tree is a pair (formed with cons) whose car is the constructed tree and whose cdr is the list of elements not included in the tree. +

    +

    Exercise 2.64: The following procedure list->tree converts an ordered list to a balanced binary tree. The helper procedure partial-tree takes as arguments an integer n and list of at least n elements and constructs a balanced tree containing the first n elements of the list. The result returned by partial-tree is a pair (formed with cons) whose car is the constructed tree and whose cdr is the list of elements not included in the tree.

    (define (list->tree elements) @@ -800,28 +797,28 @@

    Sets as binary trees

    • -

      Write a short paragraph explaining as clearly as you can how partial-tree works. Draw the tree produced by list->tree for the list (1 3 5 7 9 11). +

      Write a short paragraph explaining as clearly as you can how partial-tree works. Draw the tree produced by list->tree for the list (1 3 5 7 9 11).

    • -

      What is the order of growth in the number of steps required by list->tree to convert a list of n elements? +

      What is the order of growth in the number of steps required by list->tree to convert a list of n elements?

    -

    -

    Exercise 2.65: Use the results of Exercise 2-63 and Exercise 2-64 to give $\Theta(n)$ implementations of union-set and intersection-set for sets implemented as (balanced) binary trees.41 +

    +

    Exercise 2.65: Use the results of Exercise 2-63 and Exercise 2-64 to give $\Theta(n)$ implementations of union-set and intersection-set for sets implemented as (balanced) binary trees.41

    Sets and information retrieval

    We have examined options for using lists to represent sets and have seen how the choice of representation for a data object can have a large impact on the performance of the programs that use the data. Another reason for concentrating on sets is that the techniques discussed here appear again and again in applications involving information retrieval. -

    Consider a data base containing a large number of individual records, such as the personnel files for a company or the transactions in an accounting system. A typical data-management system spends a large amount of time accessing or modifying the data in the records and therefore requires an efficient method for accessing records. This is done by identifying a part of each record to serve as an identifying key . A key can be anything that uniquely identifies the record. For a personnel file, it might be an employee's ID number. For an accounting system, it might be a transaction number. Whatever the key is, when we define the record as a data structure we should include a key selector procedure that retrieves the key associated with a given record. +

    Consider a data base containing a large number of individual records, such as the personnel files for a company or the transactions in an accounting system. A typical data-management system spends a large amount of time accessing or modifying the data in the records and therefore requires an efficient method for accessing records. This is done by identifying a part of each record to serve as an identifying key . A key can be anything that uniquely identifies the record. For a personnel file, it might be an employee's ID number. For an accounting system, it might be a transaction number. Whatever the key is, when we define the record as a data structure we should include a key selector procedure that retrieves the key associated with a given record. -

    Now we represent the data base as a set of records. To locate the record with a given key we use a procedure lookup, which takes as arguments a key and a data base and which returns the record that has that key, or false if there is no such record. Lookup is implemented in almost the same way as element-of-set?. For example, if the set of records is implemented as an unordered list, we could use +

    Now we represent the data base as a set of records. To locate the record with a given key we use a procedure lookup, which takes as arguments a key and a data base and which returns the record that has that key, or false if there is no such record. Lookup is implemented in almost the same way as element-of-set?. For example, if the set of records is implemented as an unordered list, we could use

    (define (lookup given-key set-of-records) @@ -836,16 +833,15 @@

    Sets and information retrieval

    Of course, there are better ways to represent large sets than as unordered lists. Information-retrieval systems in which records have to be ``randomly accessed'' are typically implemented by a tree-based method, such as the binary-tree representation discussed previously. In designing such a system the methodology of data abstraction can be a great help. The designer can create an initial implementation using a simple, straightforward representation such as unordered lists. This will be unsuitable for the eventual system, but it can be useful in providing a ``quick and dirty'' data base with which to test the rest of the system. Later on, the data representation can be modified to be more sophisticated. If the data base is accessed in terms of abstract selectors and constructors, this change in representation will not require any changes to the rest of the system. -

    -

    Exercise 2.66: Implement the lookup +

    +

    Exercise 2.66: Implement the lookup procedure for the case where the set of records is structured as a binary tree, ordered by the numerical values of the keys.

    - -

    2.3.4 Example: Huffman Encoding Trees

    +

    2.3.4 Example: Huffman Encoding Trees

    -

    This section provides practice in the use of list structure and data abstraction to manipulate sets and trees. The application is to methods for representing data as sequences of ones and zeros (bits). For example, the ASCII standard code used to represent text in computers encodes each character as a sequence of seven bits. Using seven bits allows us to distinguish 2^(7), or 128, possible different characters. In general, if we want to distinguish n different symbols, we will need to use log_2 n bits per symbol. If all our messages are made up of the eight symbols A, B, C, D, E, F, G, and H, we can choose a code with three bits per character, for example +

    This section provides practice in the use of list structure and data abstraction to manipulate sets and trees. The application is to methods for representing data as sequences of ones and zeros (bits). For example, the ASCII standard code used to represent text in computers encodes each character as a sequence of seven bits. Using seven bits allows us to distinguish 2^(7), or 128, possible different characters. In general, if we want to distinguish n different symbols, we will need to use log_2 n bits per symbol. If all our messages are made up of the eight symbols A, B, C, D, E, F, G, and H, we can choose a code with three bits per character, for example

     A 000 C 010 E 100 G 110
    @@ -864,7 +860,7 @@ 

    2.3.4 Example: Huffman Encoding Trees

    001000010000011000100000101000001001000000000110000111
    -

    Codes such as ASCII and the A-through-H code above are known as fixed-length codes, because they represent each symbol in the message with the same number of bits. It is sometimes advantageous to use variable-length codes, in which different symbols may be represented by different numbers of bits. For example, Morse code does not use the same number of dots and dashes for each letter of the alphabet. In particular, E, the most frequent letter, is represented by a single dot. In general, if our messages are such that some symbols appear very frequently and some very rarely, we can encode data more efficiently (i.e., using fewer bits per message) if we assign shorter codes to the frequent symbols. Consider the following alternative code for the letters A through H: +

    Codes such as ASCII and the A-through-H code above are known as fixed-length codes, because they represent each symbol in the message with the same number of bits. It is sometimes advantageous to use variable-length codes, in which different symbols may be represented by different numbers of bits. For example, Morse code does not use the same number of dots and dashes for each letter of the alphabet. In particular, E, the most frequent letter, is represented by a single dot. In general, if our messages are such that some symbols appear very frequently and some very rarely, we can encode data more efficiently (i.e., using fewer bits per message) if we assign shorter codes to the frequent symbols. Consider the following alternative code for the letters A through H:

     A 0   C 1010  E 1100  G 1110
    @@ -879,7 +875,7 @@ 

    2.3.4 Example: Huffman Encoding Trees

    This string contains 42 bits, so it saves more than 20% in space in comparison with the fixed-length code shown above. -

    One of the difficulties of using a variable-length code is knowing when you have reached the end of a symbol in reading a sequence of zeros and ones. Morse code solves this problem by using a special separator code (in this case, a pause) after the sequence of dots and dashes for each letter. Another solution is to design the code in such a way that no complete code for any symbol is the beginning (or prefix ) of the code for another symbol. Such a code is called a prefix code . In the example above, A is encoded by 0 and B is encoded by 100, so no other symbol can have a code that begins with 0 or with 100. +

    One of the difficulties of using a variable-length code is knowing when you have reached the end of a symbol in reading a sequence of zeros and ones. Morse code solves this problem by using a special separator code (in this case, a pause) after the sequence of dots and dashes for each letter. Another solution is to design the code in such a way that no complete code for any symbol is the beginning (or prefix ) of the code for another symbol. Such a code is called a prefix code . In the example above, A is encoded by 0 and B is encoded by 100, so no other symbol can have a code that begins with 0 or with 100.

    In general, we can attain significant savings if we use variable-length prefix codes that take advantage of the relative frequencies of the symbols in the messages to be encoded. One particular scheme for doing this is called the Huffman encoding method, after its discoverer, David Huffman. A Huffman code can be represented as a binary tree whose leaves are the symbols that are encoded. At each non-leaf node of the tree there is a set containing all the symbols in the leaves that lie below the node. In addition, each symbol at a leaf is assigned a weight (which is its relative frequency), and each non-leaf node contains a weight that is the sum of all the weights of the leaves lying below it. The weights are not used in the encoding or the decoding process. We will see below how they are used to help construct the tree. @@ -896,7 +892,7 @@

    2.3.4 Example: Huffman Encoding Trees

    Generating Huffman trees

    -

    Given an "alphabet" of symbols and their relative frequencies, how do we construct the "best" code? (In other words, which tree will encode messages with the fewest bits?) Huffman gave an algorithm for doing this and showed that the resulting code is indeed the best variable-length code for messages where the relative frequency of the symbols matches the frequencies with which the code was constructed. We will not prove this optimality of Huffman codes here, but we will show how Huffman trees are constructed.42 +

    Given an "alphabet" of symbols and their relative frequencies, how do we construct the "best" code? (In other words, which tree will encode messages with the fewest bits?) Huffman gave an algorithm for doing this and showed that the resulting code is indeed the best variable-length code for messages where the relative frequency of the symbols matches the frequencies with which the code was constructed. We will not prove this optimality of Huffman codes here, but we will show how Huffman trees are constructed.42

    The algorithm for generating a Huffman tree is very simple. The idea is to arrange the tree so that the symbols with the lowest frequency appear farthest away from the root. Begin with the set of leaf nodes, containing symbols and their frequencies, as determined by the initial data from which the code is to be constructed. Now find two leaves with the lowest weights and merge them to produce a node that has these two nodes as its left and right branches. The weight of the new node is the sum of the two weights. Remove the two leaves from the original set and replace them by this new node. Now continue this process. At each step, merge two nodes with the smallest weights, removing them from the set and replacing them with a node that has these two as its left and right branches. The process stops when there is only one node left, which is the root of the entire tree. Here is how the Huffman tree of Figure 2-18 was generated: @@ -917,7 +913,7 @@

    Representing Huffman trees

    In the exercises below we will work with a system that uses Huffman trees to encode and decode messages and generates Huffman trees according to the algorithm outlined above. We will begin by discussing how trees are represented. -

    Leaves of the tree are represented by a list consisting of the symbol leaf, the symbol at the leaf, and the weight: +

    Leaves of the tree are represented by a list consisting of the symbol leaf, the symbol at the leaf, and the weight:

    (define (make-leaf symbol weight) @@ -934,7 +930,7 @@

    Representing Huffman trees

    C.prompt("scheme-define-leaf-selectors"); -

    A general tree will be a list of a left branch, a right branch, a set of symbols, and a weight. The set of symbols will be simply a list of the symbols, rather than some more sophisticated set representation. When we make a tree by merging two nodes, we obtain the weight of the tree as the sum of the weights of the nodes, and the set of symbols as the union of the sets of symbols for the nodes. Since our symbol sets are represented as lists, we can form the union by using the append procedure we defined in section 2.2.1: +

    A general tree will be a list of a left branch, a right branch, a set of symbols, and a weight. The set of symbols will be simply a list of the symbols, rather than some more sophisticated set representation. When we make a tree by merging two nodes, we obtain the weight of the tree as the sum of the weights of the nodes, and the set of symbols as the union of the sets of symbols for the nodes. Since our symbol sets are represented as lists, we can form the union by using the append procedure we defined in section 2.2.1:

    (define (make-code-tree left right) @@ -968,7 +964,7 @@

    Representing Huffman trees

    C.prompt("scheme-define-huffman-selectors"); -

    The procedures symbols and weight must do something slightly different depending on whether they are called with a leaf or a general tree. These are simple examples of generic procedures (procedures that can handle more than one kind of data), which we will have much more to say about in sections 2-4 and 2-5. +

    The procedures symbols and weight must do something slightly different depending on whether they are called with a leaf or a general tree. These are simple examples of generic procedures (procedures that can handle more than one kind of data), which we will have much more to say about in sections 2-4 and 2-5.

    The decoding procedure

    @@ -996,13 +992,13 @@

    The decoding procedure

    C.prompt("scheme-define-decode"); -

    The procedure decode-1 takes two arguments: the list of remaining bits and the current position in the tree. It keeps moving ``down'' the tree, choosing a left or a right branch according to whether the next bit in the list is a zero or a one. (This is done with the procedure choose-branch.) When it reaches a leaf, it returns the symbol at that leaf as the next symbol in the message by consing it onto the result of decoding the rest of the message, starting at the root of the tree. Note the error check in the final clause of choose-branch, which complains if the procedure finds something other than a zero or a one in the input data. +

    The procedure decode-1 takes two arguments: the list of remaining bits and the current position in the tree. It keeps moving ``down'' the tree, choosing a left or a right branch according to whether the next bit in the list is a zero or a one. (This is done with the procedure choose-branch.) When it reaches a leaf, it returns the symbol at that leaf as the next symbol in the message by consing it onto the result of decoding the rest of the message, starting at the root of the tree. Note the error check in the final clause of choose-branch, which complains if the procedure finds something other than a zero or a one in the input data.

    Sets of weighted elements

    In our representation of trees, each non-leaf node contains a set of symbols, which we have represented as a simple list. However, the tree-generating algorithm discussed above requires that we also work with sets of leaves and trees, successively merging the two smallest items. Since we will be required to repeatedly find the smallest item in a set, it is convenient to use an ordered representation for this kind of set. -

    We will represent a set of leaves and trees as a list of elements, arranged in increasing order of weight. The following adjoin-set procedure for constructing sets is similar to the one described in Exercise 2-61; however, items are compared by their weights, and the element being added to the set is never already in it. +

    We will represent a set of leaves and trees as a list of elements, arranged in increasing order of weight. The following adjoin-set procedure for constructing sets is similar to the one described in Exercise 2-61; however, items are compared by their weights, and the element being added to the set is never already in it.

    (define (adjoin-set x set) @@ -1015,7 +1011,7 @@

    Sets of weighted elements

    C.prompt("scheme-define-weighted-adjoin"); -

    The following procedure takes a list of symbol-frequency pairs such as ((A 4) (B 2) (C 1) (D 1)) and constructs an initial ordered set of leaves, ready to be merged according to the Huffman algorithm: +

    The following procedure takes a list of symbol-frequency pairs such as ((A 4) (B 2) (C 1) (D 1)) and constructs an initial ordered set of leaves, ready to be merged according to the Huffman algorithm:

    (define (make-leaf-set pairs) @@ -1032,7 +1028,7 @@

    Sets of weighted elements

    -

    +

    Exercise 2.67: Define an encoding tree and a sample message:

    @@ -1049,13 +1045,13 @@

    Sets of weighted elements

    C.no_output_frozen_prompt("scheme-define-sample-tree"); -

    Use the decode procedure to decode the message, and give the result. +

    Use the decode procedure to decode the message, and give the result.

    -

    -

    Exercise 2.68: The encode procedure takes as arguments a message and a tree and produces the list of bits that gives the encoded message. +

    +

    Exercise 2.68: The encode procedure takes as arguments a message and a tree and produces the list of bits that gives the encoded message.

    (define (encode message tree) @@ -1068,12 +1064,12 @@

    Sets of weighted elements

    C.no_output_frozen_prompt("scheme-define-encode"); -

    encode-symbol is a procedure, which you must write, that returns the list of bits that encodes a given symbol according to a given tree. You should design encode-symbol so that it signals an error if the symbol is not in the tree at all. Test your procedure by encoding the result you obtained in Exercise 2-67 with the sample tree and seeing whether it is the same as the original sample message. +

    encode-symbol is a procedure, which you must write, that returns the list of bits that encodes a given symbol according to a given tree. You should design encode-symbol so that it signals an error if the symbol is not in the tree at all. Test your procedure by encoding the result you obtained in Exercise 2-67 with the sample tree and seeing whether it is the same as the original sample message.

    -

    +

    Exercise 2.69: The following procedure takes as its argument a list of symbol-frequency pairs (where no symbol appears in more than one pair) and generates a Huffman encoding tree according to the Huffman algorithm.

    @@ -1084,12 +1080,12 @@

    Sets of weighted elements

    C.no_output_frozen_prompt("scheme-define-generate-huffman-tree"); -

    make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set of leaves. Successive-merge is the procedure you must write, using make-code-tree to successively merge the smallest-weight elements of the set until there is only one element left, which is the desired Huffman tree. (This procedure is slightly tricky, but not really complicated. If you find yourself designing a complex procedure, then you are almost certainly doing something wrong. You can take significant advantage of the fact that we are using an ordered set representation.) +

    make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set of leaves. Successive-merge is the procedure you must write, using make-code-tree to successively merge the smallest-weight elements of the set until there is only one element left, which is the desired Huffman tree. (This procedure is slightly tricky, but not really complicated. If you find yourself designing a complex procedure, then you are almost certainly doing something wrong. You can take significant advantage of the fact that we are using an ordered set representation.)

    -

    +

    Exercise 2.70: The following eight-symbol alphabet with associated relative frequencies was designed to efficiently encode the lyrics of 1950s rock songs. (Note that the "symbols" of an "alphabet" need not be individual letters.)

    @@ -1099,7 +1095,7 @@ 

    Sets of weighted elements

    JOB 2 WAH 1
    -

    Use generate-huffman-tree (Exercise 2-69) to generate a corresponding Huffman tree, and use encode (Exercise 2-68) to encode the following message: +

    Use generate-huffman-tree (Exercise 2-69) to generate a corresponding Huffman tree, and use encode (Exercise 2-68) to encode the following message:

     Get a job
    @@ -1120,13 +1116,13 @@ 

    Sets of weighted elements

    -

    +

    Exercise 2.71: Suppose we have a Huffman tree for an alphabet of n symbols, and that the relative frequencies of the symbols are 1, 2, 4, ..., 2^(n-1). Sketch the tree for n=5; for n=10. In such a tree (for general n) how may bits are required to encode the most frequent symbol? the least frequent symbol?

    -

    +

    Exercise 2.72: Consider the encoding procedure that you designed in Exercise 2-68. What is the order of growth in the number of steps needed to encode a symbol? Be sure to include the number of steps needed to search the symbol list at each node encountered. To answer this question in general is difficult. Consider the special case where the relative frequencies of the n symbols are as described in Exercise 2-71, and give the order of growth (as a function of n) of the number of steps needed to encode the most frequent and least frequent symbols in the alphabet.

    @@ @@ -1142,15 +1138,15 @@

    Sets of weighted elements

    -

    34 Strictly, our use of the quotation mark violates the general rule that all compound expressions in our language should be delimited by parentheses and look like lists. We can recover this consistency by introducing a special form quote, which serves the same purpose as the quotation mark. Thus, we would type (quote a) instead of 'a, and we would type (quote (a b c)) instead of '(a b c). This is precisely how the interpreter works. The quotation mark is just a single-character abbreviation for wrapping the next complete expression with quote to form (quote <expression>). This is important because it maintains the principle that any expression seen by the interpreter can be manipulated as a data object. For instance, we could construct the expression (car '(a b c)), which is the same as (car (quote (a b c))), by evaluating (list 'car (list 'quote '(a b c))). +

    34 Strictly, our use of the quotation mark violates the general rule that all compound expressions in our language should be delimited by parentheses and look like lists. We can recover this consistency by introducing a special form quote, which serves the same purpose as the quotation mark. Thus, we would type (quote a) instead of 'a, and we would type (quote (a b c)) instead of '(a b c). This is precisely how the interpreter works. The quotation mark is just a single-character abbreviation for wrapping the next complete expression with quote to form (quote <expression>). This is important because it maintains the principle that any expression seen by the interpreter can be manipulated as a data object. For instance, we could construct the expression (car '(a b c)), which is the same as (car (quote (a b c))), by evaluating (list 'car (list 'quote '(a b c))).

    -

    35 We can consider two symbols to be "the same" if they consist of the same characters in the same order. Such a definition skirts a deep issue that we are not yet ready to address: the meaning of "sameness" in a programming language. We will return to this in chapter 3 (section 3.1.3). +

    35 We can consider two symbols to be "the same" if they consist of the same characters in the same order. Such a definition skirts a deep issue that we are not yet ready to address: the meaning of "sameness" in a programming language. We will return to this in chapter 3 (section 3.1.3).

    -

    36 In practice, programmers use equal? to compare lists that contain numbers as well as symbols. Numbers are not considered to be symbols. The question of whether two numerically equal numbers (as tested by =) are also eq? is highly implementation-dependent. A better definition of equal? (such as the one that comes as a primitive in Scheme) would also stipulate that if $a$ and $b$ are both numbers, then $a$ and $b$ are equal? if they are numerically equal. +

    36 In practice, programmers use equal? to compare lists that contain numbers as well as symbols. Numbers are not considered to be symbols. The question of whether two numerically equal numbers (as tested by =) are also eq? is highly implementation-dependent. A better definition of equal? (such as the one that comes as a primitive in Scheme) would also stipulate that if $a$ and $b$ are both numbers, then $a$ and $b$ are equal? if they are numerically equal.

    @@ -1158,25 +1154,25 @@

    Sets of weighted elements

    • -

      For any set $S$ and any object $x$, (element-of-set? x (adjoin-set x S)) is true (informally: "Adjoining an object to a set produces a set that contains the object"). +

      For any set $S$ and any object $x$, (element-of-set? x (adjoin-set x S)) is true (informally: "Adjoining an object to a set produces a set that contains the object").

    • -

      For any sets $S$ and $T$ and any object $x$, (element-of-set? x (union-set S T)) is equal to (or (element-of-set? x S) (element-of-set? x T)) (informally: "The elements of (union S T) are the elements that are in $S$ or in $T$"). +

      For any sets $S$ and $T$ and any object $x$, (element-of-set? x (union-set S T)) is equal to (or (element-of-set? x S) (element-of-set? x T)) (informally: "The elements of (union S T) are the elements that are in $S$ or in $T$").

    • -

      For any object $x$, (element-of-set? x '()) is false (informally: "No object is an element of the empty set"). +

      For any object $x$, (element-of-set? x '()) is false (informally: "No object is an element of the empty set").

    -

    38 Halving the size of the problem at each step is the distinguishing characteristic of logarithmic growth, as we saw with the fast-exponentiation algorithm of section 1.2.4 and the half-interval search method of section 1.3.3. +

    38 Halving the size of the problem at each step is the distinguishing characteristic of logarithmic growth, as we saw with the fast-exponentiation algorithm of section 1.2.4 and the half-interval search method of section 1.3.3.

    -

    39 We are representing sets in terms of trees, and trees in terms of lists – in effect, a data abstraction built upon a data abstraction. We can regard the procedures entry, left-branch, right-branch, and make-tree as a way of isolating the abstraction of a "binary tree" from the particular way we might wish to represent such a tree in terms of list structure. +

    39 We are representing sets in terms of trees, and trees in terms of lists – in effect, a data abstraction built upon a data abstraction. We can regard the procedures entry, left-branch, right-branch, and make-tree as a way of isolating the abstraction of a "binary tree" from the particular way we might wish to represent such a tree in terms of list structure.

    diff --git a/content/2-4-representation.content.html b/content/2-4-representation.content.html index 82b52aa..05f3e0a 100644 --- a/content/2-4-representation.content.html +++ b/content/2-4-representation.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

    @@ -14,17 +14,17 @@

    Multiple Representations for Abstract Data


    -

    We have introduced data abstraction, a methodology for structuring systems in such a way that much of a program can be specified independent of the choices involved in implementing the data objects that the program manipulates. For example, we saw in section 2.1.1 how to separate the task of designing a program that uses rational numbers from the task of implementing rational numbers in terms of the computer language's primitive mechanisms for constructing compound data. The key idea was to erect an abstraction barrier -- in this case, the selectors and constructors for rational numbers (make-rat, numer, denom)---that isolates the way rational numbers are used from their underlying representation in terms of list structure. A similar abstraction barrier isolates the details of the procedures that perform rational arithmetic (add-rat, sub-rat, mul-rat, and div-rat) from the ``higher-level'' procedures that use rational numbers. The resulting program has the structure shown in Figure 2-1. +

    We have introduced data abstraction, a methodology for structuring systems in such a way that much of a program can be specified independent of the choices involved in implementing the data objects that the program manipulates. For example, we saw in section 2.1.1 how to separate the task of designing a program that uses rational numbers from the task of implementing rational numbers in terms of the computer language's primitive mechanisms for constructing compound data. The key idea was to erect an abstraction barrier -- in this case, the selectors and constructors for rational numbers (make-rat, numer, denom)---that isolates the way rational numbers are used from their underlying representation in terms of list structure. A similar abstraction barrier isolates the details of the procedures that perform rational arithmetic (add-rat, sub-rat, mul-rat, and div-rat) from the ``higher-level'' procedures that use rational numbers. The resulting program has the structure shown in Figure 2-1.

    These data-abstraction barriers are powerful tools for controlling complexity. By isolating the underlying representations of data objects, we can divide the task of designing a large program into smaller tasks that can be performed separately. But this kind of data abstraction is not yet powerful enough, because it may not always make sense to speak of ``the underlying representation'' for a data object.

    For one thing, there might be more than one useful representation for a data object, and we might like to design systems that can deal with multiple representations. To take a simple example, complex numbers may be represented in two almost equivalent ways: in rectangular form (real and imaginary parts) and in polar form (magnitude and angle). Sometimes rectangular form is more appropriate and sometimes polar form is more appropriate. Indeed, it is perfectly plausible to imagine a system in which complex numbers are represented in both ways, and in which the procedures for manipulating complex numbers work with either representation. -

    More importantly, programming systems are often designed by many people working over extended periods of time, subject to requirements that change over time. In such an environment, it is simply not possible for everyone to agree in advance on choices of data representation. So in addition to the data-abstraction barriers that isolate representation from use, we need abstraction barriers that isolate different design choices from each other and permit different choices to coexist in a single program. Furthermore, since large programs are often created by combining pre-existing modules that were designed in isolation, we need conventions that permit programmers to incorporate modules into larger systems additively , that is, without having to redesign or reimplement these modules. +

    More importantly, programming systems are often designed by many people working over extended periods of time, subject to requirements that change over time. In such an environment, it is simply not possible for everyone to agree in advance on choices of data representation. So in addition to the data-abstraction barriers that isolate representation from use, we need abstraction barriers that isolate different design choices from each other and permit different choices to coexist in a single program. Furthermore, since large programs are often created by combining pre-existing modules that were designed in isolation, we need conventions that permit programmers to incorporate modules into larger systems additively , that is, without having to redesign or reimplement these modules. -

    In this section, we will learn how to cope with data that may be represented in different ways by different parts of a program. This requires constructing generic procedures ---procedures that can operate on data that may be represented in more than one way. Our main technique for building generic procedures will be to work in terms of data objects that have type tags , that is, data objects that include explicit information about how they are to be processed. We will also discuss data-directed programming, a powerful and convenient implementation strategy for additively assembling systems with generic operations. +

    In this section, we will learn how to cope with data that may be represented in different ways by different parts of a program. This requires constructing generic procedures ---procedures that can operate on data that may be represented in more than one way. Our main technique for building generic procedures will be to work in terms of data objects that have type tags , that is, data objects that include explicit information about how they are to be processed. We will also discuss data-directed programming, a powerful and convenient implementation strategy for additively assembling systems with generic operations. -

    We begin with the simple complex-number example. We will see how type tags and data-directed style enable us to design separate rectangular and polar representations for complex numbers while maintaining the notion of an abstract ``complex-number'' data object. We will accomplish this by defining arithmetic procedures for complex numbers (add-complex, sub-complex, mul-complex, and div-complex) in terms of generic selectors that access parts of a complex number independent of how the number is represented. The resulting complex-number system, as shown in Figure 2-19, contains two different kinds of abstraction barriers. The ``horizontal'' abstraction barriers play the same role as the ones in Figure 2-1. They isolate ``higher-level'' operations from ``lower-level'' representations. In addition, there is a ``vertical'' barrier that gives us the ability to separately design and install alternative representations. +

    We begin with the simple complex-number example. We will see how type tags and data-directed style enable us to design separate rectangular and polar representations for complex numbers while maintaining the notion of an abstract ``complex-number'' data object. We will accomplish this by defining arithmetic procedures for complex numbers (add-complex, sub-complex, mul-complex, and div-complex) in terms of generic selectors that access parts of a complex number independent of how the number is represented. The resulting complex-number system, as shown in Figure 2-19, contains two different kinds of abstraction barriers. The ``horizontal'' abstraction barriers play the same role as the ones in Figure 2-1. They isolate ``higher-level'' operations from ``lower-level'' representations. In addition, there is a ``vertical'' barrier that gives us the ability to separately design and install alternative representations. @@ -33,12 +33,11 @@

    Multiple Representations for Abstract Data

    Figure 2.19: Data-abstraction barriers in the complex-number system.

    -

    In section 2.5 we will show how to use type tags and data-directed style to develop a generic arithmetic package. This provides procedures (add, mul, and so on) that can be used to manipulate all sorts of ``numbers''and can be easily extended when a new kind of number is needed. In section 2.5.3, we'll show how to use generic arithmetic in a system that performs symbolic algebra. +

    In section 2.5 we will show how to use type tags and data-directed style to develop a generic arithmetic package. This provides procedures (add, mul, and so on) that can be used to manipulate all sorts of ``numbers''and can be easily extended when a new kind of number is needed. In section 2.5.3, we'll show how to use generic arithmetic in a system that performs symbolic algebra. - -

    2.4.1 Representations for Complex Numbers

    +

    2.4.1 Representations for Complex Numbers

    -

    We will develop a system that performs arithmetic operations on complex numbers as a simple but unrealistic example of a program that uses generic operations. We begin by discussing two plausible representations for complex numbers as ordered pairs: rectangular form (real part and imaginary part) and polar form (magnitude and angle).43 Section 2.4.2 will show how both representations can be made to coexist in a single system through the use of type tags and generic operations. +

    We will develop a system that performs arithmetic operations on complex numbers as a simple but unrealistic example of a program that uses generic operations. We begin by discussing two plausible representations for complex numbers as ordered pairs: rectangular form (real part and imaginary part) and polar form (magnitude and angle).43 Section 2.4.2 will show how both representations can be made to coexist in a single system through the use of type tags and generic operations.

    Like rational numbers, complex numbers are naturally represented as ordered pairs. The set of complex numbers can be thought of as a two-dimensional space with two orthogonal axes, the "real" axis and the "imaginary" axis. (See Figure 2-20.) From this point of view, the complex number $z = x + iy$ (where $i^2 = - 1$) can be thought of as the point in the plane whose real coordinate is $x$ and whose imaginary coordinate is $y$. Addition of complex numbers reduces in this representation to addition of coordinates: @@ -65,7 +64,7 @@

    2.4.1 Representations for Complex Numbers

    Figure 2.20: Complex numbers as points in the plane.

    -

    To design such a system, we can follow the same data-abstraction strategy we followed in designing the rational-number package in section 2.1.1. Assume that the operations on complex numbers are implemented in terms of four selectors: real-part, imag-part, magnitude, and angle. Also assume that we have two procedures for constructing complex numbers: make-from-real-imag returns a complex number with specified real and imaginary parts, and make-from-mag-ang returns a complex number with specified magnitude and angle. These procedures have the property that, for any complex number z, both +

    To design such a system, we can follow the same data-abstraction strategy we followed in designing the rational-number package in section 2.1.1. Assume that the operations on complex numbers are implemented in terms of four selectors: real-part, imag-part, magnitude, and angle. Also assume that we have two procedures for constructing complex numbers: make-from-real-imag returns a complex number with specified real and imaginary parts, and make-from-mag-ang returns a complex number with specified magnitude and angle. These procedures have the property that, for any complex number z, both

    (make-from-real-imag (real-part z) (imag-part z)) @@ -83,7 +82,7 @@

    2.4.1 Representations for Complex Numbers

    C.no_output_prompt("scheme-reconstruct-magnitude-angle"); -

    produce complex numbers that are equal to z. +

    produce complex numbers that are equal to z.

    Using these constructors and selectors, we can implement arithmetic on complex numbers using the ``abstract data'' specified by the constructors and selectors, just as we did for rational numbers in section 2.1.1. As shown in the formulas above, we can add and subtract complex numbers in terms of real and imaginary parts while multiplying and dividing complex numbers in terms of magnitudes and angles: @@ -117,7 +116,7 @@

    2.4.1 Representations for Complex Numbers

    y &= r \sin A \qquad \qquad A=\arctan{y,x} \end{align} -

    which relate the real and imaginary parts (x, y) to the magnitude and the angle (r, A).44 Ben's representation is therefore given by the following selectors and constructors: +

    which relate the real and imaginary parts (x, y) to the magnitude and the angle (r, A).44 Ben's representation is therefore given by the following selectors and constructors:

    (define (real-part z) (car z)) @@ -162,16 +161,15 @@

    2.4.1 Representations for Complex Numbers

    C.prompt("scheme-define-polar-selectors"); -

    The discipline of data abstraction ensures that the same implementation of add-complex, sub-complex, mul-complex, and div-complex will work with either Ben's representation or Alyssa's representation. +

    The discipline of data abstraction ensures that the same implementation of add-complex, sub-complex, mul-complex, and div-complex will work with either Ben's representation or Alyssa's representation. - -

    2.4.2 Tagged data

    +

    2.4.2 Tagged data

    One way to view data abstraction is as an application of the "principle of least commitment." In implementing the complex-number system in section 2.4.1, we can use either Ben's rectangular representation or Alyssa's polar representation. The abstraction barrier formed by the selectors and constructors permits us to defer to the last possible moment the choice of a concrete representation for our data objects and thus retain maximum flexibility in our system design. -

    The principle of least commitment can be carried to even further extremes. If we desire, we can maintain the ambiguity of representation even after we have designed the selectors and constructors, and elect to use both Ben's representation and Alyssa's representation. If both representations are included in a single system, however, we will need some way to distinguish data in polar form from data in rectangular form. Otherwise, if we were asked, for instance, to find the magnitude of the pair (3,4), we wouldn't know whether to answer $5$ (interpreting the number in rectangular form) or $3$ (interpreting the number in polar form). A straightforward way to accomplish this distinction is to include a type tag ---the symbol rectangular or polar---as part of each complex number. Then when we need to manipulate a complex number we can use the tag to decide which selector to apply. +

    The principle of least commitment can be carried to even further extremes. If we desire, we can maintain the ambiguity of representation even after we have designed the selectors and constructors, and elect to use both Ben's representation and Alyssa's representation. If both representations are included in a single system, however, we will need some way to distinguish data in polar form from data in rectangular form. Otherwise, if we were asked, for instance, to find the magnitude of the pair (3,4), we wouldn't know whether to answer $5$ (interpreting the number in rectangular form) or $3$ (interpreting the number in polar form). A straightforward way to accomplish this distinction is to include a type tag ---the symbol rectangular or polar---as part of each complex number. Then when we need to manipulate a complex number we can use the tag to decide which selector to apply. -

    In order to manipulate tagged data, we will assume that we have procedures type-tag and contents that extract from a data object the tag and the actual contents (the polar or rectangular coordinates, in the case of a complex number). We will also postulate a procedure attach-tag that takes a tag and contents and produces a tagged data object. A straightforward way to implement this is to use ordinary list structure: +

    In order to manipulate tagged data, we will assume that we have procedures type-tag and contents that extract from a data object the tag and the actual contents (the polar or rectangular coordinates, in the case of a complex number). We will also postulate a procedure attach-tag that takes a tag and contents and produces a tagged data object. A straightforward way to implement this is to use ordinary list structure:

    (define (attach-tag type-tag contents) @@ -191,7 +189,7 @@

    2.4.2 Tagged data

    C.prompt("scheme-define-type-tag"); -

    Using these procedures, we can define predicates rectangular? and polar?, which recognize polar and rectangular numbers, respectively: +

    Using these procedures, we can define predicates rectangular? and polar?, which recognize polar and rectangular numbers, respectively:

    (define (rectangular? z) @@ -204,7 +202,7 @@

    2.4.2 Tagged data

    C.prompt("scheme-define-recognise-rectangular-polar-tags", ["scheme-define-type-tag"]); -

    With type tags, Ben and Alyssa can now modify their code so that their two different representations can coexist in the same system. Whenever Ben constructs a complex number, he tags it as rectangular. Whenever Alyssa constructs a complex number, she tags it as polar. In addition, Ben and Alyssa must make sure that the names of their procedures do not conflict. One way to do this is for Ben to append the suffix rectangular to the name of each of his representation procedures and for Alyssa to append polar to the names of hers. Here is Ben's revised rectangular representation from section 2.4.1: +

    With type tags, Ben and Alyssa can now modify their code so that their two different representations can coexist in the same system. Whenever Ben constructs a complex number, he tags it as rectangular. Whenever Alyssa constructs a complex number, she tags it as polar. In addition, Ben and Alyssa must make sure that the names of their procedures do not conflict. One way to do this is for Ben to append the suffix rectangular to the name of each of his representation procedures and for Alyssa to append polar to the names of hers. Here is Ben's revised rectangular representation from section 2.4.1:

    (define (real-part-rectangular z) (car z)) @@ -255,7 +253,7 @@

    2.4.2 Tagged data

    C.prompt("scheme-define-polar-tagged-selectors", ["scheme-define-type-tag"]); -

    Each generic selector is implemented as a procedure that checks the tag of its argument and calls the appropriate procedure for handling data of that type. For example, to obtain the real part of a complex number, real-part examines the tag to determine whether to use Ben's real-part-rectangular or Alyssa's real-part-polar. In either case, we use contents to extract the bare, untagged datum and send this to the rectangular or polar procedure as required: +

    Each generic selector is implemented as a procedure that checks the tag of its argument and calls the appropriate procedure for handling data of that type. For example, to obtain the real part of a complex number, real-part examines the tag to determine whether to use Ben's real-part-rectangular or Alyssa's real-part-polar. In either case, we use contents to extract the bare, untagged datum and send this to the rectangular or polar procedure as required:

    (define (real-part z) @@ -290,7 +288,7 @@

    2.4.2 Tagged data

    C.prompt("scheme-define-tagged-complex-selectors", ["scheme-define-recognise-rectangular-polar-tags", "scheme-define-rectangular-tagged-selectors", "scheme-define-polar-tagged-selectors"]); -

    To implement the complex-number arithmetic operations, we can use the same procedures add-complex, sub-complex, mul-complex, and div-complex from section 2.4.1, because the selectors they call are generic, and so will work with either representation. For example, the procedure add-complex is still +

    To implement the complex-number arithmetic operations, we can use the same procedures add-complex, sub-complex, mul-complex, and div-complex from section 2.4.1, because the selectors they call are generic, and so will work with either representation. For example, the procedure add-complex is still

    (define (add-complex z1 z2) @@ -326,18 +324,17 @@

    2.4.2 Tagged data

    The resulting complex-number system has the structure shown in Figure 2-21. The system has been decomposed into three relatively independent parts: the complex-number-arithmetic operations, Alyssa's polar implementation, and Ben's rectangular implementation. The polar and rectangular implementations could have been written by Ben and Alyssa working separately, and both of these can be used as underlying representations by a third programmer implementing the complex-arithmetic procedures in terms of the abstract constructor/selector interface. -

    Since each data object is tagged with its type, the selectors operate on the data in a generic manner. That is, each selector is defined to have a behavior that depends upon the particular type of data it is applied to. Notice the general mechanism for interfacing the separate representations: Within a given representation implementation (say, Alyssa's polar package) a complex number is an untyped pair (magnitude, angle). When a generic selector operates on a number of polar type, it strips off the tag and passes the contents on to Alyssa's code. Conversely, when Alyssa constructs a number for general use, she tags it with a type so that it can be appropriately recognized by the higher-level procedures. This discipline of stripping off and attaching tags as data objects are passed from level to level can be an important organizational strategy, as we shall see in section 2.5. +

    Since each data object is tagged with its type, the selectors operate on the data in a generic manner. That is, each selector is defined to have a behavior that depends upon the particular type of data it is applied to. Notice the general mechanism for interfacing the separate representations: Within a given representation implementation (say, Alyssa's polar package) a complex number is an untyped pair (magnitude, angle). When a generic selector operates on a number of polar type, it strips off the tag and passes the contents on to Alyssa's code. Conversely, when Alyssa constructs a number for general use, she tags it with a type so that it can be appropriately recognized by the higher-level procedures. This discipline of stripping off and attaching tags as data objects are passed from level to level can be an important organizational strategy, as we shall see in section 2.5. - -

    2.4.3 Data-Directed Programming and Additivity

    +

    2.4.3 Data-Directed Programming and Additivity

    -

    The general strategy of checking the type of a datum and calling an appropriate procedure is called dispatching on type . This is a powerful strategy for obtaining modularity in system design. Oh the other hand, implementing the dispatch as in section 2.4.2 has two significant weaknesses. One weakness is that the generic interface procedures (real-part, imag-part, magnitude, and angle) must know about all the different representations. For instance, suppose we wanted to incorporate a new representation for complex numbers into our complex-number system. We would need to identify this new representation with a type, and then add a clause to each of the generic interface procedures to check for the new type and apply the appropriate selector for that representation. +

    The general strategy of checking the type of a datum and calling an appropriate procedure is called dispatching on type . This is a powerful strategy for obtaining modularity in system design. Oh the other hand, implementing the dispatch as in section 2.4.2 has two significant weaknesses. One weakness is that the generic interface procedures (real-part, imag-part, magnitude, and angle) must know about all the different representations. For instance, suppose we wanted to incorporate a new representation for complex numbers into our complex-number system. We would need to identify this new representation with a type, and then add a clause to each of the generic interface procedures to check for the new type and apply the appropriate selector for that representation.

    Another weakness of the technique is that even though the individual representations can be designed separately, we must guarantee that no two procedures in the entire system have the same name. This is why Ben and Alyssa had to change the names of their original procedures from section 2.4.1. -

    The issue underlying both of these weaknesses is that the technique for implementing generic interfaces is not additive . The person implementing the generic selector procedures must modify those procedures each time a new representation is installed, and the people interfacing the individual representations must modify their code to avoid name conflicts. In each of these cases, the changes that must be made to the code are straightforward, but they must be made nonetheless, and this is a source of inconvenience and error. This is not much of a problem for the complex-number system as it stands, but suppose there were not two but hundreds of different representations for complex numbers. And suppose that there were many generic selectors to be maintained in the abstract-data interface. Suppose, in fact, that no one programmer knew all the interface procedures or all the representations. The problem is real and must be addressed in such programs as large-scale data-base-management systems. +

    The issue underlying both of these weaknesses is that the technique for implementing generic interfaces is not additive . The person implementing the generic selector procedures must modify those procedures each time a new representation is installed, and the people interfacing the individual representations must modify their code to avoid name conflicts. In each of these cases, the changes that must be made to the code are straightforward, but they must be made nonetheless, and this is a source of inconvenience and error. This is not much of a problem for the complex-number system as it stands, but suppose there were not two but hundreds of different representations for complex numbers. And suppose that there were many generic selectors to be maintained in the abstract-data interface. Suppose, in fact, that no one programmer knew all the interface procedures or all the representations. The problem is real and must be addressed in such programs as large-scale data-base-management systems. -

    What we need is a means for modularizing the system design even further. This is provided by the programming technique known as data-directed programming . To understand how data-directed programming works, begin with the observation that whenever we deal with a set of generic operations that are common to a set of different types we are, in effect, dealing with a two-dimensional table that contains the possible operations on one axis and the possible types on the other axis. The entries in the table are the procedures that implement each operation for each type of argument presented. In the complex-number system developed in the previous section, the correspondence between operation name, data type, and actual procedure was spread out among the various conditional clauses in the generic interface procedures. But the same information could have been organized in a table, as shown in Figure 2-22. +

    What we need is a means for modularizing the system design even further. This is provided by the programming technique known as data-directed programming . To understand how data-directed programming works, begin with the observation that whenever we deal with a set of generic operations that are common to a set of different types we are, in effect, dealing with a two-dimensional table that contains the possible operations on one axis and the possible types on the other axis. The entries in the table are the procedures that implement each operation for each type of argument presented. In the complex-number system developed in the previous section, the correspondence between operation name, data type, and actual procedure was spread out among the various conditional clauses in the generic interface procedures. But the same information could have been organized in a table, as shown in Figure 2-22.

    Data-directed programming is the technique of designing programs to work with such a table directly. Previously, we implemented the mechanism that interfaces the complex-arithmetic code with the two representation packages as a set of procedures that each perform an explicit dispatch on type. Here we will implement the interface as a single procedure that looks up the combination of the operation name and argument type in the table to find the correct procedure to apply, and then applies it to the contents of the argument. If we do this, then to add a new representation package to the system we need not change any existing procedures; we need only add new entries to the table. @@ -346,22 +343,22 @@

    2.4.3 Data-Directed Programming and Additivity

    Figure 2.22: Table of operations for the complex-number system.

    -

    To implement this plan, assume that we have two procedures, put and get, for manipulating the operation-and-type table: +

    To implement this plan, assume that we have two procedures, put and get, for manipulating the operation-and-type table:

    • -

      (put <op> <type> <item>) installs the <item> in the table, indexed by the <op> and the <type>. +

      (put <op> <type> <item>) installs the <item> in the table, indexed by the <op> and the <type>.

    • -

      (get <op> <type>) looks up the <op>, <type> entry in the table and returns the item found there. If no item is found, get returns false. +

      (get <op> <type>) looks up the <op>, <type> entry in the table and returns the item found there. If no item is found, get returns false.

    -

    For now, we can assume that put and get are included in our language. In Chapter 3 (section 3.3.3, Exercise 3-24) we will see how to implement these and other operations for manipulating tables. +

    For now, we can assume that put and get are included in our language. In Chapter 3 (section 3.3.3, Exercise 3-24) we will see how to implement these and other operations for manipulating tables. -

    Here is how data-directed programming can be used in the complex-number system. Ben, who developed the rectangular representation, implements his code just as he did originally. He defines a collection of procedures, or a package , and interfaces these to the rest of the system by adding entries to the table that tell the system how to operate on rectangular numbers. This is accomplished by calling the following procedure: +

    Here is how data-directed programming can be used in the complex-number system. Ben, who developed the rectangular representation, implements his code just as he did originally. He defines a collection of procedures, or a package , and interfaces these to the rest of the system by adding entries to the table that tell the system how to operate on rectangular numbers. This is accomplished by calling the following procedure:

    (define (install-rectangular-package) @@ -393,7 +390,7 @@

    2.4.3 Data-Directed Programming and Additivity

    C.prompt("scheme-define-install-rectangular-package"); -

    Notice that the internal procedures here are the same procedures from section 2.4.1 that Ben wrote when he was working in isolation. No changes are necessary in order to interface them to the rest of the system. Moreover, since these procedure definitions are internal to the installation procedure, Ben needn't worry about name conflicts with other procedures outside the rectangular package. To interface these to the rest of the system, Ben installs his real-part procedure under the operation name real-part and the type (rectangular), and similarly for the other selectors.45 The interface also defines the constructors to be used by the external system.46 These are identical to Ben's internally defined constructors, except that they attach the tag. +

    Notice that the internal procedures here are the same procedures from section 2.4.1 that Ben wrote when he was working in isolation. No changes are necessary in order to interface them to the rest of the system. Moreover, since these procedure definitions are internal to the installation procedure, Ben needn't worry about name conflicts with other procedures outside the rectangular package. To interface these to the rest of the system, Ben installs his real-part procedure under the operation name real-part and the type (rectangular), and similarly for the other selectors.45 The interface also defines the constructors to be used by the external system.46 These are identical to Ben's internally defined constructors, except that they attach the tag.

    Alyssa's polar package is analogous: @@ -427,9 +424,9 @@

    2.4.3 Data-Directed Programming and Additivity

    C.prompt("scheme-define-install-polar-package"); -

    Even though Ben and Alyssa both still use their original procedures defined with the same names as each other's (e.g., real-part), these definitions are now internal to different procedures (see section 1.1.8), so there is no name conflict. +

    Even though Ben and Alyssa both still use their original procedures defined with the same names as each other's (e.g., real-part), these definitions are now internal to different procedures (see section 1.1.8), so there is no name conflict. -

    The complex-arithmetic selectors access the table by means of a general ``operation'' procedure called apply-generic, which applies a generic operation to some arguments. apply-generic looks in the table under the name of the operation and the types of the arguments and applies the resulting procedure if one is present:47 +

    The complex-arithmetic selectors access the table by means of a general ``operation'' procedure called apply-generic, which applies a generic operation to some arguments. apply-generic looks in the table under the name of the operation and the types of the arguments and applies the resulting procedure if one is present:47

    (define (apply-generic op . args) @@ -445,7 +442,7 @@

    2.4.3 Data-Directed Programming and Additivity

    C.prompt("scheme-defne-apply-generic"); -

    Using apply-generic, we can define our generic selectors as follows: +

    Using apply-generic, we can define our generic selectors as follows:

    (define (real-part z) (apply-generic 'real-part z)) @@ -474,7 +471,7 @@

    2.4.3 Data-Directed Programming and Additivity

    -

    +

    Exercise 2.73: Section 2.3.2 described a program that performs symbolic differentiation:

    @@ -490,14 +487,14 @@

    2.4.3 Data-Directed Programming and Additivity

    (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) - + <more rules can be added here> (else (error "unknown expression type -- DERIV" exp))))
    -

    We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the "type tag" of the datum is the algebraic operator symbol (such as +) and the operation being performed is deriv. We can transform this program into data-directed style by rewriting the basic derivative procedure as +

    We can regard this program as performing a dispatch on the type of the expression to be differentiated. In this situation the "type tag" of the datum is the algebraic operator symbol (such as +) and the operation being performed is deriv. We can transform this program into data-directed style by rewriting the basic derivative procedure as

    (define (deriv exp var) @@ -517,7 +514,7 @@

    2.4.3 Data-Directed Programming and Additivity

    • -

      Explain what was done above. Why can't we assimilate the predicates number? and same-variable? into the data-directed dispatch? +

      Explain what was done above. Why can't we assimilate the predicates number? and same-variable? into the data-directed dispatch?

    • @@ -529,7 +526,7 @@

      2.4.3 Data-Directed Programming and Additivity

    • -

      In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in deriv looked like +

      In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in deriv looked like

      ((get (operator exp) 'deriv) (operands exp) var) @@ -545,22 +542,22 @@

      2.4.3 Data-Directed Programming and Additivity

      -

      +

      Exercise 2.74: Insatiable Enterprises, Inc., is a highly decentralized conglomerate company consisting of a large number of independent divisions located all over the world. The company's computer facilities have just been interconnected by means of a clever network-interfacing scheme that makes the entire network appear to any user to be a single computer. Insatiable's president, in her first attempt to exploit the ability of the network to extract administrative information from division files, is dismayed to discover that, although all the division files have been implemented as data structures in Scheme, the particular data structure used varies from division to division. A meeting of division managers is hastily called to search for a strategy to integrate the files that will satisfy headquarters' needs while preserving the existing autonomy of the divisions. -

      Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division's personnel records consist of a single file, which contains a set of records keyed on employees' names. The structure of the set varies from division to division. Furthermore, each employee's record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as address and salary. In particular: +

      Show how such a strategy can be implemented with data-directed programming. As an example, suppose that each division's personnel records consist of a single file, which contains a set of records keyed on employees' names. The structure of the set varies from division to division. Furthermore, each employee's record is itself a set (structured differently from division to division) that contains information keyed under identifiers such as address and salary. In particular:

      • -

        Implement for headquarters a get-record procedure that retrieves a specified employee's record from a specified personnel file. The procedure should be applicable to any division's file. Explain how the individual divisions' files should be structured. In particular, what type information must be supplied? +

        Implement for headquarters a get-record procedure that retrieves a specified employee's record from a specified personnel file. The procedure should be applicable to any division's file. Explain how the individual divisions' files should be structured. In particular, what type information must be supplied?

      • -

        Implement for headquarters a get-salary procedure that returns the salary information from a given employee's record from any division's personnel file. How should the record be structured in order to make this operation work? +

        Implement for headquarters a get-salary procedure that returns the salary information from a given employee's record from any division's personnel file. How should the record be structured in order to make this operation work?

      • -

        Implement for headquarters a find-employee-record procedure. This should search all the divisions' files for the record of a given employee and return the record. Assume that this procedure takes as arguments an employee's name and a list of all the divisions' files. +

        Implement for headquarters a find-employee-record procedure. This should search all the divisions' files for the record of a given employee and return the record. Assume that this procedure takes as arguments an employee's name and a list of all the divisions' files.

      • @@ -573,7 +570,7 @@

        Message passing

        The key idea of data-directed programming is to handle generic operations in programs by dealing explicitly with operation-and-type tables, such as the table in Figure 2-22. The style of programming we used in section 2.4.2 organized the required dispatching on type by having each operation take care of its own dispatching. In effect, this decomposes the operation-and-type table into rows, with each generic operation procedure representing a row of the table. -

        An alternative implementation strategy is to decompose the table into columns and, instead of using "intelligent operations" that dispatch on data types, to work with "intelligent data objects" that dispatch on operation names. We can do this by arranging things so that a data object, such as a rectangular number, is represented as a procedure that takes as input the required operation name and performs the operation indicated. In such a discipline, make-from-real-imag could be written as +

        An alternative implementation strategy is to decompose the table into columns and, instead of using "intelligent operations" that dispatch on data types, to work with "intelligent data objects" that dispatch on operation names. We can do this by arranging things so that a data object, such as a rectangular number, is represented as a procedure that takes as input the required operation name and performs the operation indicated. In such a discipline, make-from-real-imag could be written as

        (define (make-from-real-imag x y) @@ -591,7 +588,7 @@

        Message passing

        C.prompt("scheme-define-make-intelligent-complex-from-real-imag"); -

        The corresponding apply-generic procedure, which applies a generic operation to an argument, now simply feeds the operation's name to the data object and lets the object do the work:48 +

        The corresponding apply-generic procedure, which applies a generic operation to an argument, now simply feeds the operation's name to the data object and lets the object do the work:48

        (define (apply-generic op arg) (arg op)) @@ -600,17 +597,17 @@

        Message passing

        C.prompt("scheme-define-message-passing-apply-generic"); -

        Note that the value returned by make-from-real-imag is a procedure---the internal dispatch procedure. This is the procedure that is invoked when apply-generic requests an operation to be performed. +

        Note that the value returned by make-from-real-imag is a procedure---the internal dispatch procedure. This is the procedure that is invoked when apply-generic requests an operation to be performed. -

        This style of programming is called message passing . The name comes from the image that a data object is an entity that receives the requested operation name as a ``message.'' We have already seen an example of message passing in section 2.1.3, where we saw how cons, car, and cdr could be defined with no data objects but only procedures. Here we see that message passing is not a mathematical trick but a useful technique for organizing systems with generic operations. In the remainder of this chapter we will continue to use data-directed programming, rather than message passing, to discuss generic arithmetic operations. In Chapter 3 we will return to message passing, and we will see that it can be a powerful tool for structuring simulation programs. +

        This style of programming is called message passing . The name comes from the image that a data object is an entity that receives the requested operation name as a ``message.'' We have already seen an example of message passing in section 2.1.3, where we saw how cons, car, and cdr could be defined with no data objects but only procedures. Here we see that message passing is not a mathematical trick but a useful technique for organizing systems with generic operations. In the remainder of this chapter we will continue to use data-directed programming, rather than message passing, to discuss generic arithmetic operations. In Chapter 3 we will return to message passing, and we will see that it can be a powerful tool for structuring simulation programs. -

        -

        Exercise 2.75: Implement the constructor make-from-mag-ang in message-passing style. This procedure should be analogous to the make-from-real-imag procedure given above. +

        +

        Exercise 2.75: Implement the constructor make-from-mag-ang in message-passing style. This procedure should be analogous to the make-from-real-imag procedure given above.

        -

        +

        Exercise 2.76: As a large system with generic operations evolves, new types of data objects or new operations may be needed. For each of the three strategies---generic operations with explicit dispatch, data-directed style, and message-passing-style---describe the changes that must be made to a system in order to add new types or new operations. Which organization would be most appropriate for a system in which new types must often be added? Which would be most appropriate for a system in which new operations must often be added?

        @@ @@ -634,9 +631,9 @@

        Message passing

        -

        47 Apply-generic uses the dotted-tail notation described in Exercise 2-20, because different generic operations may take different numbers of arguments. In apply-generic, op has as its value the first argument to apply-generic and args has as its value a list of the remaining arguments. +

        47 Apply-generic uses the dotted-tail notation described in Exercise 2-20, because different generic operations may take different numbers of arguments. In apply-generic, op has as its value the first argument to apply-generic and args has as its value a list of the remaining arguments. -

        Apply-generic also uses the primitive procedure apply, which takes two arguments, a procedure and a list. Apply applies the procedure, using the elements in the list as arguments. For example, +

        Apply-generic also uses the primitive procedure apply, which takes two arguments, a procedure and a list. Apply applies the procedure, using the elements in the list as arguments. For example,

        (apply + (list 1 2 3 4)) diff --git a/content/2-5-generic.content.html b/content/2-5-generic.content.html index 37847aa..cd10a4f 100644 --- a/content/2-5-generic.content.html +++ b/content/2-5-generic.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

        @@ -14,19 +14,18 @@

        Systems with Generic Operations


        -

        In the previous section, we saw how to design systems in which data objects can be represented in more than one way. The key idea is to link the code that specifies the data operations to the several representations by means of generic interface procedures. Now we will see how to use this same idea not only to define operations that are generic over different representations but also to define operations that are generic over different kinds of arguments. We have already seen several different packages of arithmetic operations: the primitive arithmetic (+, -, *, /) built into our language, the rational-number arithmetic (add-rat, sub-rat, mul-rat, div-rat) of section 2.1.1, and the complex-number arithmetic that we implemented in section 2.4.3. We will now use data-directed techniques to construct a package of arithmetic operations that incorporates all the arithmetic packages we have already constructed. +

        In the previous section, we saw how to design systems in which data objects can be represented in more than one way. The key idea is to link the code that specifies the data operations to the several representations by means of generic interface procedures. Now we will see how to use this same idea not only to define operations that are generic over different representations but also to define operations that are generic over different kinds of arguments. We have already seen several different packages of arithmetic operations: the primitive arithmetic (+, -, *, /) built into our language, the rational-number arithmetic (add-rat, sub-rat, mul-rat, div-rat) of section 2.1.1, and the complex-number arithmetic that we implemented in section 2.4.3. We will now use data-directed techniques to construct a package of arithmetic operations that incorporates all the arithmetic packages we have already constructed. -

        Figure 2-23 shows the structure of the system we shall build. Notice the abstraction barriers. From the perspective of someone using ``numbers,'' there is a single procedure add that operates on whatever numbers are supplied. Add is part of a generic interface that allows the separate ordinary-arithmetic, rational-arithmetic, and complex-arithmetic packages to be accessed uniformly by programs that use numbers. Any individual arithmetic package (such as the complex package) may itself be accessed through generic procedures (such as add-complex) that combine packages designed for different representations (such as rectangular and polar). Moreover, the structure of the system is additive, so that one can design the individual arithmetic packages separately and combine them to produce a generic arithmetic system. +

        Figure 2-23 shows the structure of the system we shall build. Notice the abstraction barriers. From the perspective of someone using ``numbers,'' there is a single procedure add that operates on whatever numbers are supplied. Add is part of a generic interface that allows the separate ordinary-arithmetic, rational-arithmetic, and complex-arithmetic packages to be accessed uniformly by programs that use numbers. Any individual arithmetic package (such as the complex package) may itself be accessed through generic procedures (such as add-complex) that combine packages designed for different representations (such as rectangular and polar). Moreover, the structure of the system is additive, so that one can design the individual arithmetic packages separately and combine them to produce a generic arithmetic system.

        Figure 2.23: Generic arithmetic system.

        - -

        2.5.1 Generic Arithmetic Operations

        +

        2.5.1 Generic Arithmetic Operations

        -

        The task of designing generic arithmetic operations is analogous to that of designing the generic complex-number operations. We would like, for instance, to have a generic addition procedure add that acts like ordinary primitive addition + on ordinary numbers, like add-rat on rational numbers, and like add-complex on complex numbers. We can implement add, and the other generic arithmetic operations, by following the same strategy we used in section 2.4.3 to implement the generic selectors for complex numbers. We will attach a type tag to each kind of number and cause the generic procedure to dispatch to an appropriate package according to the data type of its arguments. +

        The task of designing generic arithmetic operations is analogous to that of designing the generic complex-number operations. We would like, for instance, to have a generic addition procedure add that acts like ordinary primitive addition + on ordinary numbers, like add-rat on rational numbers, and like add-complex on complex numbers. We can implement add, and the other generic arithmetic operations, by following the same strategy we used in section 2.4.3 to implement the generic selectors for complex numbers. We will attach a type tag to each kind of number and cause the generic procedure to dispatch to an appropriate package according to the data type of its arguments.

        The generic arithmetic procedures are defined as follows: @@ -40,7 +39,7 @@

        2.5.1 Generic Arithmetic Operations

        C.prompt("scheme-define-generic-arith"); -

        We begin by installing a package for handling ordinary numbers, that is, the primitive numbers of our language. We will tag these with the symbol scheme-number. The arithmetic operations in this package are the primitive arithmetic procedures (so there is no need to define extra procedures to handle the untagged numbers). Since these operations each take two arguments, they are installed in the table keyed by the list (scheme-number scheme-number): +

        We begin by installing a package for handling ordinary numbers, that is, the primitive numbers of our language. We will tag these with the symbol scheme-number. The arithmetic operations in this package are the primitive arithmetic procedures (so there is no need to define extra procedures to handle the untagged numbers). Since these operations each take two arguments, they are installed in the table keyed by the list (scheme-number scheme-number):

        (define (install-scheme-number-package) @@ -119,7 +118,7 @@

        2.5.1 Generic Arithmetic Operations

        C.prompt("scheme-define-install-rational-package"); -

        We can install a similar package to handle complex numbers, using the tag complex. In creating the package, we extract from the table the operations make-from-real-imag and make-from-mag-ang that were defined by the rectangular and polar packages. Additivity permits us to use, as the internal operations, the same add-complex, sub-complex, mul-complex, and div-complex procedures from section 2.4.1. +

        We can install a similar package to handle complex numbers, using the tag complex. In creating the package, we extract from the table the operations make-from-real-imag and make-from-mag-ang that were defined by the rectangular and polar packages. Additivity permits us to use, as the internal operations, the same add-complex, sub-complex, mul-complex, and div-complex procedures from section 2.4.1.

        (define (install-complex-package) @@ -176,17 +175,17 @@

        2.5.1 Generic Arithmetic Operations

        C.prompt("scheme-define-make-complex"); -

        What we have here is a two-level tag system. A typical complex number, such as 3 + 4i in rectangular form, would be represented as shown in Figure 2-24. The outer tag (complex) is used to direct the number to the complex package. Once within the complex package, the next tag (rectangular) is used to direct the number to the rectangular package. In a large and complicated system there might be many levels, each interfaced with the next by means of generic operations. As a data object is passed ``downward,'' the outer tag that is used to direct it to the appropriate package is stripped off (by applying contents) and the next level of tag (if any) becomes visible to be used for further dispatching. +

        What we have here is a two-level tag system. A typical complex number, such as 3 + 4i in rectangular form, would be represented as shown in Figure 2-24. The outer tag (complex) is used to direct the number to the complex package. Once within the complex package, the next tag (rectangular) is used to direct the number to the rectangular package. In a large and complicated system there might be many levels, each interfaced with the next by means of generic operations. As a data object is passed ``downward,'' the outer tag that is used to direct it to the appropriate package is stripped off (by applying contents) and the next level of tag (if any) becomes visible to be used for further dispatching.

        Figure 2.24: Representation of 3 + 4i in rectangular form.

        -

        In the above packages, we used add-rat, add-complex, and the other arithmetic procedures exactly as originally written. Once these definitions are internal to different installation procedures, however, they no longer need names that are distinct from each other: we could simply name them add, sub, mul, and div in both packages. +

        In the above packages, we used add-rat, add-complex, and the other arithmetic procedures exactly as originally written. Once these definitions are internal to different installation procedures, however, they no longer need names that are distinct from each other: we could simply name them add, sub, mul, and div in both packages. -

        -

        Exercise 2.77: Louis Reasoner tries to evaluate the expression (magnitude z) where z is the object shown in Figure 2-24. To his surprise, instead of the answer $5$ he gets an error message from apply-generic, saying there is no method for the operation magnitude on the types (complex). He shows this interaction to Alyssa P. Hacker, who says "The problem is that the complex-number selectors were never defined for complex numbers, just for polar and rectangular numbers. All you have to do to make this work is add the following to the complex package:" +

        +

        Exercise 2.77: Louis Reasoner tries to evaluate the expression (magnitude z) where z is the object shown in Figure 2-24. To his surprise, instead of the answer $5$ he gets an error message from apply-generic, saying there is no method for the operation magnitude on the types (complex). He shows this interaction to Alyssa P. Hacker, who says "The problem is that the complex-number selectors were never defined for complex numbers, just for polar and rectangular numbers. All you have to do to make this work is add the following to the complex package:"

        (put 'real-part '(complex) real-part) @@ -198,33 +197,32 @@

        2.5.1 Generic Arithmetic Operations

        C.no_output_frozen_prompt("scheme-complex-selectors-for-complex"); -

        Describe in detail why this works. As an example, trace through all the procedures called in evaluating the expression (magnitude z) where z is the object shown in Figure 2-24. In particular, how many times is apply-generic invoked? What procedure is dispatched to in each case? +

        Describe in detail why this works. As an example, trace through all the procedures called in evaluating the expression (magnitude z) where z is the object shown in Figure 2-24. In particular, how many times is apply-generic invoked? What procedure is dispatched to in each case?

        -

        -

        Exercise 2.78: The internal procedures in the scheme-number package are essentially nothing more than calls to the primitive procedures +, -, etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as symbol? and number? determine whether data objects have particular types. Modify the definitions of type-tag, contents, and attach-tag from section 2.4.2 so that our generic system takes advantage of Scheme's internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as Scheme numbers rather than as pairs whose car is the symbol scheme-number. +

        +

        Exercise 2.78: The internal procedures in the scheme-number package are essentially nothing more than calls to the primitive procedures +, -, etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as symbol? and number? determine whether data objects have particular types. Modify the definitions of type-tag, contents, and attach-tag from section 2.4.2 so that our generic system takes advantage of Scheme's internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as Scheme numbers rather than as pairs whose car is the symbol scheme-number.

        -

        -

        Exercise 2.79: Define a generic equality predicate equ? that tests the equality of two numbers, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. +

        +

        Exercise 2.79: Define a generic equality predicate equ? that tests the equality of two numbers, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers.

        -

        -

        Exercise 2.80: Define a generic predicate =zero? that tests if its argument is zero, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers. +

        +

        Exercise 2.80: Define a generic predicate =zero? that tests if its argument is zero, and install it in the generic arithmetic package. This operation should work for ordinary numbers, rational numbers, and complex numbers.

        - -

        2.5.2 Combining Data of Different Types

        +

        2.5.2 Combining Data of Different Types

        We have seen how to define a unified arithmetic system that encompasses ordinary numbers, complex numbers, rational numbers, and any other type of number we might decide to invent, but we have ignored an important issue. The operations we have defined so far treat the different data types as being completely independent. Thus, there are separate packages for adding, say, two ordinary numbers, or two complex numbers. What we have not yet considered is the fact that it is meaningful to define operations that cross the type boundaries, such as the addition of a complex number to an ordinary number. We have gone to great pains to introduce barriers between parts of our programs so that they can be developed and understood separately. We would like to introduce the cross-type operations in some carefully controlled way, so that we can support them without seriously violating our module boundaries. -

        One way to handle cross-type operations is to design a different procedure for each possible combination of types for which the operation is valid. For example, we could extend the complex-number package so that it provides a procedure for adding complex numbers to ordinary numbers and installs this in the table using the tag (complex scheme-number)49 +

        One way to handle cross-type operations is to design a different procedure for each possible combination of types for which the operation is valid. For example, we could extend the complex-number package so that it provides a procedure for adding complex numbers to ordinary numbers and installs this in the table using the tag (complex scheme-number)49

        ;; to be included in the complex package @@ -243,7 +241,7 @@

        2.5.2 Combining Data of Different Types

        Coercion

        -

        In the general situation of completely unrelated operations acting on completely unrelated types, implementing explicit cross-type operations, cumbersome though it may be, is the best that one can hope for. Fortunately, we can usually do better by taking advantage of additional structure that may be latent in our type system. Often the different data types are not completely independent, and there may be ways by which objects of one type may be viewed as being of another type. This process is called coercion . For example, if we are asked to arithmetically combine an ordinary number with a complex number, we can view the ordinary number as a complex number whose imaginary part is zero. This transforms the problem to that of combining two complex numbers, which can be handled in the ordinary way by the complex-arithmetic package. +

        In the general situation of completely unrelated operations acting on completely unrelated types, implementing explicit cross-type operations, cumbersome though it may be, is the best that one can hope for. Fortunately, we can usually do better by taking advantage of additional structure that may be latent in our type system. Often the different data types are not completely independent, and there may be ways by which objects of one type may be viewed as being of another type. This process is called coercion . For example, if we are asked to arithmetically combine an ordinary number with a complex number, we can view the ordinary number as a complex number whose imaginary part is zero. This transforms the problem to that of combining two complex numbers, which can be handled in the ordinary way by the complex-arithmetic package.

        In general, we can implement this idea by designing coercion procedures that transform an object of one type into an equivalent object of another type. Here is a typical coercion procedure, which transforms a given ordinary number to a complex number with that real part and zero imaginary part: @@ -264,9 +262,9 @@

        Coercion

        C.prompt("scheme-put-coercion"); -

        (We assume that there are put-coercion and get-coercion procedures available for manipulating this table.) Generally some of the slots in the table will be empty, because it is not generally possible to coerce an arbitrary data object of each type into all other types. For example, there is no way to coerce an arbitrary complex number to an ordinary number, so there will be no general complex->scheme-number procedure included in the table. +

        (We assume that there are put-coercion and get-coercion procedures available for manipulating this table.) Generally some of the slots in the table will be empty, because it is not generally possible to coerce an arbitrary data object of each type into all other types. For example, there is no way to coerce an arbitrary complex number to an ordinary number, so there will be no general complex->scheme-number procedure included in the table. -

        Once the coercion table has been set up, we can handle coercion in a uniform manner by modifying the apply-generic procedure of section 2.4.3. When asked to apply an operation, we first check whether the operation is defined for the arguments' types, just as before. If so, we dispatch to the procedure found in the operation-and-type table. Otherwise, we try coercion. For simplicity, we consider only the case where there are two arguments.50 We check the coercion table to see if objects of the first type can be coerced to the second type. If so, we coerce the first argument and try the operation again. If objects of the first type cannot in general be coerced to the second type, we try the coercion the other way around to see if there is a way to coerce the second argument to the type of the first argument. Finally, if there is no known way to coerce either type to the other type, we give up. Here is the procedure: +

        Once the coercion table has been set up, we can handle coercion in a uniform manner by modifying the apply-generic procedure of section 2.4.3. When asked to apply an operation, we first check whether the operation is defined for the arguments' types, just as before. If so, we dispatch to the procedure found in the operation-and-type table. Otherwise, we try coercion. For simplicity, we consider only the case where there are two arguments.50 We check the coercion table to see if objects of the first type can be coerced to the second type. If so, we coerce the first argument and try the operation again. If objects of the first type cannot in general be coerced to the second type, we try the coercion the other way around to see if there is a way to coerce the second argument to the type of the first argument. Finally, if there is no known way to coerce either type to the other type, we give up. Here is the procedure:

        (define (apply-generic op . args) @@ -295,24 +293,24 @@

        Coercion

        C.prompt("scheme-define-apply-generic"); -

        This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly n^2 procedures for a system with n types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation.51 What we are counting on here is the fact that the appropriate transformation between types depends only on the types themselves, not on the operation to be applied. +

        This coercion scheme has many advantages over the method of defining explicit cross-type operations, as outlined above. Although we still need to write coercion procedures to relate the types (possibly n^2 procedures for a system with n types), we need to write only one procedure for each pair of types rather than a different procedure for each collection of types and each generic operation.51 What we are counting on here is the fact that the appropriate transformation between types depends only on the types themselves, not on the operation to be applied.

        On the other hand, there may be applications for which our coercion scheme is not general enough. Even when neither of the objects to be combined can be converted to the type of the other it may still be possible to perform the operation by converting both objects to a third type. In order to deal with such complexity and still preserve modularity in our programs, it is usually necessary to build systems that take advantage of still further structure in the relations among types, as we discuss next.

        Hierarchies of types

        -

        The coercion scheme presented above relied on the existence of natural relations between pairs of types. Often there is more ``global'' structure in how the different types relate to each other. For instance, suppose we are building a generic arithmetic system to handle integers, rational numbers, real numbers, and complex numbers. In such a system, it is quite natural to regard an integer as a special kind of rational number, which is in turn a special kind of real number, which is in turn a special kind of complex number. What we actually have is a so-called hierarchy of types , in which, for example, integers are a subtype of rational numbers (i.e., any operation that can be applied to a rational number can automatically be applied to an integer). Conversely, we say that rational numbers form a supertype of integers. The particular hierarchy we have here is of a very simple kind, in which each type has at most one supertype and at most one subtype. Such a structure, called a tower , is illustrated in Figure 2-25. +

        The coercion scheme presented above relied on the existence of natural relations between pairs of types. Often there is more ``global'' structure in how the different types relate to each other. For instance, suppose we are building a generic arithmetic system to handle integers, rational numbers, real numbers, and complex numbers. In such a system, it is quite natural to regard an integer as a special kind of rational number, which is in turn a special kind of real number, which is in turn a special kind of complex number. What we actually have is a so-called hierarchy of types , in which, for example, integers are a subtype of rational numbers (i.e., any operation that can be applied to a rational number can automatically be applied to an integer). Conversely, we say that rational numbers form a supertype of integers. The particular hierarchy we have here is of a very simple kind, in which each type has at most one supertype and at most one subtype. Such a structure, called a tower , is illustrated in Figure 2-25.

        Figure 2.25: A tower of types.

        -

        If we have a tower structure, then we can greatly simplify the problem of adding a new type to the hierarchy, for we need only specify how the new type is embedded in the next supertype above it and how it is the supertype of the type below it. For example, if we want to add an integer to a complex number, we need not explicitly define a special coercion procedure integer->complex. Instead, we define how an integer can be transformed into a rational number, how a rational number is transformed into a real number, and how a real number is transformed into a complex number. We then allow the system to transform the integer into a complex number through these steps and then add the two complex numbers. +

        If we have a tower structure, then we can greatly simplify the problem of adding a new type to the hierarchy, for we need only specify how the new type is embedded in the next supertype above it and how it is the supertype of the type below it. For example, if we want to add an integer to a complex number, we need not explicitly define a special coercion procedure integer->complex. Instead, we define how an integer can be transformed into a rational number, how a rational number is transformed into a real number, and how a real number is transformed into a complex number. We then allow the system to transform the integer into a complex number through these steps and then add the two complex numbers. -

        We can redesign our apply-generic procedure in the following way: For each type, we need to supply a raise procedure, which ``raises'' objects of that type one level in the tower. Then when the system is required to operate on objects of different types it can successively raise the lower types until all the objects are at the same level in the tower. (Exercise 2-83 and Exercise 2-84 concern the details of implementing such a strategy.) +

        We can redesign our apply-generic procedure in the following way: For each type, we need to supply a raise procedure, which ``raises'' objects of that type one level in the tower. Then when the system is required to operate on objects of different types it can successively raise the lower types until all the objects are at the same level in the tower. (Exercise 2-83 and Exercise 2-84 concern the details of implementing such a strategy.) -

        Another advantage of a tower is that we can easily implement the notion that every type ``inherits'' all operations defined on a supertype. For instance, if we do not supply a special procedure for finding the real part of an integer, we should nevertheless expect that real-part will be defined for integers by virtue of the fact that integers are a subtype of complex numbers. In a tower, we can arrange for this to happen in a uniform way by modifying apply-generic. If the required operation is not directly defined for the type of the object given, we raise the object to its supertype and try again. We thus crawl up the tower, transforming our argument as we go, until we either find a level at which the desired operation can be performed or hit the top (in which case we give up). +

        Another advantage of a tower is that we can easily implement the notion that every type ``inherits'' all operations defined on a supertype. For instance, if we do not supply a special procedure for finding the real part of an integer, we should nevertheless expect that real-part will be defined for integers by virtue of the fact that integers are a subtype of complex numbers. In a tower, we can arrange for this to happen in a uniform way by modifying apply-generic. If the required operation is not directly defined for the type of the object given, we raise the object to its supertype and try again. We thus crawl up the tower, transforming our argument as we go, until we either find a level at which the desired operation can be performed or hit the top (in which case we give up).

        Yet another advantage of a tower over a more general hierarchy is that it gives us a simple way to ``lower'' a data object to the simplest representation. For example, if we add 2 + 3i to 4 - 3i, it would be nice to obtain the answer as the integer 6 rather than as the complex number 6 + 0i. Exercise 2-85 discusses a way to implement such a lowering operation. (The trick is that we need a general way to distinguish those objects that can be lowered, such as 6 + 0i, from those that cannot, such as 6 + 2i.) @@ -323,10 +321,10 @@

        Hierarchies of types

        Inadequacies of hierarchies

        -

        If the data types in our system can be naturally arranged in a tower, this greatly simplifies the problems of dealing with generic operations on different types, as we have seen. Unfortunately, this is usually not the case. Figure 2-26 illustrates a more complex arrangement of mixed types, this one showing relations among different types of geometric figures. We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for instance, are both subtypes of polygons. In addition, a type may have more than one supertype. For example, an isosceles right triangle may be regarded either as an isosceles triangle or as a right triangle. This multiple-supertypes issue is particularly thorny, since it means that there is no unique way to ``raise'' a type in the hierarchy. Finding the ``correct'' supertype in which to apply an operation to an object may involve considerable searching through the entire type network on the part of a procedure such as apply-generic. Since there generally are multiple subtypes for a type, there is a similar problem in coercing a value ``down'' the type hierarchy. Dealing with large numbers of interrelated types while still preserving modularity in the design of large systems is very difficult, and is an area of much current research.52 +

        If the data types in our system can be naturally arranged in a tower, this greatly simplifies the problems of dealing with generic operations on different types, as we have seen. Unfortunately, this is usually not the case. Figure 2-26 illustrates a more complex arrangement of mixed types, this one showing relations among different types of geometric figures. We see that, in general, a type may have more than one subtype. Triangles and quadrilaterals, for instance, are both subtypes of polygons. In addition, a type may have more than one supertype. For example, an isosceles right triangle may be regarded either as an isosceles triangle or as a right triangle. This multiple-supertypes issue is particularly thorny, since it means that there is no unique way to ``raise'' a type in the hierarchy. Finding the ``correct'' supertype in which to apply an operation to an object may involve considerable searching through the entire type network on the part of a procedure such as apply-generic. Since there generally are multiple subtypes for a type, there is a similar problem in coercing a value ``down'' the type hierarchy. Dealing with large numbers of interrelated types while still preserving modularity in the design of large systems is very difficult, and is an area of much current research.52 -

        -

        Exercise 2.81: Louis Reasoner has noticed that apply-generic may try to coerce the arguments to each other's type even if they already have the same type. Therefore, he reasons, we need to put procedures in the coercion table to coerce arguments of each type to their own type. For example, in addition to the scheme-number->complex coercion shown above, he would do: +

        +

        Exercise 2.81: Louis Reasoner has noticed that apply-generic may try to coerce the arguments to each other's type even if they already have the same type. Therefore, he reasons, we need to put procedures in the coercion table to coerce arguments of each type to their own type. For example, in addition to the scheme-number->complex coercion shown above, he would do:

        (define (scheme-number->scheme-number n) n) @@ -341,7 +339,7 @@

        Inadequacies of hierarchies

        • -

          With Louis's coercion procedures installed, what happens if apply-generic is called with two arguments of type scheme-number or two arguments of type complex for an operation that is not found in the table for those types? For example, assume that we've defined a generic exponentiation operation: +

          With Louis's coercion procedures installed, what happens if apply-generic is called with two arguments of type scheme-number or two arguments of type complex for an operation that is not found in the table for those types? For example, assume that we've defined a generic exponentiation operation:

          (define (exp x y) (apply-generic 'exp x y)) @@ -355,53 +353,52 @@

          Inadequacies of hierarchies

          ;; following added to Scheme-number package (put 'exp '(scheme-number scheme-number) - (lambda (x y) (tag (expt x y)))) ; using primitive expt + (lambda (x y) (tag (expt x y)))) ; using primitive expt
          -

          What happens if we call exp with two complex numbers as arguments? +

          What happens if we call exp with two complex numbers as arguments?

        • -

          Is Louis correct that something had to be done about coercion with arguments of the same type, or does apply-generic work correctly as is?

        • Modify apply-generic so that it doesn't try coercion if the two arguments have the same type. +

          Is Louis correct that something had to be done about coercion with arguments of the same type, or does apply-generic work correctly as is?

        • Modify apply-generic so that it doesn't try coercion if the two arguments have the same type.

        -

        +

        Exercise 2.82: Show how to generalize -apply-generic to handle coercion in the general case of multiple arguments. One strategy is to attempt to coerce all the arguments to the type of the first argument, then to the type of the second argument, and so on. Give an example of a situation where this strategy (and likewise the two-argument version given above) is not sufficiently general. (Hint: Consider the case where there are some suitable mixed-type operations present in the table that will not be tried.) +apply-generic to handle coercion in the general case of multiple arguments. One strategy is to attempt to coerce all the arguments to the type of the first argument, then to the type of the second argument, and so on. Give an example of a situation where this strategy (and likewise the two-argument version given above) is not sufficiently general. (Hint: Consider the case where there are some suitable mixed-type operations present in the table that will not be tried.)

        -

        -

        Exercise 2.83: Suppose you are designing a generic arithmetic system for dealing with the tower of types shown in Figure 2-25: integer, rational, real, complex. For each type (except complex), design a procedure that raises objects of that type one level in the tower. Show how to install a generic raise operation that will work for each type (except complex). +

        +

        Exercise 2.83: Suppose you are designing a generic arithmetic system for dealing with the tower of types shown in Figure 2-25: integer, rational, real, complex. For each type (except complex), design a procedure that raises objects of that type one level in the tower. Show how to install a generic raise operation that will work for each type (except complex).

        -

        -

        Exercise 2.84: Using the raise operation of Exercise 2-83, modify the apply-generic procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is "compatible" with the rest of the system and will not lead to problems in adding new levels to the tower. +

        +

        Exercise 2.84: Using the raise operation of Exercise 2-83, modify the apply-generic procedure so that it coerces its arguments to have the same type by the method of successive raising, as discussed in this section. You will need to devise a way to test which of two types is higher in the tower. Do this in a manner that is "compatible" with the rest of the system and will not lead to problems in adding new levels to the tower.

        -

        -

        Exercise 2.85: This section mentioned a method for "simplifying" a data object by lowering it in the tower of types as far as possible. Design a procedure drop that accomplishes this for the tower described in Exercise 2-83. The key is to decide, in some general way, whether an object can be lowered. For example, the complex number $1.5 + 0i$ can be lowered as far as real, the complex number $1 + 0i$ can be lowered as far as integer, and the complex number $2 + 3i$ cannot be lowered at all. Here is a plan for determining whether an object can be lowered: Begin by defining a generic operation project that "pushes" an object down in the tower. For example, projecting a complex number would involve throwing away the imaginary part. Then a number can be dropped if, when we project it and raise the result back to the type we started with, we end up with something equal to what we started with. Show how to implement this idea in detail, by writing a drop procedure that drops an object as far as possible. You will need to design the various projection operations53 and install project as a generic operation in the system. You will also need to make use of a generic equality predicate, such as described in Exercise 2-79. Finally, use drop to rewrite apply-generic from Exercise 2-84 so that it "simplifies" its answers. +

        +

        Exercise 2.85: This section mentioned a method for "simplifying" a data object by lowering it in the tower of types as far as possible. Design a procedure drop that accomplishes this for the tower described in Exercise 2-83. The key is to decide, in some general way, whether an object can be lowered. For example, the complex number $1.5 + 0i$ can be lowered as far as real, the complex number $1 + 0i$ can be lowered as far as integer, and the complex number $2 + 3i$ cannot be lowered at all. Here is a plan for determining whether an object can be lowered: Begin by defining a generic operation project that "pushes" an object down in the tower. For example, projecting a complex number would involve throwing away the imaginary part. Then a number can be dropped if, when we project it and raise the result back to the type we started with, we end up with something equal to what we started with. Show how to implement this idea in detail, by writing a drop procedure that drops an object as far as possible. You will need to design the various projection operations53 and install project as a generic operation in the system. You will also need to make use of a generic equality predicate, such as described in Exercise 2-79. Finally, use drop to rewrite apply-generic from Exercise 2-84 so that it "simplifies" its answers.

        -

        -

        Exercise 2.86: Suppose we want to handle complex numbers whose real parts, imaginary parts, magnitudes, and angles can be either ordinary numbers, rational numbers, or other numbers we might wish to add to the system. Describe and implement the changes to the system needed to accommodate this. You will have to define operations such as sine and cosine that are generic over ordinary numbers and rational numbers. +

        +

        Exercise 2.86: Suppose we want to handle complex numbers whose real parts, imaginary parts, magnitudes, and angles can be either ordinary numbers, rational numbers, or other numbers we might wish to add to the system. Describe and implement the changes to the system needed to accommodate this. You will have to define operations such as sine and cosine that are generic over ordinary numbers and rational numbers.

        - -

        2.5.3 Example: Symbolic Algebra

        +

        2.5.3 Example: Symbolic Algebra

        The manipulation of symbolic algebraic expressions is a complex process that illustrates many of the hardest problems that occur in the design of large-scale systems. An algebraic expression, in general, can be viewed as a hierarchical structure, a tree of operators applied to operands. We can construct algebraic expressions by starting with a set of primitive objects, such as constants and variables, and combining these by means of algebraic operators, such as addition and multiplication. As in other languages, we form abstractions that enable us to refer to compound objects in simple terms. Typical abstractions in symbolic algebra are ideas such as linear combination, polynomial, rational function, or trigonometric function. We can regard these as compound ``types,'' which are often useful for directing the processing of expressions. For example, we could describe the expression @@ -415,7 +412,7 @@

        2.5.3 Example: Symbolic Algebra

        Arithmetic on polynomials

        -

        Our first task in designing a system for performing arithmetic on polynomials is to decide just what a polynomial is. Polynomials are normally defined relative to certain variables (the indeterminates of the polynomial). For simplicity, we will restrict ourselves to polynomials having just one indeterminate (univariate polynomials).54 below.} We will define a polynomial to be a sum of terms, each of which is either a coefficient, a power of the indeterminate, or a product of a coefficient and a power of the indeterminate. A coefficient is defined as an algebraic expression that is not dependent upon the indeterminate of the polynomial. For example, +

        Our first task in designing a system for performing arithmetic on polynomials is to decide just what a polynomial is. Polynomials are normally defined relative to certain variables (the indeterminates of the polynomial). For simplicity, we will restrict ourselves to polynomials having just one indeterminate (univariate polynomials).54 below.} We will define a polynomial to be a sum of terms, each of which is either a coefficient, a power of the indeterminate, or a product of a coefficient and a power of the indeterminate. A coefficient is defined as an algebraic expression that is not dependent upon the indeterminate of the polynomial. For example, $$ 5x^2 + 3x + 7 @@ -429,11 +426,11 @@

        Arithmetic on polynomials

        is a polynomial in x whose coefficients are polynomials in y. -

        Already we are skirting some thorny issues. Is the first of these polynomials the same as the polynomial $5y^2 + 3y + 7$, or not? A reasonable answer might be "yes, if we are considering a polynomial purely as a mathematical function, but no, if we are considering a polynomial to be a syntactic form." The second polynomial is algebraically equivalent to a polynomial in $y$ whose coefficients are polynomials in x. Should our system recognize this, or not? Furthermore, there are other ways to represent a polynomial – for example, as a product of factors, or (for a univariate polynomial) as the set of roots, or as a listing of the values of the polynomial at a specified set of points.55 We can finesse these questions by deciding that in our algebraic-manipulation system a "polynomial" will be a particular syntactic form, not its underlying mathematical meaning. +

        Already we are skirting some thorny issues. Is the first of these polynomials the same as the polynomial $5y^2 + 3y + 7$, or not? A reasonable answer might be "yes, if we are considering a polynomial purely as a mathematical function, but no, if we are considering a polynomial to be a syntactic form." The second polynomial is algebraically equivalent to a polynomial in $y$ whose coefficients are polynomials in x. Should our system recognize this, or not? Furthermore, there are other ways to represent a polynomial – for example, as a product of factors, or (for a univariate polynomial) as the set of roots, or as a listing of the values of the polynomial at a specified set of points.55 We can finesse these questions by deciding that in our algebraic-manipulation system a "polynomial" will be a particular syntactic form, not its underlying mathematical meaning.

        Now we must consider how to go about doing arithmetic on polynomials. In this simple system, we will consider only addition and multiplication. Moreover, we will insist that two polynomials to be combined must have the same indeterminate. -

        We will approach the design of our system by following the familiar discipline of data abstraction. We will represent polynomials using a data structure called a poly , which consists of a variable and a collection of terms. We assume that we have selectors variable and term-list that extract those parts from a poly and a constructor make-poly that assembles a poly from a given variable and a term list. A variable will be just a symbol, so we can use the same-variable? procedure of section 2.3.2 to compare variables. The following procedures define addition and multiplication of polys: +

        We will approach the design of our system by following the familiar discipline of data abstraction. We will represent polynomials using a data structure called a poly , which consists of a variable and a collection of terms. We assume that we have selectors variable and term-list that extract those parts from a poly and a constructor make-poly that assembles a poly from a given variable and a term list. A variable will be just a symbol, so we can use the same-variable? procedure of section 2.3.2 to compare variables. The following procedures define addition and multiplication of polys:

        (define (add-poly p1 p2) @@ -456,7 +453,7 @@

        Arithmetic on polynomials

        C.prompt("scheme-define-add-mul-poly"); -

        To incorporate polynomials into our generic arithmetic system, we need to supply them with type tags. We'll use the tag polynomial, and install appropriate operations on tagged polynomials in the operation table. We'll embed all our code in an installation procedure for the polynomial package, similar to the ones in section 2.5.1: +

        To incorporate polynomials into our generic arithmetic system, we need to supply them with type tags. We'll use the tag polynomial, and install appropriate operations on tagged polynomials in the operation table. We'll embed all our code in an installation procedure for the polynomial package, similar to the ones in section 2.5.1:

        (define (install-polynomial-package) @@ -466,7 +463,7 @@

        Arithmetic on polynomials

        (cons variable term-list)) (define (variable p) (car p)) (define (term-list p) (cdr p)) - ;procedures same-variable? and variable? from section 2.3.2 + ;procedures same-variable? and variable? from section 2.3.2 ;; representation of terms and term lists ;procedures adjoin-term ... coeff from text below @@ -495,9 +492,9 @@

        Arithmetic on polynomials

        Polynomial addition is performed termwise. Terms of the same order (i.e., with the same power of the indeterminate) must be combined. This is done by forming a new term of the same order whose coefficient is the sum of the coefficients of the addends. Terms in one addend for which there are no terms of the same order in the other addend are simply accumulated into the sum polynomial being constructed. -

        In order to manipulate term lists, we will assume that we have a constructor the-empty-termlist that returns an empty term list and a constructor adjoin-term that adjoins a new term to a term list. We will also assume that we have a predicate empty-termlist? that tells if a given term list is empty, a selector first-term that extracts the highest-order term from a term list, and a selector rest-terms that returns all but the highest-order term. To manipulate terms, we will suppose that we have a constructor make-term that constructs a term with given order and coefficient, and selectors order and coeff that return, respectively, the order and the coefficient of the term. These operations allow us to consider both terms and term lists as data abstractions, whose concrete representations we can worry about separately. +

        In order to manipulate term lists, we will assume that we have a constructor the-empty-termlist that returns an empty term list and a constructor adjoin-term that adjoins a new term to a term list. We will also assume that we have a predicate empty-termlist? that tells if a given term list is empty, a selector first-term that extracts the highest-order term from a term list, and a selector rest-terms that returns all but the highest-order term. To manipulate terms, we will suppose that we have a constructor make-term that constructs a term with given order and coefficient, and selectors order and coeff that return, respectively, the order and the coefficient of the term. These operations allow us to consider both terms and term lists as data abstractions, whose concrete representations we can worry about separately. -

        Here is the procedure that constructs the term list for the sum of two polynomials:56 +

        Here is the procedure that constructs the term list for the sum of two polynomials:56

        (define (add-terms L1 L2) @@ -522,9 +519,9 @@

        Arithmetic on polynomials

        C.prompt("scheme-define-add-terms"); -

        The most important point to note here is that we used the generic addition procedure add to add together the coefficients of the terms being combined. This has powerful consequences, as we will see below. +

        The most important point to note here is that we used the generic addition procedure add to add together the coefficients of the terms being combined. This has powerful consequences, as we will see below. -

        In order to multiply two term lists, we multiply each term of the first list by all the terms of the other list, repeatedly using mul-term-by-all-terms, which multiplies a given term by all terms in a given term list. The resulting term lists (one for each term of the first list) are accumulated into a sum. Multiplying two terms forms a term whose order is the sum of the orders of the factors and whose coefficient is the product of the coefficients of the factors: +

        In order to multiply two term lists, we multiply each term of the first list by all the terms of the other list, repeatedly using mul-term-by-all-terms, which multiplies a given term by all terms in a given term list. The resulting term lists (one for each term of the first list) are accumulated into a sum. Multiplying two terms forms a term whose order is the sum of the orders of the factors and whose coefficient is the product of the coefficients of the factors:

        (define (mul-terms L1 L2) @@ -546,25 +543,25 @@

        Arithmetic on polynomials

        C.prompt("scheme-define-mul-terms"); -

        This is really all there is to polynomial addition and multiplication. Notice that, since we operate on terms using the generic procedures add and mul, our polynomial package is automatically able to handle any type of coefficient that is known about by the generic arithmetic package. If we include a coercion mechanism such as one of those discussed in section 2.5.2, then we also are automatically able to handle operations on polynomials of different coefficient types, such as +

        This is really all there is to polynomial addition and multiplication. Notice that, since we operate on terms using the generic procedures add and mul, our polynomial package is automatically able to handle any type of coefficient that is known about by the generic arithmetic package. If we include a coercion mechanism such as one of those discussed in section 2.5.2, then we also are automatically able to handle operations on polynomials of different coefficient types, such as $$ \left[3x^2 + (2 + 3i)x + 7\right] \cdot \left[ x^4 + \frac{2}{3} x^2 + (5 + 3i) \right] $$ -

        Because we installed the polynomial addition and multiplication procedures add-poly and mul-poly in the generic arithmetic system as the add and mul operations for type polynomial, our system is also automatically able to handle polynomial operations such as +

        Because we installed the polynomial addition and multiplication procedures add-poly and mul-poly in the generic arithmetic system as the add and mul operations for type polynomial, our system is also automatically able to handle polynomial operations such as $$ \left[(y + 1)x^2 + (y^2 + 1)x + (y - 1)\right] \cdot \left[(y - 2)x + (y^3 + 7)\right] $$ -

        The reason is that when the system tries to combine coefficients, it will dispatch through add and mul. Since the coefficients are themselves polynomials (in y), these will be combined using add-poly and mul-poly. The result is a kind of ``data-directed recursion'' in which, for example, a call to mul-poly will result in recursive calls to mul-poly in order to multiply the coefficients. If the coefficients of the coefficients were themselves polynomials (as might be used to represent polynomials in three variables), the data direction would ensure that the system would follow through another level of recursive calls, and so on through as many levels as the structure of the data dictates.57 +

        The reason is that when the system tries to combine coefficients, it will dispatch through add and mul. Since the coefficients are themselves polynomials (in y), these will be combined using add-poly and mul-poly. The result is a kind of ``data-directed recursion'' in which, for example, a call to mul-poly will result in recursive calls to mul-poly in order to multiply the coefficients. If the coefficients of the coefficients were themselves polynomials (as might be used to represent polynomials in three variables), the data direction would ensure that the system would follow through another level of recursive calls, and so on through as many levels as the structure of the data dictates.57

        Representing term lists

        -

        Finally, we must confront the job of implementing a good representation for term lists. A term list is, in effect, a set of coefficients keyed by the order of the term. Hence, any of the methods for representing sets, as discussed in section 2.3.3, can be applied to this task. On the other hand, our procedures add-terms and mul-terms always access term lists sequentially from highest to lowest order. Thus, we will use some kind of ordered list representation. +

        Finally, we must confront the job of implementing a good representation for term lists. A term list is, in effect, a set of coefficients keyed by the order of the term. Hence, any of the methods for representing sets, as discussed in section 2.3.3, can be applied to this task. On the other hand, our procedures add-terms and mul-terms always access term lists sequentially from highest to lowest order. Thus, we will use some kind of ordered list representation. -

        How should we structure the list that represents a term list? One consideration is the ``density'' of the polynomials we intend to manipulate. A polynomial is said to be dense if it has nonzero coefficients in terms of most orders. If it has many zero terms it is said to be sparse . For example, +

        How should we structure the list that represents a term list? One consideration is the ``density'' of the polynomials we intend to manipulate. A polynomial is said to be dense if it has nonzero coefficients in terms of most orders. If it has many zero terms it is said to be sparse . For example, $$ A : x^5 + 2x^4 + 3x^2 - 2x - 5 @@ -578,7 +575,7 @@

        Representing term lists

        is sparse. -

        The term lists of dense polynomials are most efficiently represented as lists of the coefficients. For example, A above would be nicely represented as (1 2 0 3 -2 -5). The order of a term in this representation is the length of the sublist beginning with that term's coefficient, decremented by 1.58 This would be a terrible representation for a sparse polynomial such as B: There would be a giant list of zeros punctuated by a few lonely nonzero terms. A more reasonable representation of the term list of a sparse polynomial is as a list of the nonzero terms, where each term is a list containing the order of the term and the coefficient for that order. In such a scheme, polynomial B is efficiently represented as ((100 1) (2 2) (0 1)). As most polynomial manipulations are performed on sparse polynomials, we will use this method. We will assume that term lists are represented as lists of terms, arranged from highest-order to lowest-order term. Once we have made this decision, implementing the selectors and constructors for terms and term lists is straightforward:59 +

        The term lists of dense polynomials are most efficiently represented as lists of the coefficients. For example, A above would be nicely represented as (1 2 0 3 -2 -5). The order of a term in this representation is the length of the sublist beginning with that term's coefficient, decremented by 1.58 This would be a terrible representation for a sparse polynomial such as B: There would be a giant list of zeros punctuated by a few lonely nonzero terms. A more reasonable representation of the term list of a sparse polynomial is as a list of the nonzero terms, where each term is a list containing the order of the term and the coefficient for that order. In such a scheme, polynomial B is efficiently represented as ((100 1) (2 2) (0 1)). As most polynomial manipulations are performed on sparse polynomials, we will use this method. We will assume that term lists are represented as lists of terms, arranged from highest-order to lowest-order term. Once we have made this decision, implementing the selectors and constructors for terms and term lists is straightforward:59

        (define (adjoin-term term term-list) @@ -600,7 +597,7 @@

        Representing term lists

        -

        where =zero? is as defined in Exercise 2-80. (See also Exercise 2-87 below.) +

        where =zero? is as defined in Exercise 2-80. (See also Exercise 2-87 below.)

        Users of the polynomial package will create (tagged) polynomials by means of the procedure: @@ -612,23 +609,23 @@

        Representing term lists

        C.prompt("scheme-define-make-polynomial"); -
        -

        Exercise 2.87: Install =zero? for polynomials in the generic arithmetic package. This will allow adjoin-term to work for polynomials with coefficients that are themselves polynomials. +

        +

        Exercise 2.87: Install =zero? for polynomials in the generic arithmetic package. This will allow adjoin-term to work for polynomials with coefficients that are themselves polynomials.

        -
        +

        Exercise 2.88: Extend the polynomial system to include subtraction of polynomials. (Hint: You may find it helpful to define a generic negation operation.)

        -
        +

        Exercise 2.89: Define procedures that implement the term-list representation described above as appropriate for dense polynomials.

        -
        +

        Exercise 2.90: Suppose we want to have a polynomial system that is efficient for both sparse and dense polynomials. One way to do this is to allow both kinds of term-list representations in our system. The situation is analogous to the complex-number example of section 2.4, where we allowed both rectangular and polar representations. To do this we must distinguish different types of term lists and make the operations on term lists generic. Redesign the polynomial system to implement this generalization. This is a major effort, not a local change.

        -
        +

        Exercise 2.91: A univariate polynomial can be divided by another one to produce a polynomial quotient and a polynomial remainder. For example,

        @@ -639,9 +636,9 @@ 

        Representing term lists

        Division can be performed via long division. That is, divide the highest-order term of the dividend by the highest-order term of the divisor. The result is the first term of the quotient. Next, multiply the result by the divisor, subtract that from the dividend, and produce the rest of the answer by recursively dividing the difference by the divisor. Stop when the order of the divisor exceeds the order of the dividend and declare the dividend to be the remainder. Also, if the dividend ever becomes zero, return zero as both quotient and remainder. -

        We can design a div-poly procedure on the model of add-poly and mul-poly. The procedure checks to see if the two polys have the same variable. If so, div-poly strips off the variable and passes the problem to div-terms, which performs the division operation on term lists. Div-poly finally reattaches the variable to the result supplied by div-terms. It is convenient to design div-terms to compute both the quotient and the remainder of a division. Div-terms can take two term lists as arguments and return a list of the quotient term list and the remainder term list. +

        We can design a div-poly procedure on the model of add-poly and mul-poly. The procedure checks to see if the two polys have the same variable. If so, div-poly strips off the variable and passes the problem to div-terms, which performs the division operation on term lists. Div-poly finally reattaches the variable to the result supplied by div-terms. It is convenient to design div-terms to compute both the quotient and the remainder of a division. Div-terms can take two term lists as arguments and return a list of the quotient term list and the remainder term list. -

        Complete the following definition of div-terms by filling in the missing expressions. Use this to implement div-poly, which takes two polys as arguments and returns a list of the quotient and remainder polys. +

        Complete the following definition of div-terms by filling in the missing expressions. Use this to implement div-poly, which takes two polys as arguments and returns a list of the quotient and remainder polys.

        (define (div-terms L1 L2) @@ -672,13 +669,13 @@

        Hierarchies of types in symbolic algebra

        It should not be surprising that controlling coercion is a serious problem in the design of large-scale algebraic-manipulation systems. Much of the complexity of such systems is concerned with relationships among diverse types. Indeed, it is fair to say that we do not yet completely understand coercion. In fact, we do not yet completely understand the concept of a data type. Nevertheless, what we know provides us with powerful structuring and modularity principles to support the design of large systems. -

        +

        Exercise 2.92: By imposing an ordering on variables, extend the polynomial package so that addition and multiplication of polynomials works for polynomials in different variables. (This is not easy!)

        Extended exercise: Rational functions

        -

        We can extend our generic arithmetic system to include rational functions . These are ``fractions'' whose numerator and denominator are polynomials, such as +

        We can extend our generic arithmetic system to include rational functions . These are ``fractions'' whose numerator and denominator are polynomials, such as $$ \frac{x+1}{x^3 - 1} @@ -695,8 +692,8 @@

        Extended exercise: Rational functions

        If we modify our rational-arithmetic package so that it uses generic operations, then it will do what we want, except for the problem of reducing fractions to lowest terms. -

        -

        Exercise 2.93: Modify the rational-arithmetic package to use generic operations, but change make-rat so that it does not attempt to reduce fractions to lowest terms. Test your system by calling make-rational on two polynomials to produce a rational function +

        +

        Exercise 2.93: Modify the rational-arithmetic package to use generic operations, but change make-rat so that it does not attempt to reduce fractions to lowest terms. Test your system by calling make-rational on two polynomials to produce a rational function

        (define p1 (make-polynomial 'x '((2 1)(0 1)))) @@ -707,9 +704,9 @@

        Extended exercise: Rational functions

        C.prompt("scheme-test-rational-functions"); -

        Now add rf to itself, using add. You will observe that this addition procedure does not reduce fractions to lowest terms. +

        Now add rf to itself, using add. You will observe that this addition procedure does not reduce fractions to lowest terms. -

        We can reduce polynomial fractions to lowest terms using the same idea we used with integers: modifying make-rat to divide both the numerator and the denominator by their greatest common divisor. The notion of ``greatest common divisor'' makes sense for polynomials. In fact, we can compute the GCD of two polynomials using essentially the same Euclid's Algorithm that works for integers.60 The integer version is +

        We can reduce polynomial fractions to lowest terms using the same idea we used with integers: modifying make-rat to divide both the numerator and the denominator by their greatest common divisor. The notion of ``greatest common divisor'' makes sense for polynomials. In fact, we can compute the GCD of two polynomials using essentially the same Euclid's Algorithm that works for integers.60 The integer version is

        (define (gcd a b) @@ -733,13 +730,13 @@

        Extended exercise: Rational functions

        C.prompt("scheme-define-gcd-terms"); -

        where remainder-terms picks out the remainder component of the list returned by the term-list division operation div-terms that was implemented in Exercise 2-91. +

        where remainder-terms picks out the remainder component of the list returned by the term-list division operation div-terms that was implemented in Exercise 2-91.

        -

        -

        Exercise 2.94: Using div-terms, implement the procedure remainder-terms and use this to define gcd-terms as above. Now write a procedure gcd-poly that computes the polynomial GCD of two polys. (The procedure should signal an error if the two polys are not in the same variable.) Install in the system a generic operation greatest-common-divisor that reduces to gcd-poly for polynomials and to ordinary gcd for ordinary numbers. As a test, try +

        +

        Exercise 2.94: Using div-terms, implement the procedure remainder-terms and use this to define gcd-terms as above. Now write a procedure gcd-poly that computes the polynomial GCD of two polys. (The procedure should signal an error if the two polys are not in the same variable.) Install in the system a generic operation greatest-common-divisor that reduces to gcd-poly for polynomials and to ordinary gcd for ordinary numbers. As a test, try

        (define p1 (make-polynomial 'x '((4 1) (3 -1) (2 -2) (1 2)))) @@ -755,7 +752,7 @@

        Extended exercise: Rational functions

        -

        +

        Exercise 2.95: Define P_1, P_2, and P_3 to be the polynomials $$ @@ -764,21 +761,21 @@

        Extended exercise: Rational functions

        P_3 : 13x + 5 $$ -

        Now define $Q_1$ to be the product of $P_1$ and $P_2$ and $Q_2$ to be the product of $P_1$ and $P_3$, and use greatest-common-divisor (Exercise 2-94) to compute the GCD of $Q_1$ and $Q_2$. Note that the answer is not the same as $P_1$. This example introduces noninteger operations into the computation, causing difficulties with the GCD algorithm.61 To understand what is happening, try tracing gcd-terms while computing the GCD or try performing the division by hand. +

        Now define $Q_1$ to be the product of $P_1$ and $P_2$ and $Q_2$ to be the product of $P_1$ and $P_3$, and use greatest-common-divisor (Exercise 2-94) to compute the GCD of $Q_1$ and $Q_2$. Note that the answer is not the same as $P_1$. This example introduces noninteger operations into the computation, causing difficulties with the GCD algorithm.61 To understand what is happening, try tracing gcd-terms while computing the GCD or try performing the division by hand.

        We can solve the problem exhibited in Exercise 2-95 if we use the following modification of the GCD algorithm (which really works only in the case of polynomials with integer coefficients). Before performing any polynomial division in the GCD computation, we multiply the dividend by an integer constant factor, chosen to guarantee that no fractions will arise during the division process. Our answer will thus differ from the actual GCD by an integer constant factor, but this does not matter in the case of reducing rational functions to lowest terms; the GCD will be used to divide both the numerator and denominator, so the integer constant factor will cancel out. -

        More precisely, if $P$ and $Q$ are polynomials, let $O_1$ be the order of $P$ (i.e., the order of the largest term of $P$) and let $O_2$ be the order of $Q$. Let $c$ be the leading coefficient of $Q$. Then it can be shown that, if we multiply $P$ by the integerizing factor $ c^{1 + O_1 - O_2}$, the resulting polynomial can be divided by $Q$ by using the div-terms algorithm without introducing any fractions. The operation of multiplying the dividend by this constant and then dividing is sometimes called the pseudodivision of $P$ by $Q$. The remainder of the division is called the pseudoremainder .

        +

        More precisely, if $P$ and $Q$ are polynomials, let $O_1$ be the order of $P$ (i.e., the order of the largest term of $P$) and let $O_2$ be the order of $Q$. Let $c$ be the leading coefficient of $Q$. Then it can be shown that, if we multiply $P$ by the integerizing factor $ c^{1 + O_1 - O_2}$, the resulting polynomial can be divided by $Q$ by using the div-terms algorithm without introducing any fractions. The operation of multiplying the dividend by this constant and then dividing is sometimes called the pseudodivision of $P$ by $Q$. The remainder of the division is called the pseudoremainder .

        Exercise 2.96:
        • -

          Implement the procedure pseudoremainder-terms, which is just like remainder-terms except that it multiplies the dividend by the integerizing factor described above before calling div-terms. Modify gcd-terms to use pseudoremainder-terms, and verify that greatest-common-divisor now produces an answer with integer coefficients on the example in Exercise 2-95. +

          Implement the procedure pseudoremainder-terms, which is just like remainder-terms except that it multiplies the dividend by the integerizing factor described above before calling div-terms. Modify gcd-terms to use pseudoremainder-terms, and verify that greatest-common-divisor now produces an answer with integer coefficients on the example in Exercise 2-95.

        • -

          The GCD now has integer coefficients, but they are larger than those of P_1. Modify gcd-terms so that it removes common factors from the coefficients of the answer by dividing all the coefficients by their (integer) greatest common divisor. +

          The GCD now has integer coefficients, but they are larger than those of P_1. Modify gcd-terms so that it removes common factors from the coefficients of the answer by dividing all the coefficients by their (integer) greatest common divisor.

        @@ -786,7 +783,7 @@

        Extended exercise: Rational functions

        • -Compute the GCD of the numerator and denominator, using the version of gcd-terms from Exercise 2-96. +Compute the GCD of the numerator and denominator, using the version of gcd-terms from Exercise 2-96.
        • @@ -803,11 +800,11 @@

          Extended exercise: Rational functions

          Exercise 2.97:
          • -

            Implement this algorithm as a procedure reduce-terms that takes two term lists n and d as arguments and returns a list nn, dd, which are n and d reduced to lowest terms via the algorithm given above. Also write a procedure reduce-poly, analogous to add-poly, that checks to see if the two polys have the same variable. If so, reduce-poly strips off the variable and passes the problem to reduce-terms, then reattaches the variable to the two term lists supplied by reduce-terms. +

            Implement this algorithm as a procedure reduce-terms that takes two term lists n and d as arguments and returns a list nn, dd, which are n and d reduced to lowest terms via the algorithm given above. Also write a procedure reduce-poly, analogous to add-poly, that checks to see if the two polys have the same variable. If so, reduce-poly strips off the variable and passes the problem to reduce-terms, then reattaches the variable to the two term lists supplied by reduce-terms.

          • -

            Define a procedure analogous to reduce-terms that does what the original make-rat did for integers: +

            Define a procedure analogous to reduce-terms that does what the original make-rat did for integers:

            (define (reduce-integers n d) @@ -818,7 +815,7 @@

            Extended exercise: Rational functions

            C.prompt("scheme-define-reduce-integers"); -

            and define reduce as a generic operation that calls apply-generic to dispatch to either reduce-poly (for polynomial arguments) or reduce-integers (for scheme-number arguments). You can now easily make the rational-arithmetic package reduce fractions to lowest terms by having make-rat call reduce before combining the given numerator and denominator to form a rational number. The system now handles rational expressions in either integers or polynomials. To test your program, try the example at the beginning of this extended exercise: +

            and define reduce as a generic operation that calls apply-generic to dispatch to either reduce-poly (for polynomial arguments) or reduce-integers (for scheme-number arguments). You can now easily make the rational-arithmetic package reduce fractions to lowest terms by having make-rat call reduce before combining the given numerator and denominator to form a rational number. The system now handles rational expressions in either integers or polynomials. To test your program, try the example at the beginning of this extended exercise:

            (define p1 (make-polynomial 'x '((1 1)(0 1)))) @@ -837,7 +834,7 @@

            Extended exercise: Rational functions

            See if you get the correct answer, correctly reduced to lowest terms. -

            The GCD computation is at the heart of any system that does operations on rational functions. The algorithm used above, although mathematically straightforward, is extremely slow. The slowness is due partly to the large number of division operations and partly to the enormous size of the intermediate coefficients generated by the pseudodivisions. One of the active areas in the development of algebraic-manipulation systems is the design of better algorithms for computing polynomial GCDs.62 +

            The GCD computation is at the heart of any system that does operations on rational functions. The algorithm used above, although mathematically straightforward, is extremely slow. The slowness is due partly to the large number of division operations and partly to the enormous size of the intermediate coefficients generated by the pseudodivisions. One of the active areas in the development of algebraic-manipulation systems is the design of better algorithms for computing polynomial GCDs.62

        @@ -845,7 +842,7 @@

        Extended exercise: Rational functions

        {{footnotes}}
        -

        49 We also have to supply an almost identical procedure to handle the types (scheme-number complex). +

        49 We also have to supply an almost identical procedure to handle the types (scheme-number complex).

        diff --git a/content/3-0-modularity-objects-and-state.content.html b/content/3-0-modularity-objects-and-state.content.html index 8449594..6ed4004 100644 --- a/content/3-0-modularity-objects-and-state.content.html +++ b/content/3-0-modularity-objects-and-state.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

        Modularity, Objects, and State

        @@ -32,7 +32,7 @@

        Modularity, Objects, and State

        To a large extent, then, the way we organize a large program is dictated by our perception of the system to be modeled. In this chapter we will investigate two prominent organizational strategies arising from two rather different ``world views'' of the structure of systems. The first organizational strategy concentrates on objects, viewing a large system as a collection of distinct objects whose behaviors may change over time. An alternative organizational strategy concentrates on the streams of information that flow in the system, much as an electrical engineer views a signal-processing system. -

        Both the object-based approach and the stream-processing approach raise significant linguistic issues in programming. With objects, we must be concerned with how a computational object can change and yet maintain its identity. This will force us to abandon our old substitution model of computation (section 1.1.5) in favor of a more mechanistic but less theoretically tractable environment model of computation. The difficulties of dealing with objects, change, and identity are a fundamental consequence of the need to grapple with time in our computational models. These difficulties become even greater when we allow the possibility of concurrent execution of programs. The stream approach can be most fully exploited when we decouple simulated time in our model from the order of the events that take place in the computer during evaluation. We will accomplish this using a technique known as delayed evaluation. +

        Both the object-based approach and the stream-processing approach raise significant linguistic issues in programming. With objects, we must be concerned with how a computational object can change and yet maintain its identity. This will force us to abandon our old substitution model of computation (section 1.1.5) in favor of a more mechanistic but less theoretically tractable environment model of computation. The difficulties of dealing with objects, change, and identity are a fundamental consequence of the need to grapple with time in our computational models. These difficulties become even greater when we allow the possibility of concurrent execution of programs. The stream approach can be most fully exploited when we decouple simulated time in our model from the order of the events that take place in the computer during evaluation. We will accomplish this using a technique known as delayed evaluation. {{footnotes}} @@ diff --git a/content/3-1-assignment.content.html b/content/3-1-assignment.content.html index 5440abf..af3de13 100644 --- a/content/3-1-assignment.content.html +++ b/content/3-1-assignment.content.html @@ -3,26 +3,25 @@ @@ {{main_text}} - + - - + +

        Assignment and Local State


        -

        We ordinarily view the world as populated by independent objects, each of which has a state that changes over time. An object is said to "have state" if its behavior is influenced by its history. A bank account, for example, has state in that the answer to the question "Can I withdraw $100?" depends upon the history of deposit and withdrawal transactions. We can characterize an object's state by one or more state variables , which among them maintain enough information about history to determine the object's current behavior. In a simple banking system, we could characterize the state of an account by a current balance rather than by remembering the entire history of account transactions. +

        We ordinarily view the world as populated by independent objects, each of which has a state that changes over time. An object is said to "have state" if its behavior is influenced by its history. A bank account, for example, has state in that the answer to the question "Can I withdraw $100?" depends upon the history of deposit and withdrawal transactions. We can characterize an object's state by one or more state variables , which among them maintain enough information about history to determine the object's current behavior. In a simple banking system, we could characterize the state of an account by a current balance rather than by remembering the entire history of account transactions.

        In a system composed of many objects, the objects are rarely completely independent. Each may influence the states of others through interactions, which serve to couple the state variables of one object to those of other objects. Indeed, the view that a system is composed of separate objects is most useful when the state variables of the system can be grouped into closely coupled subsystems that are only loosely coupled to other subsystems. -

        This view of a system can be a powerful framework for organizing computational models of the system. For such a model to be modular, it should be decomposed into computational objects that model the actual objects in the system. Each computational object must have its own local state variables describing the actual object's state. Since the states of objects in the system being modeled change over time, the state variables of the corresponding computational objects must also change. If we choose to model the flow of time in the system by the elapsed time in the computer, then we must have a way to construct computational objects whose behaviors change as our programs run. In particular, if we wish to model state variables by ordinary symbolic names in the programming language, then the language must provide an assignment operator to enable us to change the value associated with a name. +

        This view of a system can be a powerful framework for organizing computational models of the system. For such a model to be modular, it should be decomposed into computational objects that model the actual objects in the system. Each computational object must have its own local state variables describing the actual object's state. Since the states of objects in the system being modeled change over time, the state variables of the corresponding computational objects must also change. If we choose to model the flow of time in the system by the elapsed time in the computer, then we must have a way to construct computational objects whose behaviors change as our programs run. In particular, if we wish to model state variables by ordinary symbolic names in the programming language, then the language must provide an assignment operator to enable us to change the value associated with a name. - -

        3.1.1 Local State Variables

        +

        3.1.1 Local State Variables

        -

        To illustrate what we mean by having a computational object with time-varying state, let us model the situation of withdrawing money from a bank account. We will do this using a procedure withdraw, which takes as argument an amount to be withdrawn. If there is enough money in the account to accommodate the withdrawal, then withdraw should return the balance remaining after the withdrawal. Otherwise, withdraw should return the message Insufficient funds. For example, if we begin with $100 in the account, we should obtain the following sequence of responses using withdraw: +

        To illustrate what we mean by having a computational object with time-varying state, let us model the situation of withdrawing money from a bank account. We will do this using a procedure withdraw, which takes as argument an amount to be withdrawn. If there is enough money in the account to accommodate the withdrawal, then withdraw should return the balance remaining after the withdrawal. Otherwise, withdraw should return the message Insufficient funds. For example, if we begin with $100 in the account, we should obtain the following sequence of responses using withdraw:

        (withdraw 25) @@ -34,9 +33,9 @@

        3.1.1 Local State Variables

        C.prompt("scheme-withdraw-25-25-60-15", ["scheme-define-withdraw"]); -

        Observe that the expression (withdraw 25), evaluated twice, yields different values. This is a new kind of behavior for a procedure. Until now, all our procedures could be viewed as specifications for computing mathematical functions. A call to a procedure computed the value of the function applied to the given arguments, and two calls to the same procedure with the same arguments always produced the same result.1 +

        Observe that the expression (withdraw 25), evaluated twice, yields different values. This is a new kind of behavior for a procedure. Until now, all our procedures could be viewed as specifications for computing mathematical functions. A call to a procedure computed the value of the function applied to the given arguments, and two calls to the same procedure with the same arguments always produced the same result.1 -

        To implement withdraw, we can use a variable balance to indicate the balance of money in the account and define withdraw as a procedure that accesses balance. The withdraw procedure checks to see if balance is at least as large as the requested amount. If so, withdraw decrements balance by amount and returns the new value of balance. Otherwise, withdraw returns the Insufficient funds message. Here are the definitions of balance and withdraw: +

        To implement withdraw, we can use a variable balance to indicate the balance of money in the account and define withdraw as a procedure that accesses balance. The withdraw procedure checks to see if balance is at least as large as the requested amount. If so, withdraw decrements balance by amount and returns the new value of balance. Otherwise, withdraw returns the Insufficient funds message. Here are the definitions of balance and withdraw:

        (define balance 100) @@ -51,7 +50,7 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-withdraw"); -

        Decrementing balance is accomplished by the expression +

        Decrementing balance is accomplished by the expression

        (set! balance (- balance amount)) @@ -60,7 +59,7 @@

        3.1.1 Local State Variables

        C.no_output_frozen_prompt("scheme-decrement-balance"); -

        This uses the set! special form, whose syntax is +

        This uses the set! special form, whose syntax is

        (set! <name> <new-value>) @@ -69,22 +68,22 @@

        3.1.1 Local State Variables

        C.no_output_frozen_prompt("scheme-set-syntax"); -

        Here is a symbol and is any expression. Set! changes so that its value is the result obtained by evaluating . In the case at hand, we are changing balance so that its new value will be the result of subtracting amount from the previous value of balance.2 +

        Here <name> is a symbol and <new-value> is any expression. Set! changes <name> so that its value is the result obtained by evaluating <new-value>. In the case at hand, we are changing balance so that its new value will be the result of subtracting amount from the previous value of balance.2 -

        withdraw also uses the begin special form to cause two expressions to be evaluated in the case where the if test is true: first decrementing balance and then returning the value of balance. In general, evaluating the expression +

        withdraw also uses the begin special form to cause two expressions to be evaluated in the case where the if test is true: first decrementing balance and then returning the value of balance. In general, evaluating the expression

        -(begin <exp_1> <exp_2> ... <exp_k>> +(begin <exp_1> <exp_2> ... <exp_k>
        -

        causes the expressions <exp_1> through <exp_k> to be evaluated in sequence and the value of the final expression to be returned as the value of the entire begin form.3 +

        causes the expressions <exp_1> through <exp_k> to be evaluated in sequence and the value of the final expression <exp_k> to be returned as the value of the entire begin form.3 -

        Although withdraw works as desired, the variable balance presents a problem. As specified above, balance is a name defined in the global environment and is freely accessible to be examined or modified by any procedure. It would be much better if we could somehow make balance internal to withdraw, so that withdraw would be the only procedure that could access balance directly and any other procedure could access balance only indirectly (through calls to withdraw). This would more accurately model the notion that balance is a local state variable used by withdraw to keep track of the state of the account. +

        Although withdraw works as desired, the variable balance presents a problem. As specified above, balance is a name defined in the global environment and is freely accessible to be examined or modified by any procedure. It would be much better if we could somehow make balance internal to withdraw, so that withdraw would be the only procedure that could access balance directly and any other procedure could access balance only indirectly (through calls to withdraw). This would more accurately model the notion that balance is a local state variable used by withdraw to keep track of the state of the account. -

        We can make balance internal to withdraw by rewriting the definition as follows: +

        We can make balance internal to withdraw by rewriting the definition as follows:

        (define new-withdraw @@ -99,11 +98,11 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-new-withdraw"); -

        What we have done here is use let to establish an environment with a local variable balance, bound to the initial value 100. Within this local environment, we use lambda to create a procedure that takes amount as an argument and behaves like our previous withdraw procedure. This procedure – returned as the result of evaluating the let expression – is new-withdraw, which behaves in precisely the same way as withdraw but whose variable balance is not accessible by any other procedure.4 +

        What we have done here is use let to establish an environment with a local variable balance, bound to the initial value 100. Within this local environment, we use lambda to create a procedure that takes amount as an argument and behaves like our previous withdraw procedure. This procedure – returned as the result of evaluating the let expression – is new-withdraw, which behaves in precisely the same way as withdraw but whose variable balance is not accessible by any other procedure.4 -

        Combining set! with local variables is the general programming technique we will use for constructing computational objects with local state. Unfortunately, using this technique raises a serious problem: When we first introduced procedures, we also introduced the substitution model of evaluation (section 1.1.5) to provide an interpretation of what procedure application means. We said that applying a procedure should be interpreted as evaluating the body of the procedure with the formal parameters replaced by their values. The trouble is that, as soon as we introduce assignment into our language, substitution is no longer an adequate model of procedure application. (We will see why this is so in section 3.1.3.) As a consequence, we technically have at this point no way to understand why the new-withdraw procedure behaves as claimed above. In order to really understand a procedure such as new-withdraw, we will need to develop a new model of procedure application. In section 3.2 we will introduce such a model, together with an explanation of set! and local variables. First, however, we examine some variations on the theme established by new-withdraw. +

        Combining set! with local variables is the general programming technique we will use for constructing computational objects with local state. Unfortunately, using this technique raises a serious problem: When we first introduced procedures, we also introduced the substitution model of evaluation (section 1.1.5) to provide an interpretation of what procedure application means. We said that applying a procedure should be interpreted as evaluating the body of the procedure with the formal parameters replaced by their values. The trouble is that, as soon as we introduce assignment into our language, substitution is no longer an adequate model of procedure application. (We will see why this is so in section 3.1.3.) As a consequence, we technically have at this point no way to understand why the new-withdraw procedure behaves as claimed above. In order to really understand a procedure such as new-withdraw, we will need to develop a new model of procedure application. In section 3.2 we will introduce such a model, together with an explanation of set! and local variables. First, however, we examine some variations on the theme established by new-withdraw. -

        The following procedure, make-withdraw, creates ``withdrawal processors.'' The formal parameter balance in make-withdraw specifies the initial amount of money in the account.5 +

        The following procedure, make-withdraw, creates ``withdrawal processors.'' The formal parameter balance in make-withdraw specifies the initial amount of money in the account.5

        (define (make-withdraw balance) @@ -117,7 +116,7 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-make-withdraw"); -

        make-withdraw can be used as follows to create two objects W1 and W2: +

        make-withdraw can be used as follows to create two objects W1 and W2:

        (define W1 (make-withdraw 100)) @@ -132,7 +131,7 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-W1-W2", ["scheme-define-make-withdraw"]); -

        Observe that W1 and W2 are completely independent objects, each with its own local state variable balance. Withdrawals from one do not affect the other. +

        Observe that W1 and W2 are completely independent objects, each with its own local state variable balance. Withdrawals from one do not affect the other.

        We can also create objects that handle deposits as well as withdrawals, and thus we can represent simple bank accounts. Here is a procedure that returns a ``bank-account object'' with a specified initial balance: @@ -157,9 +156,9 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-make-account"); -

        Each call to make-account sets up an environment with a local state variable balance. Within this environment, make-account defines procedures deposit and withdraw that access balance and an additional procedure dispatch that takes a ``message'' as input and returns one of the two local procedures. The dispatch procedure itself is returned as the value that represents the bank-account object. This is precisely the message-passing style of programming that we saw in section 2.4.3, although here we are using it in conjunction with the ability to modify local variables. +

        Each call to make-account sets up an environment with a local state variable balance. Within this environment, make-account defines procedures deposit and withdraw that access balance and an additional procedure dispatch that takes a ``message'' as input and returns one of the two local procedures. The dispatch procedure itself is returned as the value that represents the bank-account object. This is precisely the message-passing style of programming that we saw in section 2.4.3, although here we are using it in conjunction with the ability to modify local variables. -

        make-account can be used as follows: +

        make-account can be used as follows:

        (define acc (make-account 100)) @@ -173,7 +172,7 @@

        3.1.1 Local State Variables

        C.prompt("scheme-test-make-account", ["scheme-define-make-account"]); -

        Each call to acc returns the locally defined deposit or withdraw procedure, which is then applied to the specified amount. As was the case with make-withdraw, another call to make-account +

        Each call to acc returns the locally defined deposit or withdraw procedure, which is then applied to the specified amount. As was the case with make-withdraw, another call to make-account

        (define acc2 (make-account 100)) @@ -182,10 +181,10 @@

        3.1.1 Local State Variables

        C.prompt("scheme-define-acc-2", ["scheme-define-make-account"]); -

        will produce a completely separate account object, which maintains its own local balance. +

        will produce a completely separate account object, which maintains its own local balance. -

        -

        Exercise 3.1: An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum; for example +

        +

        Exercise 3.1: An accumulator is a procedure that is called repeatedly with a single numeric argument and accumulates its arguments into a sum. Each time it is called, it returns the currently accumulated sum. Write a procedure make-accumulator that generates accumulators, each maintaining an independent sum. The input to make-accumulator should specify the initial value of the sum; for example

        (define A (make-accumulator 5)) @@ -203,8 +202,8 @@

        3.1.1 Local State Variables

        -

        -

        Exercise 3.2: In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, f, that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure: +

        +

        Exercise 3.2: In software-testing applications, it is useful to be able to count the number of times a given procedure is called during the course of a computation. Write a procedure make-monitored that takes as input a procedure, f, that itself takes one input. The result returned by make-monitored is a third procedure, say mf, that keeps track of the number of times it has been called by maintaining an internal counter. If the input to mf is the special symbol how-many-calls?, then mf returns the value of the counter. If the input is the special symbol reset-count, then mf resets the counter to zero. For any other input, mf returns the result of calling f on that input and increments the counter. For instance, we could make a monitored version of the sqrt procedure:

        (define s (make-monitored sqrt)) @@ -222,8 +221,8 @@

        3.1.1 Local State Variables

        -

        -

        Exercise 3.3: Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in +

        +

        Exercise 3.3: Modify the make-account procedure so that it creates password-protected accounts. That is, make-account should take a symbol as an additional argument, as in

        (define acc (make-account 100 'secret-password)) @@ -248,15 +247,14 @@

        3.1.1 Local State Variables

        -

        -

        Exercise 3.4: Modify the make-account procedure of Exercise 3-3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops.

        +
        +

        Exercise 3.4: Modify the make-account procedure of Exercise 3-3 by adding another local state variable so that, if an account is accessed more than seven consecutive times with an incorrect password, it invokes the procedure call-the-cops.

        - -

        3.1.2 The Benefits of Introducing Assignment

        +

        3.1.2 The Benefits of Introducing Assignment

        -

        As we shall see, introducing assignment into our programming language leads us into a thicket of difficult conceptual issues. Nevertheless, viewing systems as collections of objects with local state is a powerful technique for maintaining a modular design. As a simple example, consider the design of a procedure rand that, whenever it is called, returns an integer chosen at random. +

        As we shall see, introducing assignment into our programming language leads us into a thicket of difficult conceptual issues. Nevertheless, viewing systems as collections of objects with local state is a powerful technique for maintaining a modular design. As a simple example, consider the design of a procedure rand that, whenever it is called, returns an integer chosen at random. -

        It is not at all clear what is meant by ``chosen at random.'' What we presumably want is for successive calls to rand to produce a sequence of numbers that has statistical properties of uniform distribution. We will not discuss methods for generating suitable sequences here. Rather, let us assume that we have a procedure rand-update that has the property that if we start with a given number $x_1$ and form +

        It is not at all clear what is meant by ``chosen at random.'' What we presumably want is for successive calls to rand to produce a sequence of numbers that has statistical properties of uniform distribution. We will not discuss methods for generating suitable sequences here. Rather, let us assume that we have a procedure rand-update that has the property that if we start with a given number $x_1$ and form

        x_2 = (rand-update x_1) @@ -266,9 +264,9 @@

        3.1.2 The Benefits of Introducing Assignment

        C.no_output_frozen_prompt("scheme-rand-update"); -

        then the sequence of values $x_1, x_2, x_3, \ldots$ will have the desired statistical properties.6 +

        then the sequence of values $x_1, x_2, x_3, \ldots$ will have the desired statistical properties.6 -

        We can implement rand as a procedure with a local state variable $x$ that is initialized to some fixed value random-init. Each call to rand computes rand-update of the current value of $x$, returns this as the random number, and also stores this as the new value of $x$. +

        We can implement rand as a procedure with a local state variable $x$ that is initialized to some fixed value random-init. Each call to rand computes rand-update of the current value of $x$, returns this as the random number, and also stores this as the new value of $x$.

        (define (rand-update x) @@ -290,11 +288,11 @@

        3.1.2 The Benefits of Introducing Assignment

        C.prompt("scheme-define-rand", ["scheme-hidden-define-rand-update-init"]); -

        Of course, we could generate the same sequence of random numbers without using assignment by simply calling rand-update directly. However, this would mean that any part of our program that used random numbers would have to explicitly remember the current value of x to be passed as an argument to rand-update. To realize what an annoyance this would be, consider using random numbers to implement a technique called Monte Carlo simulation . +

        Of course, we could generate the same sequence of random numbers without using assignment by simply calling rand-update directly. However, this would mean that any part of our program that used random numbers would have to explicitly remember the current value of x to be passed as an argument to rand-update. To realize what an annoyance this would be, consider using random numbers to implement a technique called Monte Carlo simulation . -

        The Monte Carlo method consists of choosing sample experiments at random from a large set and then making deductions on the basis of the probabilities estimated from tabulating the results of those experiments. For example, we can approximate $\pi$ using the fact that $\frac{6}{\pi^2}$ is the probability that two integers chosen at random will have no factors in common; that is, that their greatest common divisor will be $1$.7 To obtain the approximation to $\pi$, we perform a large number of experiments. In each experiment we choose two integers at random and perform a test to see if their GCD is $1$. The fraction of times that the test is passed gives us our estimate of $\frac{6}{\pi^2}$, and from this we obtain our approximation to $\pi$. +

        The Monte Carlo method consists of choosing sample experiments at random from a large set and then making deductions on the basis of the probabilities estimated from tabulating the results of those experiments. For example, we can approximate $\pi$ using the fact that $\frac{6}{\pi^2}$ is the probability that two integers chosen at random will have no factors in common; that is, that their greatest common divisor will be $1$.7 To obtain the approximation to $\pi$, we perform a large number of experiments. In each experiment we choose two integers at random and perform a test to see if their GCD is $1$. The fraction of times that the test is passed gives us our estimate of $\frac{6}{\pi^2}$, and from this we obtain our approximation to $\pi$. -

        The heart of our program is a procedure monte-carlo, which takes as arguments the number of times to try an experiment, together with the experiment, represented as a no-argument procedure that will return either true or false each time it is run. monte-carlo runs the experiment for the designated number of trials and returns a number telling the fraction of the trials in which the experiment was found to be true. +

        The heart of our program is a procedure monte-carlo, which takes as arguments the number of times to try an experiment, together with the experiment, represented as a no-argument procedure that will return either true or false each time it is run. monte-carlo runs the experiment for the designated number of trials and returns a number telling the fraction of the trials in which the experiment was found to be true.

        (define (sqrt x) @@ -330,7 +328,7 @@

        3.1.2 The Benefits of Introducing Assignment

        C.prompt("scheme-define-monte-carlo", ["scheme-define-rand", "scheme-define-hidden-sqrt-gcd"]); -

        Now let us try the same computation using rand-update directly rather than rand, the way we would be forced to proceed if we did not use assignment to model local state: +

        Now let us try the same computation using rand-update directly rather than rand, the way we would be forced to proceed if we did not use assignment to model local state:

        (define (estimate-pi trials) @@ -356,18 +354,18 @@

        3.1.2 The Benefits of Introducing Assignment

        C.prompt("scheme-define-monte-carlo-with-rand", ["scheme-hidden-define-rand-update-init", "scheme-define-hidden-sqrt-gcd"]); -

        While the program is still simple, it betrays some painful breaches of modularity. In our first version of the program, using rand, we can express the Monte Carlo method directly as a general monte-carlo procedure that takes as an argument an arbitrary experiment procedure. In our second version of the program, with no local state for the random-number generator, random-gcd-test must explicitly manipulate the random numbers x1 and x2 and recycle x2 through the iterative loop as the new input to rand-update. This explicit handling of the random numbers intertwines the structure of accumulating test results with the fact that our particular experiment uses two random numbers, whereas other Monte Carlo experiments might use one random number or three. Even the top-level procedure estimate-pi has to be concerned with supplying an initial random number. The fact that the random-number generator's insides are leaking out into other parts of the program makes it difficult for us to isolate the Monte Carlo idea so that it can be applied to other tasks. In the first version of the program, assignment encapsulates the state of the random-number generator within the rand procedure, so that the details of random-number generation remain independent of the rest of the program. +

        While the program is still simple, it betrays some painful breaches of modularity. In our first version of the program, using rand, we can express the Monte Carlo method directly as a general monte-carlo procedure that takes as an argument an arbitrary experiment procedure. In our second version of the program, with no local state for the random-number generator, random-gcd-test must explicitly manipulate the random numbers x1 and x2 and recycle x2 through the iterative loop as the new input to rand-update. This explicit handling of the random numbers intertwines the structure of accumulating test results with the fact that our particular experiment uses two random numbers, whereas other Monte Carlo experiments might use one random number or three. Even the top-level procedure estimate-pi has to be concerned with supplying an initial random number. The fact that the random-number generator's insides are leaking out into other parts of the program makes it difficult for us to isolate the Monte Carlo idea so that it can be applied to other tasks. In the first version of the program, assignment encapsulates the state of the random-number generator within the rand procedure, so that the details of random-number generation remain independent of the rest of the program.

        The general phenomenon illustrated by the Monte Carlo example is this: From the point of view of one part of a complex process, the other parts appear to change with time. They have hidden time-varying local state. If we wish to write computer programs whose structure reflects this decomposition, we make computational objects (such as bank accounts and random-number generators) whose behavior changes with time. We model state with local state variables, and we model the changes of state with assignments to those variables.

        It is tempting to conclude this discussion by saying that, by introducing assignment and the technique of hiding state in local variables, we are able to structure systems in a more modular fashion than if all state had to be manipulated explicitly, by passing additional parameters. Unfortunately, as we shall see, the story is not so simple. -

        +

        Exercise 3.5: Monte Carlo integration is a method of estimating definite integrals by means of Monte Carlo simulation. Consider computing the area of a region of space described by a predicate $P(x, y)$ that is true for points $(x, y)$ in the region and false for points not in the region. For example, the region contained within a circle of radius $3$ centered at $(5, 7)$ is described by the predicate that tests whether $(x - 5)^2 + (y - 7)^2 \le 3^2$. To estimate the area of the region described by such a predicate, begin by choosing a rectangle that contains the region. For example, a rectangle with diagonally opposite corners at $(2, 4)$ and $(8, 10)$ contains the circle above. The desired integral is the area of that portion of the rectangle that lies in the region. We can estimate the integral by picking, at random, points $(x,y)$ that lie in the rectangle, and testing $P(x, y)$ for each point to determine whether the point lies in the region. If we try this with many points, then the fraction of points that fall in the region should give an estimate of the proportion of the rectangle that lies in the region. Hence, multiplying this fraction by the area of the entire rectangle should produce an estimate of the integral. -

        Implement Monte Carlo integration as a procedure estimate-integral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate [pi]. Use your estimate-integral to produce an estimate of [pi] by measuring the area of a unit circle. +

        Implement Monte Carlo integration as a procedure estimate-integral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate [pi]. Use your estimate-integral to produce an estimate of [pi] by measuring the area of a unit circle. -

        You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-in-range procedure implements this in terms of the random procedure used in section 1.2.6, which returns a nonnegative number less than its input.8 +

        You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-in-range procedure implements this in terms of the random procedure used in section 1.2.6, which returns a nonnegative number less than its input.8

        (define (random-in-range low high) @@ -381,17 +379,16 @@

        3.1.2 The Benefits of Introducing Assignment

        -

        -

        Exercise 3.6: It is useful to be able to reset a random-number generator to produce a sequence starting from a given value. Design a new rand procedure that is called with an argument that is either the symbol generate or the symbol reset and behaves as follows: (rand 'generate) produces a new random number; ((rand 'reset) <new-value>) resets the internal state variable to the designated <new-value>. Thus, by resetting the state, one can generate repeatable sequences. These are very handy to have when testing and debugging programs that use random numbers.

        +
        +

        Exercise 3.6: It is useful to be able to reset a random-number generator to produce a sequence starting from a given value. Design a new rand procedure that is called with an argument that is either the symbol generate or the symbol reset and behaves as follows: (rand 'generate) produces a new random number; ((rand 'reset) <new-value>) resets the internal state variable to the designated <new-value>. Thus, by resetting the state, one can generate repeatable sequences. These are very handy to have when testing and debugging programs that use random numbers.

        - -

        3.1.3 The Costs of Introducing Assignment

        +

        3.1.3 The Costs of Introducing Assignment

        -

        As we have seen, the set! operation enables us to model objects that have local state. However, this advantage comes at a price. Our programming language can no longer be interpreted in terms of the substitution model of procedure application that we introduced in section 1.1.5. Moreover, no simple model with ``nice'' mathematical properties can be an adequate framework for dealing with objects and assignment in programming languages. +

        As we have seen, the set! operation enables us to model objects that have local state. However, this advantage comes at a price. Our programming language can no longer be interpreted in terms of the substitution model of procedure application that we introduced in section 1.1.5. Moreover, no simple model with ``nice'' mathematical properties can be an adequate framework for dealing with objects and assignment in programming languages. -

        So long as we do not use assignments, two evaluations of the same procedure with the same arguments will produce the same result, so that procedures can be viewed as computing mathematical functions. Programming without any use of assignments, as we did throughout the first two chapters of this book, is accordingly known as functional programming . +

        So long as we do not use assignments, two evaluations of the same procedure with the same arguments will produce the same result, so that procedures can be viewed as computing mathematical functions. Programming without any use of assignments, as we did throughout the first two chapters of this book, is accordingly known as functional programming . -

        To understand how assignment complicates matters, consider a simplified version of the make-withdraw procedure of section 3.1.1 that does not bother to check for an insufficient amount: +

        To understand how assignment complicates matters, consider a simplified version of the make-withdraw procedure of section 3.1.1 that does not bother to check for an insufficient amount:

        (define (make-simplified-withdraw balance) @@ -413,7 +410,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.prompt("scheme-test-make-simplified-withdraw", ["scheme-define-make-simplified-withdraw"]); -

        Compare this procedure with the following make-decrementer procedure, which does not use set!: +

        Compare this procedure with the following make-decrementer procedure, which does not use set!:

        (define (make-decrementer balance) @@ -424,7 +421,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.prompt("scheme-define-make-decrementer"); -

        make-decrementer returns a procedure that subtracts its input from a designated amount balance, but there is no accumulated effect over successive calls, as with make-simplified-withdraw: +

        make-decrementer returns a procedure that subtracts its input from a designated amount balance, but there is no accumulated effect over successive calls, as with make-simplified-withdraw:

        @@ -448,7 +445,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.prompt("scheme-test-decrementer-D-10", ["scheme-test-decrementer"]); -

        We can use the substitution model to explain how make-decrementer works. For instance, let us analyze the evaluation of the expression +

        We can use the substitution model to explain how make-decrementer works. For instance, let us analyze the evaluation of the expression

        ((make-decrementer 25) 20) @@ -457,7 +454,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-make-decrementer-25-20"); -

        We first simplify the operator of the combination by substituting 25 for balance in the body of make-decrementer. This reduces the expression to +

        We first simplify the operator of the combination by substituting 25 for balance in the body of make-decrementer. This reduces the expression to

        ((lambda (amount) (- 25 amount)) 20) @@ -466,7 +463,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-lambda-decrementer"); -

        Now we apply the operator by substituting 20 for amount in the body of the lambda expression: +

        Now we apply the operator by substituting 20 for amount in the body of the lambda expression:

        (- 25 20) @@ -475,9 +472,9 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-decrementer-final"); -

        The final answer is 5. +

        The final answer is 5. -

        Observe, however, what happens if we attempt a similar substitution analysis with make-simplified-withdraw: +

        Observe, however, what happens if we attempt a similar substitution analysis with make-simplified-withdraw:

        ((make-simplified-withdraw 25) 20) @@ -486,7 +483,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-make-simplified-withdraw-25-20"); -

        We first simplify the operator by substituting 25 for balance in the body of make-simplified-withdraw. This reduces the expression to 9 +

        We first simplify the operator by substituting 25 for balance in the body of make-simplified-withdraw. This reduces the expression to 9

        ((lambda (amount) (set! balance (- 25 amount)) 25) 20) @@ -495,7 +492,7 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-lambda-make-simplified-withdraw"); -

        Now we apply the operator by substituting 20 for amount in the body of the lambda expression: +

        Now we apply the operator by substituting 20 for amount in the body of the lambda expression:

        (set! balance (- 25 20)) 25 @@ -504,15 +501,15 @@

        3.1.3 The Costs of Introducing Assignment

        C.no_output_frozen_prompt("scheme-set-balance-substituted"); -

        If we adhered to the substitution model, we would have to say that the meaning of the procedure application is to first set balance to 5 and then return 25 as the value of the expression. This gets the wrong answer. In order to get the correct answer, we would have to somehow distinguish the first occurrence of balance (before the effect of the set!) from the second occurrence of balance (after the effect of the set!), and the substitution model cannot do this. +

        If we adhered to the substitution model, we would have to say that the meaning of the procedure application is to first set balance to 5 and then return 25 as the value of the expression. This gets the wrong answer. In order to get the correct answer, we would have to somehow distinguish the first occurrence of balance (before the effect of the set!) from the second occurrence of balance (after the effect of the set!), and the substitution model cannot do this. -

        The trouble here is that substitution is based ultimately on the notion that the symbols in our language are essentially names for values. But as soon as we introduce set! and the idea that the value of a variable can change, a variable can no longer be simply a name. Now a variable somehow refers to a place where a value can be stored, and the value stored at this place can change. In section 3.2 we will see how environments play this role of "place" in our computational model. +

        The trouble here is that substitution is based ultimately on the notion that the symbols in our language are essentially names for values. But as soon as we introduce set! and the idea that the value of a variable can change, a variable can no longer be simply a name. Now a variable somehow refers to a place where a value can be stored, and the value stored at this place can change. In section 3.2 we will see how environments play this role of "place" in our computational model.

        Sameness and change

        The issue surfacing here is more profound than the mere breakdown of a particular model of computation. As soon as we introduce change into our computational models, many notions that were previously straightforward become problematical. Consider the concept of two things being "the same." -

        Suppose we call make-decrementer twice with the same argument to create two procedures: +

        Suppose we call make-decrementer twice with the same argument to create two procedures:

        (define D1 (make-decrementer 25)) @@ -523,9 +520,9 @@

        Sameness and change

        C.prompt("scheme-define-d12", ["scheme-define-make-decrementer"]); -

        Are D1 and D2 the same? An acceptable answer is yes, because D1 and D2 have the same computational behavior---each is a procedure that subtracts its input from 25. In fact, D1 could be substituted for D2 in any computation without changing the result. +

        Are D1 and D2 the same? An acceptable answer is yes, because D1 and D2 have the same computational behavior---each is a procedure that subtracts its input from 25. In fact, D1 could be substituted for D2 in any computation without changing the result. -

        Contrast this with making two calls to make-simplified-withdraw: +

        Contrast this with making two calls to make-simplified-withdraw:

        (define W1 (make-simplified-withdraw 25)) @@ -536,7 +533,7 @@

        Sameness and change

        C.prompt("scheme-define-w12", ["scheme-define-make-simplified-withdraw"]); -

        Are W1 and W2 the same? Surely not, because calls to W1 and W2 have distinct effects, as shown by the following sequence of interactions: +

        Are W1 and W2 the same? Surely not, because calls to W1 and W2 have distinct effects, as shown by the following sequence of interactions:

        (W1 20) @@ -547,9 +544,9 @@

        Sameness and change

        C.prompt("scheme-test-w12", ["scheme-define-w12"]); -

        Even though W1 and W2 are "equal" in the sense that they are both created by evaluating the same expression, (make-simplified-withdraw 25), it is not true that W1 could be substituted for W2 in any expression without changing the result of evaluating the expression. +

        Even though W1 and W2 are "equal" in the sense that they are both created by evaluating the same expression, (make-simplified-withdraw 25), it is not true that W1 could be substituted for W2 in any expression without changing the result of evaluating the expression. -

        A language that supports the concept that "equals can be substituted for equals" in an expresssion without changing the value of the expression is said to be referentially transparent . Referential transparency is violated when we include set! in our computer language. This makes it tricky to determine when we can simplify expressions by substituting equivalent expressions. Consequently, reasoning about programs that use assignment becomes drastically more difficult. +

        A language that supports the concept that "equals can be substituted for equals" in an expresssion without changing the value of the expression is said to be referentially transparent . Referential transparency is violated when we include set! in our computer language. This makes it tricky to determine when we can simplify expressions by substituting equivalent expressions. Consequently, reasoning about programs that use assignment becomes drastically more difficult.

        Once we forgo referential transparency, the notion of what it means for computational objects to be "the same" becomes difficult to capture in a formal way. Indeed, the meaning of "same" in the real world that our programs model is hardly clear in itself. In general, we can determine that two apparently identical objects are indeed "the same one" only by modifying one object and then observing whether the other object has changed in the same way. But how can we tell if an object has "changed" other than by observing the "same" object twice and seeing whether some property of the object differs from one observation to the next? Thus, we cannot determine "change" without some a priori notion of "sameness," and we cannot determine sameness without observing the effects of change. @@ -573,13 +570,13 @@

        Sameness and change

        C.prompt("scheme-define-peter-as-paul-acc", ["scheme-define-make-account"]); -

        In the first situation, the two bank accounts are distinct. Transactions made by Peter will not affect Paul's account, and vice versa. In the second situation, however, we have defined paul-acc to be the same thing as peter-acc. In effect, Peter and Paul now have a joint bank account, and if Peter makes a withdrawal from peter-acc Paul will observe less money in paul-acc. These two similar but distinct situations can cause confusion in building computational models. With the shared account, in particular, it can be especially confusing that there is one object (the bank account) that has two different names (peter-acc and paul-acc); if we are searching for all the places in our program where paul-acc can be changed, we must remember to look also at things that change peter-acc.10 +

        In the first situation, the two bank accounts are distinct. Transactions made by Peter will not affect Paul's account, and vice versa. In the second situation, however, we have defined paul-acc to be the same thing as peter-acc. In effect, Peter and Paul now have a joint bank account, and if Peter makes a withdrawal from peter-acc Paul will observe less money in paul-acc. These two similar but distinct situations can cause confusion in building computational models. With the shared account, in particular, it can be especially confusing that there is one object (the bank account) that has two different names (peter-acc and paul-acc); if we are searching for all the places in our program where paul-acc can be changed, we must remember to look also at things that change peter-acc.10

        With reference to the above remarks on "sameness" and "change," observe that if Peter and Paul could only examine their bank balances, and could not perform operations that changed the balance, then the issue of whether the two accounts are distinct would be moot. In general, so long as we never modify data objects, we can regard a compound data object to be precisely the totality of its pieces. For example, a rational number is determined by giving its numerator and its denominator. But this view is no longer valid in the presence of change, where a compound data object has an ``identity'' that is something different from the pieces of which it is composed. A bank account is still ``the same'' bank account even if we change the balance by making a withdrawal; conversely, we could have two different bank accounts with the same state information. This complication is a consequence, not of our programming language, but of our perception of a bank account as an object. We do not, for example, ordinarily regard a rational number as a changeable object with identity, such that we could change the numerator and still have ``the same''rational number.

        Pitfalls of imperative programming

        -

        In contrast to functional programming, programming that makes extensive use of assignment is known as imperative programming . In addition to raising complications about computational models, programs written in imperative style are susceptible to bugs that cannot occur in functional programs. For example, recall the iterative factorial program from section 1.2.1: +

        In contrast to functional programming, programming that makes extensive use of assignment is known as imperative programming . In addition to raising complications about computational models, programs written in imperative style are susceptible to bugs that cannot occur in functional programs. For example, recall the iterative factorial program from section 1.2.1:

        (define (factorial n) @@ -594,7 +591,7 @@

        Pitfalls of imperative programming

        C.prompt("scheme-define-factorial"); -

        Instead of passing arguments in the internal iterative loop, we could adopt a more imperative style by using explicit assignment to update the values of the variables product and counter: +

        Instead of passing arguments in the internal iterative loop, we could adopt a more imperative style by using explicit assignment to update the values of the variables product and counter:

        (define (factorial n) @@ -622,12 +619,12 @@

        Pitfalls of imperative programming

        C.no_output_frozen_prompt("scheme-wrong-order-assign-factorial"); -

        would have produced a different, incorrect result. In general, programming with assignment forces us to carefully consider the relative orders of the assignments to make sure that each statement is using the correct version of the variables that have been changed. This issue simply does not arise in functional programs.11 +

        would have produced a different, incorrect result. In general, programming with assignment forces us to carefully consider the relative orders of the assignments to make sure that each statement is using the correct version of the variables that have been changed. This issue simply does not arise in functional programs.11

        The complexity of imperative programs becomes even worse if we consider applications in which several processes execute concurrently. We will return to this in section 3.4. First, however, we will address the issue of providing a computational model for expressions that involve assignment, and explore the uses of objects with local state in designing simulations. -

        -

        Exercise 3.7: Consider the bank account objects created by make-account, with the password modification described in Exercise 3-3. Suppose that our banking system requires the ability to make joint accounts. Define a procedure make-joint that accomplishes this. Make-joint should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the make-joint operation to proceed. The third argument is a new password. Make-joint is to create an additional access to the original account using the new password. For example, if peter-acc is a bank account with password open-sesame, then +

        +

        Exercise 3.7: Consider the bank account objects created by make-account, with the password modification described in Exercise 3-3. Suppose that our banking system requires the ability to make joint accounts. Define a procedure make-joint that accomplishes this. Make-joint should take three arguments. The first is a password-protected account. The second argument must match the password with which the account was defined in order for the make-joint operation to proceed. The third argument is a new password. Make-joint is to create an additional access to the original account using the new password. For example, if peter-acc is a bank account with password open-sesame, then

        (define paul-acc @@ -637,29 +634,29 @@

        Pitfalls of imperative programming

        C.prompt("scheme-define-paul-acc-joint"); -

        will allow one to make transactions on peter-acc using the name paul-acc and the password rosebud. You may wish to modify your solution to Exercise 3-3 to accommodate this new feature +

        will allow one to make transactions on peter-acc using the name paul-acc and the password rosebud. You may wish to modify your solution to Exercise 3-3 to accommodate this new feature

        -

        -

        Exercise 3.8: When we defined the evaluation model in section 1.1.3, we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure f such that evaluating (+ (f 0) (f 1)) will return 0 if the arguments to + are evaluated from left to right but will return 1 if the arguments are evaluated from right to left. +

        +

        Exercise 3.8: When we defined the evaluation model in section 1.1.3, we said that the first step in evaluating an expression is to evaluate its subexpressions. But we never specified the order in which the subexpressions should be evaluated (e.g., left to right or right to left). When we introduce assignment, the order in which the arguments to a procedure are evaluated can make a difference to the result. Define a simple procedure f such that evaluating (+ (f 0) (f 1)) will return 0 if the arguments to + are evaluated from left to right but will return 1 if the arguments are evaluated from right to left.

        @@ {{footnotes}}
        -

        1 Actually, this is not quite true. One exception was the random-number generator in section 1.2.6. Another exception involved the operation/type tables we introduced in section 2.4.3, where the values of two calls to get with the same arguments depended on intervening calls to put. On the other hand, until we introduce assignment, we have no way to create such procedures ourselves. +

        1 Actually, this is not quite true. One exception was the random-number generator in section 1.2.6. Another exception involved the operation/type tables we introduced in section 2.4.3, where the values of two calls to get with the same arguments depended on intervening calls to put. On the other hand, until we introduce assignment, we have no way to create such procedures ourselves.

        -

        2 The value of a set! expression is implementation-dependent. set! should be used only for its effect, not for its value. +

        2 The value of a set! expression is implementation-dependent. set! should be used only for its effect, not for its value. -

        The name set! reflects a naming convention used in Scheme: Operations that change the values of variables (or that change data structures, as we will see in section 3.3) are given names that end with an exclamation point. This is similar to the convention of designating predicates by names that end with a question mark. +

        The name set! reflects a naming convention used in Scheme: Operations that change the values of variables (or that change data structures, as we will see in section 3.3) are given names that end with an exclamation point. This is similar to the convention of designating predicates by names that end with a question mark.

        -

        3 We have already used begin implicitly in our programs, because in Scheme the body of a procedure can be a sequence of expressions. Also, the part of each clause in a cond expression can be a sequence of expressions rather than a single expression. +

        3 We have already used begin implicitly in our programs, because in Scheme the body of a procedure can be a sequence of expressions. Also, the <consequent> part of each clause in a cond expression can be a sequence of expressions rather than a single expression.

        @@ -679,12 +676,12 @@

        Pitfalls of imperative programming

        -

        8 MIT Scheme provides such a procedure. If random is given an exact integer (as in section 1.2.6) it returns an exact integer, but if it is given a decimal value (as in this exercise) it returns a decimal value. +

        8 MIT Scheme provides such a procedure. If random is given an exact integer (as in section 1.2.6) it returns an exact integer, but if it is given a decimal value (as in this exercise) it returns a decimal value.

        -

        9 We don't substitute for the occurrence of balance in the set! expression because the in a set! is not evaluated. If we did substitute for it, we would get (set! 25 (- 25 amount)), which makes no sense. +

        9 We don't substitute for the occurrence of balance in the set! expression because the <name> in a set! is not evaluated. If we did substitute for it, we would get (set! 25 (- 25 amount)), which makes no sense.

        diff --git a/content/3-2-environment.content.html b/content/3-2-environment.content.html index 9eac4d6..b4d9446 100644 --- a/content/3-2-environment.content.html +++ b/content/3-2-environment.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

        The Environment Model of Evaluation

        @@ -31,12 +31,11 @@

        The Environment Model of Evaluation

        Figure 3.1: A simple environment structure.

        -

        Figure 3-1 shows a simple environment structure consisting of three frames, labeled I, II, and III. In the diagram, A, B, C, and D are pointers to environments. C and D point to the same environment. The variables z and x are bound in frame II, while y and x are bound in frame I. The value of x in environment D is 3. The value of x with respect to environment B is also 3. This is determined as follows: We examine the first frame in the sequence (frame III) and do not find a binding for x, so we proceed to the enclosing environment D and find the binding in frame I. On the other hand, the value of x in environment A is 7, because the first frame in the sequence (frame II) contains a binding of x to 7. With respect to environment A, the binding of x to 7 in frame II is said to shadow the binding of x to 3 in frame I. +

        Figure 3-1 shows a simple environment structure consisting of three frames, labeled I, II, and III. In the diagram, A, B, C, and D are pointers to environments. C and D point to the same environment. The variables z and x are bound in frame II, while y and x are bound in frame I. The value of x in environment D is 3. The value of x with respect to environment B is also 3. This is determined as follows: We examine the first frame in the sequence (frame III) and do not find a binding for x, so we proceed to the enclosing environment D and find the binding in frame I. On the other hand, the value of x in environment A is 7, because the first frame in the sequence (frame II) contains a binding of x to 7. With respect to environment A, the binding of x to 7 in frame II is said to shadow the binding of x to 3 in frame I. -

        The environment is crucial to the evaluation process, because it determines the context in which an expression should be evaluated. Indeed, one could say that expressions in a programming language do not, in themselves, have any meaning. Rather, an expression acquires a meaning only with respect to some environment in which it is evaluated. Even the interpretation of an expression as straightforward as (+ 1 1) depends on an understanding that one is operating in a context in which + is the symbol for addition. Thus, in our model of evaluation we will always speak of evaluating an expression with respect to some environment. To describe interactions with the interpreter, we will suppose that there is a global environment, consisting of a single frame (with no enclosing environment) that includes values for the symbols associated with the primitive procedures. For example, the idea that + is the symbol for addition is captured by saying that the symbol + is bound in the global environment to the primitive addition procedure. +

        The environment is crucial to the evaluation process, because it determines the context in which an expression should be evaluated. Indeed, one could say that expressions in a programming language do not, in themselves, have any meaning. Rather, an expression acquires a meaning only with respect to some environment in which it is evaluated. Even the interpretation of an expression as straightforward as (+ 1 1) depends on an understanding that one is operating in a context in which + is the symbol for addition. Thus, in our model of evaluation we will always speak of evaluating an expression with respect to some environment. To describe interactions with the interpreter, we will suppose that there is a global environment, consisting of a single frame (with no enclosing environment) that includes values for the symbols associated with the primitive procedures. For example, the idea that + is the symbol for addition is captured by saying that the symbol + is bound in the global environment to the primitive addition procedure. - -

        3.2.1 The Rules for Evaluation

        +

        3.2.1 The Rules for Evaluation

        The overall specification of how the interpreter evaluates a combination remains the same as when we first introduced it in section 1.1.3: @@ -45,7 +44,7 @@

        3.2.1 The Rules for Evaluation

        • -

          Evaluate the subexpressions of the combination.12 +

          Evaluate the subexpressions of the combination.12

        • @@ -55,7 +54,7 @@

          3.2.1 The Rules for Evaluation

          The environment model of evaluation replaces the substitution model in specifying what it means to apply a compound procedure to arguments. -

          In the environment model of evaluation, a procedure is always a pair consisting of some code and a pointer to an environment. Procedures are created in one way only: by evaluating a lambda expression. This produces a procedure whose code is obtained from the text of the lambda expression and whose environment is the environment in which the lambda expression was evaluated to produce the procedure. For example, consider the procedure definition +

          In the environment model of evaluation, a procedure is always a pair consisting of some code and a pointer to an environment. Procedures are created in one way only: by evaluating a lambda expression. This produces a procedure whose code is obtained from the text of the lambda expression and whose environment is the environment in which the lambda expression was evaluated to produce the procedure. For example, consider the procedure definition

          (define (square x) @@ -65,7 +64,7 @@

          3.2.1 The Rules for Evaluation

          C.prompt("scheme-define-square"); -

          evaluated in the global environment. The procedure definition syntax is just syntactic sugar for an underlying implicit lambda expression. It would have been equivalent to have used +

          evaluated in the global environment. The procedure definition syntax is just syntactic sugar for an underlying implicit lambda expression. It would have been equivalent to have used

          (define square @@ -75,22 +74,22 @@

          3.2.1 The Rules for Evaluation

          C.prompt("scheme-define-square-lambda"); -

          which evaluates (lambda (x) (* x x)) and binds square to the resulting value, all in the global environment. +

          which evaluates (lambda (x) (* x x)) and binds square to the resulting value, all in the global environment. -

          Figure 3-2 shows the result of evaluating this define expression. The procedure object is a pair whose code specifies that the procedure has one formal parameter, namely x, and a procedure body (* x x). The environment part of the procedure is a pointer to the global environment, since that is the environment in which the lambda expression was evaluated to produce the procedure. A new binding, which associates the procedure object with the symbol square, has been added to the global frame. In general, define creates definitions by adding bindings to frames. +

          Figure 3-2 shows the result of evaluating this define expression. The procedure object is a pair whose code specifies that the procedure has one formal parameter, namely x, and a procedure body (* x x). The environment part of the procedure is a pointer to the global environment, since that is the environment in which the lambda expression was evaluated to produce the procedure. A new binding, which associates the procedure object with the symbol square, has been added to the global frame. In general, define creates definitions by adding bindings to frames.

          -

          Figure 3.2: Environment structure produced by evaluating (define (square x) (* x x)) in the global environment. +

          Figure 3.2: Environment structure produced by evaluating (define (square x) (* x x)) in the global environment.

          Now that we have seen how procedures are created, we can describe how procedures are applied. The environment model specifies: To apply a procedure to arguments, create a new environment containing a frame that binds the parameters to the values of the arguments. The enclosing environment of this frame is the environment specified by the procedure. Now, within this new environment, evaluate the procedure body. -

          To show how this rule is followed, Figure 3-3 illustrates the environment structure created by evaluating the expression (square 5) in the global environment, where square is the procedure generated in Figure 3-2. Applying the procedure results in the creation of a new environment, labeled E1 in the figure, that begins with a frame in which x, the formal parameter for the procedure, is bound to the argument 5. The pointer leading upward from this frame shows that the frame's enclosing environment is the global environment. The global environment is chosen here, because this is the environment that is indicated as part of the square procedure object. Within E1, we evaluate the body of the procedure, (* x x). Since the value of x in E1 is 5, the result is (* 5 5), or 25. +

          To show how this rule is followed, Figure 3-3 illustrates the environment structure created by evaluating the expression (square 5) in the global environment, where square is the procedure generated in Figure 3-2. Applying the procedure results in the creation of a new environment, labeled E1 in the figure, that begins with a frame in which x, the formal parameter for the procedure, is bound to the argument 5. The pointer leading upward from this frame shows that the frame's enclosing environment is the global environment. The global environment is chosen here, because this is the environment that is indicated as part of the square procedure object. Within E1, we evaluate the body of the procedure, (* x x). Since the value of x in E1 is 5, the result is (* 5 5), or 25.

          -

          Figure 3.3: Environment created by evaluating (square 5) in the global environment. +

          Figure 3.3: Environment created by evaluating (square 5) in the global environment.

          The environment model of procedure application can be summarized by two rules: @@ -102,18 +101,17 @@

          3.2.1 The Rules for Evaluation

        • -

          A procedure is created by evaluating a lambda expression relative to a given environment. The resulting procedure object is a pair consisting of the text of the lambda expression and a pointer to the environment in which the procedure was created. +

          A procedure is created by evaluating a lambda expression relative to a given environment. The resulting procedure object is a pair consisting of the text of the lambda expression and a pointer to the environment in which the procedure was created.

        -

        We also specify that defining a symbol using define creates a binding in the current environment frame and assigns to the symbol the indicated value.13Finally, we specify the behavior of set!, the operation that forced us to introduce the environment model in the first place. Evaluating the expression (set! <variable> <value>) in some environment locates the binding of the variable in the environment and changes that binding to indicate the new value. That is, one finds the first frame in the environment that contains a binding for the variable and modifies that frame. If the variable is unbound in the environment, then set! signals an error. +

        We also specify that defining a symbol using define creates a binding in the current environment frame and assigns to the symbol the indicated value.13Finally, we specify the behavior of set!, the operation that forced us to introduce the environment model in the first place. Evaluating the expression (set! <variable> <value>) in some environment locates the binding of the variable in the environment and changes that binding to indicate the new value. That is, one finds the first frame in the environment that contains a binding for the variable and modifies that frame. If the variable is unbound in the environment, then set! signals an error.

        These evaluation rules, though considerably more complex than the substitution model, are still reasonably straightforward. Moreover, the evaluation model, though abstract, provides a correct description of how the interpreter evaluates expressions. In Chapter 4 we shall see how this model can serve as a blueprint for implementing a working interpreter. The following sections elaborate the details of the model by analyzing some illustrative programs. - -

        3.2.2 Applying Simple Procedures

        +

        3.2.2 Applying Simple Procedures

        -

        When we introduced the substitution model in section 1.1.5 we showed how the combination (f 5) evaluates to 136, given the following procedure definitions: +

        When we introduced the substitution model in section 1.1.5 we showed how the combination (f 5) evaluates to 136, given the following procedure definitions:

        (define (square x) @@ -129,14 +127,14 @@

        3.2.2 Applying Simple Procedures

        C.prompt("scheme-define-sum-of-squares"); -

        We can analyze the same example using the environment model. Figure 3-4 shows the three procedure objects created by evaluating the definitions of f, square, and sum-of-squares in the global environment. Each procedure object consists of some code, together with a pointer to the global environment. +

        We can analyze the same example using the environment model. Figure 3-4 shows the three procedure objects created by evaluating the definitions of f, square, and sum-of-squares in the global environment. Each procedure object consists of some code, together with a pointer to the global environment.

        Figure 3.4: Procedure objects in the global frame.

        -

        In Figure 3-5 we see the environment structure created by evaluating the expression (f 5). The call to f creates a new environment E1 beginning with a frame in which a, the formal parameter of f, is bound to the argument 5. In E1, we evaluate the body of f: +

        In Figure 3-5 we see the environment structure created by evaluating the expression (f 5). The call to f creates a new environment E1 beginning with a frame in which a, the formal parameter of f, is bound to the argument 5. In E1, we evaluate the body of f:

        (sum-of-squares (+ a 1) (* a 2)) @@ -150,15 +148,15 @@

        3.2.2 Applying Simple Procedures

        Figure 3.5: Environments created by evaluating

        -

        To evaluate this combination, we first evaluate the subexpressions. The first subexpression, sum-of-squares, has a value that is a procedure object. (Notice how this value is found: We first look in the first frame of E1, which contains no binding for sum-of-squares. Then we proceed to the enclosing environment, i.e. the global environment, and find the binding shown in Figure 3-4.) The other two subexpressions are evaluated by applying the primitive operations + and * to evaluate the two combinations (+ a 1) and (* a 2) to obtain 6 and 10, respectively. +

        To evaluate this combination, we first evaluate the subexpressions. The first subexpression, sum-of-squares, has a value that is a procedure object. (Notice how this value is found: We first look in the first frame of E1, which contains no binding for sum-of-squares. Then we proceed to the enclosing environment, i.e. the global environment, and find the binding shown in Figure 3-4.) The other two subexpressions are evaluated by applying the primitive operations + and * to evaluate the two combinations (+ a 1) and (* a 2) to obtain 6 and 10, respectively. -

        Now we apply the procedure object sum-of-squares to the arguments 6 and 10. This results in a new environment E2 in which the formal parameters x and y are bound to the arguments. Within E2 we evaluate the combination (+ (square x) (square y)). This leads us to evaluate (square x), where square is found in the global frame and x is 6. Once again, we set up a new environment, E3, in which x is bound to 6, and within this we evaluate the body of square, which is (* x x). Also as part of applying sum-of-squares, we must evaluate the subexpression (square y), where y is 10. This second call to square creates another environment, E4, in which x, the formal parameter of square, is bound to 10. And within E4 we must evaluate (* x x). +

        Now we apply the procedure object sum-of-squares to the arguments 6 and 10. This results in a new environment E2 in which the formal parameters x and y are bound to the arguments. Within E2 we evaluate the combination (+ (square x) (square y)). This leads us to evaluate (square x), where square is found in the global frame and x is 6. Once again, we set up a new environment, E3, in which x is bound to 6, and within this we evaluate the body of square, which is (* x x). Also as part of applying sum-of-squares, we must evaluate the subexpression (square y), where y is 10. This second call to square creates another environment, E4, in which x, the formal parameter of square, is bound to 10. And within E4 we must evaluate (* x x). -

        The important point to observe is that each call to square creates a new environment containing a binding for x. We can see here how the different frames serve to keep separate the different local variables all named x. Notice that each frame created by square points to the global environment, since this is the environment indicated by the square procedure object. +

        The important point to observe is that each call to square creates a new environment containing a binding for x. We can see here how the different frames serve to keep separate the different local variables all named x. Notice that each frame created by square points to the global environment, since this is the environment indicated by the square procedure object. -

        After the subexpressions are evaluated, the results are returned. The values generated by the two calls to square are added by sum-of-squares, and this result is returned by f. Since our focus here is on the environment structures, we will not dwell on how these returned values are passed from call to call; however, this is also an important aspect of the evaluation process, and we will return to it in detail in Chapter 5. +

        After the subexpressions are evaluated, the results are returned. The values generated by the two calls to square are added by sum-of-squares, and this result is returned by f. Since our focus here is on the environment structures, we will not dwell on how these returned values are passed from call to call; however, this is also an important aspect of the evaluation process, and we will return to it in detail in Chapter 5. -

        +

        Exercise 3.9: In section 1.2.1 we used the substitution model to analyze two procedures for computing factorials, a recursive version

        @@ -188,11 +186,10 @@

        3.2.2 Applying Simple Procedures

        C.prompt("scheme-define-factorial-iterative"); -

        Show the environment structures created by evaluating (factorial 6) using each version of the factorial procedure.14 +

        Show the environment structures created by evaluating (factorial 6) using each version of the factorial procedure.14

        - -

        3.2.3 Frames as the Repository of Local State

        +

        3.2.3 Frames as the Repository of Local State

        We can turn to the environment model to see how procedures and assignment can be used to represent objects with local state. As an example, consider the "withdrawal processor" from section 3.1.1 created by calling the procedure @@ -226,14 +223,14 @@

        3.2.3 Frames as the Repository of Local State

        C.prompt("scheme-w1-50", ["scheme-define-w1"]); -

        Figure 3-6 shows the result of defining the make-withdraw procedure in the global environment. This produces a procedure object that contains a pointer to the global environment. So far, this is no different from the examples we have already seen, except that the body of the procedure is itself a lambda expression. +

        Figure 3-6 shows the result of defining the make-withdraw procedure in the global environment. This produces a procedure object that contains a pointer to the global environment. So far, this is no different from the examples we have already seen, except that the body of the procedure is itself a lambda expression.

        -

        Figure 3.6: Result of defining make-withdraw in the global environment. +

        Figure 3.6: Result of defining make-withdraw in the global environment.

        -

        The interesting part of the computation happens when we apply the procedure make-withdraw to an argument: +

        The interesting part of the computation happens when we apply the procedure make-withdraw to an argument:

        (define W1 (make-withdraw 100)) @@ -242,14 +239,14 @@

        3.2.3 Frames as the Repository of Local State

        C.prompt("scheme-define-w1-again", ["scheme-define-make-withdraw"]); -

        We begin, as usual, by setting up an environment E1 in which the formal parameter balance is bound to the argument 100. Within this environment, we evaluate the body of make-withdraw, namely the lambda expression. This constructs a new procedure object, whose code is as specified by the lambda and whose environment is E1, the environment in which the lambda was evaluated to produce the procedure. The resulting procedure object is the value returned by the call to make-withdraw. This is bound to W1 in the global environment, since the define itself is being evaluated in the global environment. Figure 3-7 shows the resulting environment structure. +

        We begin, as usual, by setting up an environment E1 in which the formal parameter balance is bound to the argument 100. Within this environment, we evaluate the body of make-withdraw, namely the lambda expression. This constructs a new procedure object, whose code is as specified by the lambda and whose environment is E1, the environment in which the lambda was evaluated to produce the procedure. The resulting procedure object is the value returned by the call to make-withdraw. This is bound to W1 in the global environment, since the define itself is being evaluated in the global environment. Figure 3-7 shows the resulting environment structure.

        -

        Figure 3.7: Result of evaluating (define W1 (make-withdraw 100)). +

        Figure 3.7: Result of evaluating (define W1 (make-withdraw 100)).

        -

        Now we can analyze what happens when W1 is applied to an argument: +

        Now we can analyze what happens when W1 is applied to an argument:

        (W1 50) @@ -258,7 +255,7 @@

        3.2.3 Frames as the Repository of Local State

        C.prompt("scheme-w1-50-again", ["scheme-define-w1"]); -

        We begin by constructing a frame in which amount, the formal parameter of W1, is bound to the argument 50. The crucial point to observe is that this frame has as its enclosing environment not the global environment, but rather the environment E1, because this is the environment that is specified by the W1 procedure object. Within this new environment, we evaluate the body of the procedure: +

        We begin by constructing a frame in which amount, the formal parameter of W1, is bound to the argument 50. The crucial point to observe is that this frame has as its enclosing environment not the global environment, but rather the environment E1, because this is the environment that is specified by the W1 procedure object. Within this new environment, we evaluate the body of the procedure:

        (if (>= balance amount) @@ -270,22 +267,22 @@

        3.2.3 Frames as the Repository of Local State

        C.no_output_frozen_prompt("scheme-w1-internal"); -

        The resulting environment structure is shown in Figure 3-8. The expression being evaluated references both amount and balance. Amount will be found in the first frame in the environment, while balance will be found by following the enclosing-environment pointer to E1. +

        The resulting environment structure is shown in Figure 3-8. The expression being evaluated references both amount and balance. Amount will be found in the first frame in the environment, while balance will be found by following the enclosing-environment pointer to E1.

        -

        Figure 3.8: Environments created by applying the procedure object W1. +

        Figure 3.8: Environments created by applying the procedure object W1.

        -

        When the set! is executed, the binding of balance in E1 is changed. At the completion of the call to W1, balance is 50, and the frame that contains balance is still pointed to by the procedure object W1. The frame that binds amount (in which we executed the code that changed balance) is no longer relevant, since the procedure call that constructed it has terminated, and there are no pointers to that frame from other parts of the environment. The next time W1 is called, this will build a new frame that binds amount and whose enclosing environment is E1. We see that E1 serves as the ``place'' that holds the local state variable for the procedure object W1. Figure 3-9 shows the situation after the call to W1. +

        When the set! is executed, the binding of balance in E1 is changed. At the completion of the call to W1, balance is 50, and the frame that contains balance is still pointed to by the procedure object W1. The frame that binds amount (in which we executed the code that changed balance) is no longer relevant, since the procedure call that constructed it has terminated, and there are no pointers to that frame from other parts of the environment. The next time W1 is called, this will build a new frame that binds amount and whose enclosing environment is E1. We see that E1 serves as the ``place'' that holds the local state variable for the procedure object W1. Figure 3-9 shows the situation after the call to W1.

        -

        Figure 3.9: Environments after the call to W1. +

        Figure 3.9: Environments after the call to W1.

        Observe what happens when we create a second "withdraw" object by making -another call to make-withdraw: +another call to make-withdraw:

        (define W2 (make-withdraw 100)) @@ -294,15 +291,15 @@

        3.2.3 Frames as the Repository of Local State

        C.prompt("scheme-define-w2", ["scheme-define-make-withdraw"]); -

        This produces the environment structure of Figure 3-10, which shows that W2 is a procedure object, that is, a pair with some code and an environment. The environment E2 for W2 was created by the call to make-withdraw. It contains a frame with its own local binding for balance. On the other hand, W1 and W2 have the same code: the code specified by the lambda expression in the body of make-withdraw.15 We see here why W1 and W2 behave as independent objects. Calls to W1 reference the state variable balance stored in E1, whereas calls to W2 reference the balance stored in E2. Thus, changes to the local state of one object do not affect the other object. +

        This produces the environment structure of Figure 3-10, which shows that W2 is a procedure object, that is, a pair with some code and an environment. The environment E2 for W2 was created by the call to make-withdraw. It contains a frame with its own local binding for balance. On the other hand, W1 and W2 have the same code: the code specified by the lambda expression in the body of make-withdraw.15 We see here why W1 and W2 behave as independent objects. Calls to W1 reference the state variable balance stored in E1, whereas calls to W2 reference the balance stored in E2. Thus, changes to the local state of one object do not affect the other object.

        -

        Figure 3.10: Using (define W2 (make-withdraw 100)) to create a second object. +

        Figure 3.10: Using (define W2 (make-withdraw 100)) to create a second object.

        -
        -

        Exercise 3.10: In the make-withdraw procedure, the local variable balance is created as a parameter of make-withdraw. We could also create the local state variable explicitly, using let, as follows: +

        +

        Exercise 3.10: In the make-withdraw procedure, the local variable balance is created as a parameter of make-withdraw. We could also create the local state variable explicitly, using let, as follows:

        (define (make-withdraw initial-amount) @@ -317,7 +314,7 @@

        3.2.3 Frames as the Repository of Local State

        C.prompt("scheme-define-make-withdraw-with-let"); -

        Recall from section 1.3.2 that let is simply syntactic sugar for a procedure call: +

        Recall from section 1.3.2 that let is simply syntactic sugar for a procedure call:

        (let ((<var> <exp>)) <body>) @@ -335,7 +332,7 @@

        3.2.3 Frames as the Repository of Local State

        C.no_output_frozen_prompt("scheme-let-syntax-lambda"); -

        Use the environment model to analyze this alternate version of make-withdraw, drawing figures like the ones above to illustrate the interactions +

        Use the environment model to analyze this alternate version of make-withdraw, drawing figures like the ones above to illustrate the interactions

        (define W1 (make-withdraw 100)) @@ -346,11 +343,10 @@

        3.2.3 Frames as the Repository of Local State

        C.no_output_frozen_prompt("scheme-prompts-for-make-withdraw-let-diagram"); -

        Show that the two versions of make-withdraw create objects with the same behavior. How do the environment structures differ for the two versions? +

        Show that the two versions of make-withdraw create objects with the same behavior. How do the environment structures differ for the two versions?

        - -

        3.2.4 Internal Definitions

        +

        3.2.4 Internal Definitions

        Section 1.1.8 introduced the idea that procedures can have internal definitions, thus leading to a block structure as in the following procedure to @@ -380,14 +376,14 @@

        3.2.4 Internal Definitions

        C.prompt("scheme-define-sqrt", ["scheme-define-square", "scheme-define-average"]); -

        Now we can use the environment model to see why these internal definitions behave as desired. Figure 3-11 shows the point in the evaluation of the expression (sqrt 2) where the internal procedure good-enough? has been called for the first time with guess equal to 1. +

        Now we can use the environment model to see why these internal definitions behave as desired. Figure 3-11 shows the point in the evaluation of the expression (sqrt 2) where the internal procedure good-enough? has been called for the first time with guess equal to 1.

        -

        Figure 3.11: Sqrt procedure with internal definitions. +

        Figure 3.11: Sqrt procedure with internal definitions.

        -

        Observe the structure of the environment. Sqrt is a symbol in the global environment that is bound to a procedure object whose associated environment is the global environment. When sqrt was called, a new environment E1 was formed, subordinate to the global environment, in which the parameter x is bound to 2. The body of sqrt was then evaluated in E1. Since the first expression in the body of sqrt is +

        Observe the structure of the environment. Sqrt is a symbol in the global environment that is bound to a procedure object whose associated environment is the global environment. When sqrt was called, a new environment E1 was formed, subordinate to the global environment, in which the parameter x is bound to 2. The body of sqrt was then evaluated in E1. Since the first expression in the body of sqrt is

        (define (good-enough? guess) @@ -397,9 +393,9 @@

        3.2.4 Internal Definitions

        C.no_output_frozen_prompt("scheme-define-good-enough?"); -

        evaluating this expression defined the procedure good-enough? in the environment E1. To be more precise, the symbol good-enough? was added to the first frame of E1, bound to a procedure object whose associated environment is E1. Similarly, improve and sqrt-iter were defined as procedures in E1. For conciseness, Figure 3-11 shows only the procedure object for good-enough?. +

        evaluating this expression defined the procedure good-enough? in the environment E1. To be more precise, the symbol good-enough? was added to the first frame of E1, bound to a procedure object whose associated environment is E1. Similarly, improve and sqrt-iter were defined as procedures in E1. For conciseness, Figure 3-11 shows only the procedure object for good-enough?. -

        After the local procedures were defined, the expression (sqrt-iter 1.0) was evaluated, still in environment E1. So the procedure object bound to sqrt-iter in E1 was called with 1 as an argument. This created an environment E2 in which guess, the parameter of sqrt-iter, is bound to 1. Sqrt-iter in turn called good-enough? with the value of guess (from E2) as the argument for good-enough?. This set up another environment, E3, in which guess (the parameter of good-enough?) is bound to 1. Although sqrt-iter and good-enough? both have a parameter named guess, these are two distinct local variables located in different frames. Also, E2 and E3 both have E1 as their enclosing environment, because the sqrt-iter and good-enough? procedures both have E1 as their environment part. One consequence of this is that the symbol x that appears in the body of good-enough? will reference the binding of x that appears in E1, namely the value of x with which the original sqrt procedure was called. +

        After the local procedures were defined, the expression (sqrt-iter 1.0) was evaluated, still in environment E1. So the procedure object bound to sqrt-iter in E1 was called with 1 as an argument. This created an environment E2 in which guess, the parameter of sqrt-iter, is bound to 1. Sqrt-iter in turn called good-enough? with the value of guess (from E2) as the argument for good-enough?. This set up another environment, E3, in which guess (the parameter of good-enough?) is bound to 1. Although sqrt-iter and good-enough? both have a parameter named guess, these are two distinct local variables located in different frames. Also, E2 and E3 both have E1 as their enclosing environment, because the sqrt-iter and good-enough? procedures both have E1 as their environment part. One consequence of this is that the symbol x that appears in the body of good-enough? will reference the binding of x that appears in E1, namely the value of x with which the original sqrt procedure was called.

        The environment model thus explains the two key properties that make local procedure definitions a useful technique for modularizing programs: @@ -414,7 +410,7 @@

        3.2.4 Internal Definitions

      -
      +

      Exercise 3.11: In section 3.2.3 we saw how the environment model described the behavior of procedures with local state. Now we have seen how internal definitions work. A typical message-passing procedure contains both of these aspects. Consider the bank account procedure of section 3.1.1:

      @@ -449,7 +445,7 @@

      3.2.4 Internal Definitions

      C.prompt("scheme-define-acc", ["scheme-define-make-withdraw-message-passing"]); -

      Where is the local state for acc kept? Suppose we define another account +

      Where is the local state for acc kept? Suppose we define another account

      (define acc2 (make-account 100)) @@ -459,7 +455,7 @@

      3.2.4 Internal Definitions

      How are the local states for the two accounts kept distinct? Which parts of -the environment structure are shared between acc and acc2? +the environment structure are shared between acc and acc2?

      @@ @@ -473,10 +469,10 @@

      3.2.4 Internal Definitions

      -

      14 The environment model will not clarify our claim in section 1.2.1 that the interpreter can execute a procedure such as fact-iter in a constant amount of space using tail recursion. We will discuss tail recursion when we deal with the control structure of the interpreter in section 5.4. +

      14 The environment model will not clarify our claim in section 1.2.1 that the interpreter can execute a procedure such as fact-iter in a constant amount of space using tail recursion. We will discuss tail recursion when we deal with the control structure of the interpreter in section 5.4.

      -

      15 Whether W1 and W2 share the same physical code stored in the computer, or whether they each keep a copy of the code, is a detail of the implementation. For the interpreter we implement in chapter 4, the code is in fact shared. +

      15 Whether W1 and W2 share the same physical code stored in the computer, or whether they each keep a copy of the code, is a detail of the implementation. For the interpreter we implement in chapter 4, the code is in fact shared.

      @@ diff --git a/content/3-3-modeling.content.html b/content/3-3-modeling.content.html index b2b7fe4..6330678 100644 --- a/content/3-3-modeling.content.html +++ b/content/3-3-modeling.content.html @@ -3,17 +3,17 @@ @@ {{main_text}} - + - - + +

      Modeling with Mutable Data


      -

      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 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 +

      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 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

      (set-balance! <account> <new-value>) @@ -22,44 +22,43 @@

      Modeling with Mutable Data

      C.no_output_frozen_prompt("scheme-set-balance"); -

      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. +

      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.

      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 section 2.2. We also present some examples of simulations in which complex systems are modeled as collections of objects with local state. - -

      3.3.1 Mutable List Structure

      +

      3.3.1 Mutable List Structure

      -

      The basic operations on pairs – cons, car, and cdr – can be used to construct list structure and to select parts from list structure, but they are incapable of modifying list structure. The same is true of the list operations we have used so far, such as append and list, since these can be defined in terms of cons, car, and cdr. To modify list structures we need new operations. +

      The basic operations on pairs – cons, car, and cdr – can be used to construct list structure and to select parts from list structure, but they are incapable of modifying list structure. The same is true of the list operations we have used so far, such as append and list, since these can be defined in terms of cons, car, and cdr. To modify list structures we need new operations.

      -

      Figure 3.12: Lists x: ((a b) c d) and y: (e f). +

      Figure 3.12: Lists x: ((a b) c d) and y: (e f).

      -

      Figure 3.13: Effect of (set-car! x y) on the lists in Figure 3-12. +

      Figure 3.13: Effect of (set-car! x y) on the lists in Figure 3-12.

      -

      Figure 3.14: Effect of (define z (cons y (cdr x))) on the lists in Figure 3-12. +

      Figure 3.14: Effect of (define z (cons y (cdr x))) on the lists in Figure 3-12.

      -

      Figure 3.15: Effect of (set-cdr! x y) on the lists in Figure 3-12. +

      Figure 3.15: Effect of (set-cdr! x y) on the lists in Figure 3-12.

      -

      The primitive mutators for pairs are set-car! and set-cdr!. Set-car! takes two arguments, the first of which must be a pair. It modifies this pair, replacing the car pointer by a pointer to the second argument of set-car!.16 +

      The primitive mutators for pairs are set-car! and set-cdr!. Set-car! takes two arguments, the first of which must be a pair. It modifies this pair, replacing the car pointer by a pointer to the second argument of set-car!.16 -

      As an example, suppose that x is bound to the list ((a b) c d) and y to the list (e f) as illustrated in Figure 3-12. Evaluating the expression (set-car! x y) modifies the pair to which x is bound, replacing its car by the value of y. The result of the operation is shown in Figure 3-13. The structure x has been modified and would now be printed as ((e f) c d). The pairs representing the list (a b), identified by the pointer that was replaced, are now detached from the original structure.17 +

      As an example, suppose that x is bound to the list ((a b) c d) and y to the list (e f) as illustrated in Figure 3-12. Evaluating the expression (set-car! x y) modifies the pair to which x is bound, replacing its car by the value of y. The result of the operation is shown in Figure 3-13. The structure x has been modified and would now be printed as ((e f) c d). The pairs representing the list (a b), identified by the pointer that was replaced, are now detached from the original structure.17 -

      Compare Figure 3-13 with Figure 3-14, which illustrates the result of executing (define z (cons y (cdr x))) with x and y bound to the original lists of Figure 3-12. The variable z is now bound to a new pair created by the cons operation; the list to which x is bound is unchanged. +

      Compare Figure 3-13 with Figure 3-14, which illustrates the result of executing (define z (cons y (cdr x))) with x and y bound to the original lists of Figure 3-12. The variable z is now bound to a new pair created by the cons operation; the list to which x is bound is unchanged. -

      The set-cdr! operation is similar to set-car!. The only difference is that the cdr pointer of the pair, rather than the car pointer, is replaced. The effect of executing (set-cdr! x y) on the lists of Figure 3-12 is shown in Figure 3-15. Here the cdr pointer of x has been replaced by the pointer to (e f). Also, the list (c d), which used to be the cdr of x, is now detached from the structure. +

      The set-cdr! operation is similar to set-car!. The only difference is that the cdr pointer of the pair, rather than the car pointer, is replaced. The effect of executing (set-cdr! x y) on the lists of Figure 3-12 is shown in Figure 3-15. Here the cdr pointer of x has been replaced by the pointer to (e f). Also, the list (c d), which used to be the cdr of x, is now detached from the structure. -

      Cons builds new list structure by creating new pairs, while set-car! and set-cdr! modify existing pairs. Indeed, we could implement cons in terms of the two mutators, together with a procedure get-new-pair, which returns a new pair that is not part of any existing list structure. We obtain the new pair, set its car and cdr pointers to the designated objects, and return the new pair as the result of the cons.18 +

      Cons builds new list structure by creating new pairs, while set-car! and set-cdr! modify existing pairs. Indeed, we could implement cons in terms of the two mutators, together with a procedure get-new-pair, which returns a new pair that is not part of any existing list structure. We obtain the new pair, set its car and cdr pointers to the designated objects, and return the new pair as the result of the cons.18

      (define (get-new-pair) '(() ())) @@ -76,7 +75,7 @@

      3.3.1 Mutable List Structure

      -

      +

      Exercise 3.12: The following procedure for appending lists was introduced in section 2.2.1:

      @@ -89,7 +88,7 @@

      3.3.1 Mutable List Structure

      C.prompt("scheme-define-append"); -

      append forms a new list by successively consing the elements of x onto y. The procedure append! is similar to append, but it is a mutator rather than a constructor. It appends the lists by splicing them together, modifying the final pair of x so that its cdr is now y. (It is an error to call append! with an empty x.) +

      append forms a new list by successively consing the elements of x onto y. The procedure append! is similar to append, but it is a mutator rather than a constructor. It appends the lists by splicing them together, modifying the final pair of x so that its cdr is now y. (It is an error to call append! with an empty x.)

      (define (append! x y) @@ -100,7 +99,7 @@

      3.3.1 Mutable List Structure

      C.prompt("scheme-define-append-inplace", ["scheme-define-last-pair"]); -

      Here last-pair is a procedure that returns the last pair in its argument: +

      Here last-pair is a procedure that returns the last pair in its argument:

      (define (last-pair x) @@ -139,13 +138,13 @@

      3.3.1 Mutable List Structure

      C.no_output_frozen_prompt("scheme-define-test-append-inplace"); -

      What are the missing <response>s? Draw box-and-pointer diagrams to explain your answer. +

      What are the missing <response>s? Draw box-and-pointer diagrams to explain your answer.

      -

      -

      Exercise 3.13: Consider the following make-cycle procedure, which uses the last-pair procedure defined in Exercise 3-12: +

      +

      Exercise 3.13: Consider the following make-cycle procedure, which uses the last-pair procedure defined in Exercise 3-12:

      (define (make-cycle x) @@ -156,7 +155,7 @@

      3.3.1 Mutable List Structure

      C.prompt("scheme-define-make-cycle", ["scheme-define-last-pair"]); -

      Draw a box-and-pointer diagram that shows the structure z created by +

      Draw a box-and-pointer diagram that shows the structure z created by

      (define z (make-cycle (list 'a 'b 'c))) @@ -165,12 +164,12 @@

      3.3.1 Mutable List Structure

      C.prompt("scheme-define-z-cycle", ["scheme-define-make-cycle"]); -

      What happens if we try to compute (last-pair z)? +

      What happens if we try to compute (last-pair z)?

      -

      +

      Exercise 3.14: The following procedure is quite useful, although obscure:

      @@ -187,11 +186,11 @@

      3.3.1 Mutable List Structure

      C.prompt("scheme-define-mystery"); -

      loop uses the ``temporary'' variable temp to hold the old value of the cdr of x, since the set-cdr! on the next line destroys the cdr. Explain what mystery does in general. Suppose v is defined by (define v (list 'a 'b 'c 'd)). Draw the box-and-pointer diagram that represents the list to which v is bound. Suppose that we now evaluate (define w (mystery v)). Draw box-and-pointer diagrams that show the structures v and w after evaluating this expression. What would be printed as the values of v and w?

      +

      loop uses the ``temporary'' variable temp to hold the old value of the cdr of x, since the set-cdr! on the next line destroys the cdr. Explain what mystery does in general. Suppose v is defined by (define v (list 'a 'b 'c 'd)). Draw the box-and-pointer diagram that represents the list to which v is bound. Suppose that we now evaluate (define w (mystery v)). Draw box-and-pointer diagrams that show the structures v and w after evaluating this expression. What would be printed as the values of v and w?

      Sharing and identity

      -

      We mentioned in section 3.1.3 the theoretical issues of ``sameness'' and ``change'' raised by the introduction of assignment. These issues arise in practice when individual pairs are shared among different data objects. For example, consider the structure formed by +

      We mentioned in section 3.1.3 the theoretical issues of ``sameness'' and ``change'' raised by the introduction of assignment. These issues arise in practice when individual pairs are shared among different data objects. For example, consider the structure formed by

      (define x (list 'a 'b)) @@ -207,16 +206,16 @@

      Sharing and identity

      C.prompt("scheme-define-z1", ["scheme-define-x"]); -

      As shown in Figure 3-16, z1 is a pair whose car and cdr both point to the same pair x. This sharing of x by the car and cdr of z1 is a consequence of the straightforward way in which cons is implemented. In general, using cons to construct lists will result in an interlinked structure of pairs in which many individual pairs are shared by many different structures. +

      As shown in Figure 3-16, z1 is a pair whose car and cdr both point to the same pair x. This sharing of x by the car and cdr of z1 is a consequence of the straightforward way in which cons is implemented. In general, using cons to construct lists will result in an interlinked structure of pairs in which many individual pairs are shared by many different structures.

      -

      Figure 3.16: The list z1 formed by (cons x x). +

      Figure 3.16: The list z1 formed by (cons x x).

      -

      Figure 3.17: The list z2 formed by (cons (list 'a 'b) (list 'a 'b)). +

      Figure 3.17: The list z2 formed by (cons (list 'a 'b) (list 'a 'b)).

      In contrast to Figure 3-16, Figure 3-17 shows the structure created by @@ -228,9 +227,9 @@

      Sharing and identity

      C.prompt("scheme-define-z2"); -

      In this structure, the pairs in the two (a b) lists are distinct, although the actual symbols are shared.19 +

      In this structure, the pairs in the two (a b) lists are distinct, although the actual symbols are shared.19 -

      When thought of as a list, z1 and z2 both represent ``the same''list, ((a b) a b). In general, sharing is completely undetectable if we operate on lists using only cons, car, and cdr. However, if we allow mutators on list structure, sharing becomes significant. As an example of the difference that sharing can make, consider the following procedure, which modifies the car of the structure to which it is applied: +

      When thought of as a list, z1 and z2 both represent ``the same''list, ((a b) a b). In general, sharing is completely undetectable if we operate on lists using only cons, car, and cdr. However, if we allow mutators on list structure, sharing becomes significant. As an example of the difference that sharing can make, consider the following procedure, which modifies the car of the structure to which it is applied:

      (define (set-to-wow! x) @@ -241,7 +240,7 @@

      Sharing and identity

      C.prompt("scheme-define-set-to-wow"); -

      Even though z1 and z2 are "the same" structure, applying set-to-wow! to them yields different results. With z1, altering the car also changes the cdr, because in z1 the car and the cdr are the same pair. With z2, the car and cdr are distinct, so set-to-wow! modifies only the car: +

      Even though z1 and z2 are "the same" structure, applying set-to-wow! to them yields different results. With z1, altering the car also changes the cdr, because in z1 the car and the cdr are the same pair. With z2, the car and cdr are distinct, so set-to-wow! modifies only the car:

      @@ -278,18 +277,18 @@

      Sharing and identity

      C.prompt("scheme-test-set-to-wow-z2", ["scheme-define-z2", "scheme-define-set-to-wow"]); -

      One way to detect sharing in list structures is to use the predicate eq?, which we introduced in section 2.3.1 as a way to test whether two symbols are equal. More generally, (eq? x y) tests whether x and y are the same object (that is, whether x and y are equal as pointers). Thus, with z1 and z2 as defined in figures Figure 3-16 and Figure 3-17, (eq? (car z1) (cdr z1)) is true and (eq? (car z2) (cdr z2)) is false. +

      One way to detect sharing in list structures is to use the predicate eq?, which we introduced in section 2.3.1 as a way to test whether two symbols are equal. More generally, (eq? x y) tests whether x and y are the same object (that is, whether x and y are equal as pointers). Thus, with z1 and z2 as defined in figures Figure 3-16 and Figure 3-17, (eq? (car z1) (cdr z1)) is true and (eq? (car z2) (cdr z2)) is false. -

      As will be seen in the following sections, we can exploit sharing to greatly extend the repertoire of data structures that can be represented by pairs. On the other hand, sharing can also be dangerous, since modifications made to structures will also affect other structures that happen to share the modified parts. The mutation operations set-car! and set-cdr! should be used with care; unless we have a good understanding of how our data objects are shared, mutation can have unanticipated results.20 +

      As will be seen in the following sections, we can exploit sharing to greatly extend the repertoire of data structures that can be represented by pairs. On the other hand, sharing can also be dangerous, since modifications made to structures will also affect other structures that happen to share the modified parts. The mutation operations set-car! and set-cdr! should be used with care; unless we have a good understanding of how our data objects are shared, mutation can have unanticipated results.20 -

      -

      Exercise 3.15: Draw box-and-pointer diagrams to explain the effect of set-to-wow! on the structures z1 and z2 above. +

      +

      Exercise 3.15: Draw box-and-pointer diagrams to explain the effect of set-to-wow! on the structures z1 and z2 above.

      -

      -

      Exercise 3.16: Ben Bitdiddle decides to write a procedure to count the number of pairs in any list structure. "It's easy," he reasons. "The number of pairs in any structure is the number in the car plus the number in the cdr plus one more to count the current pair." So Ben writes the following procedure: +

      +

      Exercise 3.16: Ben Bitdiddle decides to write a procedure to count the number of pairs in any list structure. "It's easy," he reasons. "The number of pairs in any structure is the number in the car plus the number in the cdr plus one more to count the current pair." So Ben writes the following procedure:

      (define (count-pairs x) @@ -308,19 +307,19 @@

      Sharing and identity

      -

      -

      Exercise 3.17: Devise a correct version of the count-pairs procedure of Exercise 3-16 that returns the number of distinct pairs in any structure. (Hint: Traverse the structure, maintaining an auxiliary data structure that is used to keep track of which pairs have already been counted.) +

      +

      Exercise 3.17: Devise a correct version of the count-pairs procedure of Exercise 3-16 that returns the number of distinct pairs in any structure. (Hint: Traverse the structure, maintaining an auxiliary data structure that is used to keep track of which pairs have already been counted.)

      -

      -

      Exercise 3.18: Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop. Exercise 3-13 constructed such lists. +

      +

      Exercise 3.18: Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop. Exercise 3-13 constructed such lists.

      -

      +

      Exercise 3.19: Redo Exercise 3-18 using an algorithm that takes only a constant amount of space. (This requires a very clever idea.)

      @@ -344,7 +343,7 @@

      Mutation is just assignment

      C.prompt("scheme-define-cons"); -

      The same observation is true for mutable data. We can implement mutable data objects as procedures using assignment and local state. For instance, we can extend the above pair implementation to handle set-car! and set-cdr! in a manner analogous to the way we implemented bank accounts using make-account in section 3.1.1: +

      The same observation is true for mutable data. We can implement mutable data objects as procedures using assignment and local state. For instance, we can extend the above pair implementation to handle set-car! and set-cdr! in a manner analogous to the way we implemented bank accounts using make-account in section 3.1.1:

      (define (cons x y) @@ -374,9 +373,9 @@

      Mutation is just assignment

      C.prompt("scheme-define-cons-set-car-cdr"); -

      Assignment is all that is needed, theoretically, to account for the behavior of mutable data. As soon as we admit set! to our language, we raise all the issues, not only of assignment, but of mutable data in general.21 +

      Assignment is all that is needed, theoretically, to account for the behavior of mutable data. As soon as we admit set! to our language, we raise all the issues, not only of assignment, but of mutable data in general.21 -

      +

      Exercise 3.20: Draw environment diagrams to illustrate the evaluation of the sequence of expressions

      @@ -393,12 +392,11 @@

      Mutation is just assignment

      using the procedural implementation of pairs given above. (Compare Exercise 3-11.)

      - -

      3.3.2 Representing Queues

      +

      3.3.2 Representing Queues

      -

      The mutators set-car! and set-cdr! enable us to use pairs to construct data structures that cannot be built with cons, car, and cdr alone. This section shows how to use pairs to represent a data structure called a queue. Section 3.3.3 will show how to represent data structures called tables. +

      The mutators set-car! and set-cdr! enable us to use pairs to construct data structures that cannot be built with cons, car, and cdr alone. This section shows how to use pairs to represent a data structure called a queue. Section 3.3.3 will show how to represent data structures called tables. -

      A queue is a sequence in which items are inserted at one end (called the rear of the queue) and deleted from the other end (the front ). Figure 3-18 shows an initially empty queue in which the items a and b are inserted. Then a is removed, c and d are inserted, and b is removed. Because items are always removed in the order in which they are inserted, a queue is sometimes called a FIFO (first in, first out) buffer. +

      A queue is a sequence in which items are inserted at one end (called the rear of the queue) and deleted from the other end (the front ). Figure 3-18 shows an initially empty queue in which the items a and b are inserted. Then a is removed, c and d are inserted, and b is removed. Because items are always removed in the order in which they are inserted, a queue is sometimes called a FIFO (first in, first out) buffer.

       Operation                Resulting Queue
      @@ -419,31 +417,31 @@ 

      3.3.2 Representing Queues

      • -

        a constructor: (make-queue) returns an empty queue (a queue containing no items). +

        a constructor: (make-queue) returns an empty queue (a queue containing no items).

      • two selectors: -

        (empty-queue? <queue>) tests if the queue is empty. +

        (empty-queue? <queue>) tests if the queue is empty. -

        (front-queue <queue>) returns the object at the front of the queue, signaling an error if the queueis empty; it does not modify the queue. +

        (front-queue <queue>) returns the object at the front of the queue, signaling an error if the queueis empty; it does not modify the queue.

      • two mutators: -

        (insert-queue! <queue> <item>) inserts the item at the rear of the queue and returns the modified queue as its value. +

        (insert-queue! <queue> <item>) inserts the item at the rear of the queue and returns the modified queue as its value. -

        (delete-queue! <queue>) removes the item at the front of the queue and returns the modified queue as its value, signaling an error if the queue is empty before the deletion. +

        (delete-queue! <queue>) removes the item at the front of the queue and returns the modified queue as its value, signaling an error if the queue is empty before the deletion.

      -

      Because a queue is a sequence of items, we could certainly represent it as an ordinary list; the front of the queue would be the car of the list, inserting an item in the queue would amount to appending a new element at the end of the list, and deleting an item from the queue would just be taking the cdr of the list. However, this representation is inefficient, because in order to insert an item we must scan the list until we reach the end. Since the only method we have for scanning a list is by successive cdr operations, this scanning requires [theta](n) steps for a list of n items. A simple modification to the list representation overcomes this disadvantage by allowing the queue operations to be implemented so that they require [theta](1) steps; that is, so that the number of steps needed is independent of the length of the queue. +

      Because a queue is a sequence of items, we could certainly represent it as an ordinary list; the front of the queue would be the car of the list, inserting an item in the queue would amount to appending a new element at the end of the list, and deleting an item from the queue would just be taking the cdr of the list. However, this representation is inefficient, because in order to insert an item we must scan the list until we reach the end. Since the only method we have for scanning a list is by successive cdr operations, this scanning requires [theta](n) steps for a list of n items. A simple modification to the list representation overcomes this disadvantage by allowing the queue operations to be implemented so that they require [theta](1) steps; that is, so that the number of steps needed is independent of the length of the queue.

      The difficulty with the list representation arises from the need to scan to find the end of the list. The reason we need to scan is that, although the standard way of representing a list as a chain of pairs readily provides us with a pointer to the beginning of the list, it gives us no easily accessible pointer to the end. The modification that avoids the drawback is to represent the queue as a list, together with an additional pointer that indicates the final pair in the list. That way, when we go to insert an item, we can consult the rear pointer and so avoid scanning the list. -

      A queue is represented, then, as a pair of pointers, front-ptr and rear-ptr, which indicate, respectively, the first and last pairs in an ordinary list. Since we would like the queue to be an identifiable object, we can use cons to combine the two pointers. Thus, the queue itself will be the cons of the two pointers. Figure 3-19 illustrates this representation. +

      A queue is represented, then, as a pair of pointers, front-ptr and rear-ptr, which indicate, respectively, the first and last pairs in an ordinary list. Since we would like the queue to be an identifiable object, we can use cons to combine the two pointers. Thus, the queue itself will be the cons of the two pointers. Figure 3-19 illustrates this representation.

      @@ -474,7 +472,7 @@

      3.3.2 Representing Queues

      C.prompt("scheme-define-empty-queue?", ["scheme-define-queue-accessors"]); -

      The make-queue constructor returns, as an initially empty queue, a pair whose car and cdr are both the empty list: +

      The make-queue constructor returns, as an initially empty queue, a pair whose car and cdr are both the empty list:

      (define (make-queue) (cons '() '())) @@ -483,7 +481,7 @@

      3.3.2 Representing Queues

      C.prompt("scheme-define-make-queue", ["scheme-define-empty-queue?"]); -

      To select the item at the front of the queue, we return the car of the pair indicated by the front pointer: +

      To select the item at the front of the queue, we return the car of the pair indicated by the front pointer:

      (define (front-queue queue) @@ -495,11 +493,11 @@

      3.3.2 Representing Queues

      C.prompt("scheme-define-front-queue", ["scheme-define-make-queue"]); -

      To insert an item in a queue, we follow the method whose result is indicated in Figure 3-20. We first create a new pair whose car is the item to be inserted and whose cdr is the empty list. If the queue was initially empty, we set the front and rear pointers of the queue to this new pair. Otherwise, we modify the final pair in the queue to point to the new pair, and also set the rear pointer to the new pair. +

      To insert an item in a queue, we follow the method whose result is indicated in Figure 3-20. We first create a new pair whose car is the item to be inserted and whose cdr is the empty list. If the queue was initially empty, we set the front and rear pointers of the queue to this new pair. Otherwise, we modify the final pair in the queue to point to the new pair, and also set the rear pointer to the new pair.

      -

      Figure 3.20: Result of using (insert-queue! q 'd) on the queue of Figure 3-19. +

      Figure 3.20: Result of using (insert-queue! q 'd) on the queue of Figure 3-19.

      @@ -518,11 +516,11 @@

      3.3.2 Representing Queues

      C.prompt("scheme-define-insert-queue", ["scheme-define-front-queue"]); -

      To delete the item at the front of the queue, we merely modify the front pointer so that it now points at the second item in the queue, which can be found by following the cdr pointer of the first item (see Figure 3-21):22 +

      To delete the item at the front of the queue, we merely modify the front pointer so that it now points at the second item in the queue, which can be found by following the cdr pointer of the first item (see Figure 3-21):22

      -

      Figure 3.21: Result of using (delete-queue! q) on the queue of Figure 3-20. +

      Figure 3.21: Result of using (delete-queue! q) on the queue of Figure 3-20.

      @@ -541,7 +539,7 @@

      3.3.2 Representing Queues

      -

      +

      Exercise 3.21: Ben Bitdiddle decides to test the queue implementation described above. He types in the procedures to the Lisp interpreter and proceeds to try them out:

      @@ -559,13 +557,13 @@

      3.3.2 Representing Queues

      C.prompt("scheme-test-queue", ["scheme-define-queue"]); -

      ``It's all wrong!'' he complains. ``The interpreter's response shows that the last item is inserted into the queue twice. And when I delete both items, the second b is still there, so the queue isn't empty, even though it's supposed to be.'' Eva Lu Ator suggests that Ben has misunderstood what is happening. ``It's not that the items are going into the queue twice,'' she explains. ``It's just that the standard Lisp printer doesn't know how to make sense of the queue representation. If you want to see the queue printed correctly, you'll have to define your own print procedure for queues.'' Explain what Eva Lu is talking about. In particular, show why Ben's examples produce the printed results that they do. Define a procedure print-queue that takes a queue as input and prints the sequence of items in the queue. +

      ``It's all wrong!'' he complains. ``The interpreter's response shows that the last item is inserted into the queue twice. And when I delete both items, the second b is still there, so the queue isn't empty, even though it's supposed to be.'' Eva Lu Ator suggests that Ben has misunderstood what is happening. ``It's not that the items are going into the queue twice,'' she explains. ``It's just that the standard Lisp printer doesn't know how to make sense of the queue representation. If you want to see the queue printed correctly, you'll have to define your own print procedure for queues.'' Explain what Eva Lu is talking about. In particular, show why Ben's examples produce the printed results that they do. Define a procedure print-queue that takes a queue as input and prints the sequence of items in the queue.

      -

      -

      Exercise 3.22: Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form +

      +

      Exercise 3.22: Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form

      (define (make-queue) @@ -579,21 +577,20 @@

      3.3.2 Representing Queues

      C.prompt("scheme-define-make-queue-dispatch"); -

      Complete the definition of make-queue and provide implementations of the queue operations using this representation. +

      Complete the definition of make-queue and provide implementations of the queue operations using this representation.

      -

      -

      Exercise 3.23: A deque ("double-ended queue") is a sequence in which items can be inserted and deleted at either the front or the rear. Operations on deques are the constructor make-deque, the predicate empty-deque?, selectors front-deque and rear-deque, and mutators front-insert-deque!, rear-insert-deque!, front-delete-deque!, and rear-delete-deque!. Show how to represent deques using pairs, and give implementations of the operations.23 All operations should be accomplished in $\Theta(1)$ steps. +

      +

      Exercise 3.23: A deque ("double-ended queue") is a sequence in which items can be inserted and deleted at either the front or the rear. Operations on deques are the constructor make-deque, the predicate empty-deque?, selectors front-deque and rear-deque, and mutators front-insert-deque!, rear-insert-deque!, front-delete-deque!, and rear-delete-deque!. Show how to represent deques using pairs, and give implementations of the operations.23 All operations should be accomplished in $\Theta(1)$ steps.

      - -

      3.3.3 Representing Tables

      +

      3.3.3 Representing Tables

      When we studied various ways of representing sets in Chapter 2, we mentioned in section 2.3.3 the task of maintaining a table of records indexed by identifying keys. In the implementation of data-directed programming in section 2.4.3, we made extensive use of two-dimensional tables, in which information is stored and retrieved using two keys. Here we see how to build tables as mutable list structures. -

      We first consider a one-dimensional table, in which each value is stored under a single key. We implement the table as a list of records, each of which is implemented as a pair consisting of a key and the associated value. The records are glued together to form a list by pairs whose cars point to successive records. These gluing pairs are called the backbone of the table. In order to have a place that we can change when we add a new record to the table, we build the table as a headed list . A headed list has a special backbone pair at the beginning, which holds a dummy ``record''---in this case the arbitrarily chosen symbol *table*. Figure 3-22 shows the box-and-pointer diagram for the table +

      We first consider a one-dimensional table, in which each value is stored under a single key. We implement the table as a list of records, each of which is implemented as a pair consisting of a key and the associated value. The records are glued together to form a list by pairs whose cars point to successive records. These gluing pairs are called the backbone of the table. In order to have a place that we can change when we add a new record to the table, we build the table as a headed list . A headed list has a special backbone pair at the beginning, which holds a dummy ``record''---in this case the arbitrarily chosen symbol *table*. Figure 3-22 shows the box-and-pointer diagram for the table

       a:  1
      @@ -606,7 +603,7 @@ 

      3.3.3 Representing Tables

      Figure 3.22: A table represented as a headed list.

      -

      To extract information from a table we use the lookup procedure, which takes a key as argument and returns the associated value (or false if there is no value stored under that key). Lookup is defined in terms of the assoc operation, which expects a key and a list of records as arguments. Note that assoc never sees the dummy record. Assoc returns the record that has the given key as its car.24 Lookup then checks to see that the resulting record returned by assoc is not false, and returns the value (the cdr) of the record. +

      To extract information from a table we use the lookup procedure, which takes a key as argument and returns the associated value (or false if there is no value stored under that key). Lookup is defined in terms of the assoc operation, which expects a key and a list of records as arguments. Note that assoc never sees the dummy record. Assoc returns the record that has the given key as its car.24 Lookup then checks to see that the resulting record returned by assoc is not false, and returns the value (the cdr) of the record.

      (define (lookup key table) @@ -629,7 +626,7 @@

      3.3.3 Representing Tables

      C.prompt("scheme-define-assoc"); -

      To insert a value in a table under a specified key, we first use assoc to see if there is already a record in the table with this key. If not, we form a new record by consing the key with the value, and insert this at the head of the table's list of records, after the dummy record. If there already is a record with this key, we set the cdr of this record to the designated new value. The header of the table provides us with a fixed location to modify in order to insert the new record.25 +

      To insert a value in a table under a specified key, we first use assoc to see if there is already a record in the table with this key. If not, we form a new record by consing the key with the value, and insert this at the head of the table's list of records, after the dummy record. If there already is a record with this key, we set the cdr of this record to the designated new value. The header of the table provides us with a fixed location to modify in order to insert the new record.25

      (define (insert! key value table) @@ -644,7 +641,7 @@

      3.3.3 Representing Tables

      C.prompt("scheme-define-insert-table", ["scheme-define-lookup", "scheme-define-assoc"]); -

      To construct a new table, we simply create a list containing the symbol *table*: +

      To construct a new table, we simply create a list containing the symbol *table*:

      (define (make-table) @@ -691,7 +688,7 @@

      Two-dimensional tables

      C.prompt("scheme-define-lookup-2table"); -

      To insert a new item under a pair of keys, we use assoc to see if there is a subtable stored under the first key. If not, we build a new subtable containing the single record (key-2, value) and insert it into the table under the first key. If a subtable already exists for the first key, we insert the new record into this subtable, using the insertion method for one-dimensional tables described above: +

      To insert a new item under a pair of keys, we use assoc to see if there is a subtable stored under the first key. If not, we build a new subtable containing the single record (key-2, value) and insert it into the table under the first key. If a subtable already exists for the first key, we insert the new record into this subtable, using the insertion method for one-dimensional tables described above:

      (define (insert! key-1 key-2 value table) @@ -715,7 +712,7 @@

      Two-dimensional tables

      Creating local tables

      -

      The lookup and insert! operations defined above take the table as an argument. This enables us to use programs that access more than one table. Another way to deal with multiple tables is to have separate lookup and insert! procedures for each table. We can do this by representing a table procedurally, as an object that maintains an internal table as part of its local state. When sent an appropriate message, this "table object" supplies the procedure with which to operate on the internal table. Here is a generator for two-dimensional tables represented in this fashion: +

      The lookup and insert! operations defined above take the table as an argument. This enables us to use programs that access more than one table. Another way to deal with multiple tables is to have separate lookup and insert! procedures for each table. We can do this by representing a table procedurally, as an object that maintains an internal table as part of its local state. When sent an appropriate message, this "table object" supplies the procedure with which to operate on the internal table. Here is a generator for two-dimensional tables represented in this fashion:

      (define (make-table) @@ -752,7 +749,7 @@

      Creating local tables

      C.prompt("scheme-define-make-2table", ["scheme-define-insert-2table"]); -

      Using make-table, we could implement the get and put operations used in section 2.4.3 for data-directed programming, as follows: +

      Using make-table, we could implement the get and put operations used in section 2.4.3 for data-directed programming, as follows:

      (define operation-table (make-table)) @@ -763,28 +760,28 @@

      Creating local tables

      C.prompt("scheme-define-get-put", ["scheme-define-make-2table"]); -

      Get takes as arguments two keys, and put takes as arguments two keys and a value. Both operations access the same local table, which is encapsulated within the object created by the call to make-table. +

      Get takes as arguments two keys, and put takes as arguments two keys and a value. Both operations access the same local table, which is encapsulated within the object created by the call to make-table. -

      -

      Exercise 3.24: In the table implementations above, the keys are tested for equality using equal? (called by assoc). This is not always the appropriate test. For instance, we might have a table with numeric keys in which we don't need an exact match to the number we're looking up, but only a number within some tolerance of it. Design a table constructor make-table that takes as an argument a same-key? procedure that will be used to test ``equality'' of keys. Make-table should return a dispatch procedure that can be used to access appropriate lookup and insert! procedures for a local table. +

      +

      Exercise 3.24: In the table implementations above, the keys are tested for equality using equal? (called by assoc). This is not always the appropriate test. For instance, we might have a table with numeric keys in which we don't need an exact match to the number we're looking up, but only a number within some tolerance of it. Design a table constructor make-table that takes as an argument a same-key? procedure that will be used to test ``equality'' of keys. Make-table should return a dispatch procedure that can be used to access appropriate lookup and insert! procedures for a local table.

      -

      -

      Exercise 3.25: Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table. +

      +

      Exercise 3.25: Generalizing one- and two-dimensional tables, show how to implement a table in which values are stored under an arbitrary number of keys and different values may be stored under different numbers of keys. The lookup and insert! procedures should take as input a list of keys used to access the table.

      -

      +

      Exercise 3.26: To search a table as implemented above, one needs to scan through the list of records. This is basically the unordered list representation of section 2.3.3. For large tables, it may be more efficient to structure the table in a different manner. Describe a table implementation where the (key, value) records are organized using a binary tree, assuming that keys can be ordered in some way (e.g., numerically or alphabetically). (Compare Exercise 2-66 of Chapter 2.)

      -

      -

      Exercise 3.27: Memoization (also called tabulation ) is a technique that enables a procedure to record, in a local table, values that have previously been computed. This technique can make a vast difference in the performance of a program. A memoized procedure maintains a table in which values of previous calls are stored using as keys the arguments that produced the values. When the memoized procedure is asked to compute a value, it first checks the table to see if the value is already there and, if so, just returns that value. Otherwise, it computes the new value in the ordinary way and stores this in the table. As an example of memoization, recall from section 1.2.2 the exponential process for computing Fibonacci numbers: +

      +

      Exercise 3.27: Memoization (also called tabulation ) is a technique that enables a procedure to record, in a local table, values that have previously been computed. This technique can make a vast difference in the performance of a program. A memoized procedure maintains a table in which values of previous calls are stored using as keys the arguments that produced the values. When the memoized procedure is asked to compute a value, it first checks the table to see if the value is already there and, if so, just returns that value. Otherwise, it computes the new value in the ordinary way and stores this in the table. As an example of memoization, recall from section 1.2.2 the exponential process for computing Fibonacci numbers:

      (define (fib n) @@ -827,21 +824,20 @@

      Creating local tables

      C.prompt("scheme-define-memoize", ["scheme-define-table"]); -

      Draw an environment diagram to analyze the computation of (memo-fib 3). Explain why memo-fib computes the nth Fibonacci number in a number of steps proportional to n. Would the scheme still work if we had simply defined memo-fib to be (memoize fib)?

      +

      Draw an environment diagram to analyze the computation of (memo-fib 3). Explain why memo-fib computes the nth Fibonacci number in a number of steps proportional to n. Would the scheme still work if we had simply defined memo-fib to be (memoize fib)?

      - -

      3.3.4 A Simulator for Digital Circuits

      +

      3.3.4 A Simulator for Digital Circuits

      Designing complex digital systems, such as computers, is an important engineering activity. Digital systems are constructed by interconnecting simple elements. Although the behavior of these individual elements is simple, networks of them can have very complex behavior. Computer simulation of proposed circuit designs is an important tool used by digital systems engineers. In this section we design a system for performing digital logic simulations. This system typifies a kind of program called an event-driven simulation , in which actions ("events") trigger further events that happen at a later time, which in turn trigger more events, and so so. -

      Our computational model of a circuit will be composed of objects that correspond to the elementary components from which the circuit is constructed. There are wires , which carry digital signals . A digital signal may at any moment have only one of two possible values, 0 and 1. There are also various types of digital function boxes , which connect wires carrying input signals to other output wires. Such boxes produce output signals computed from their input signals. The output signal is delayed by a time that depends on the type of the function box. For example, an inverter is a primitive function box that inverts its input. If the input signal to an inverter changes to 0, then one inverter-delay later the inverter will change its output signal to 1. If the input signal to an inverter changes to 1, then one inverter-delay later the inverter will change its output signal to 0. We draw an inverter symbolically as in Figure 3-24. An and-gate , also shown in Figure 3-24, is a primitive function box with two inputs and one output. It drives its output signal to a value that is the logical and of the inputs. That is, if both of its input signals become 1, then one and-gate-delay time later the and-gate will force its output signal to be 1; otherwise the output will be 0. An or-gate is a similar two-input primitive function box that drives its output signal to a value that is the logical or of the inputs. That is, the output will become 1 if at least one of the input signals is 1; otherwise the output will become 0. +

      Our computational model of a circuit will be composed of objects that correspond to the elementary components from which the circuit is constructed. There are wires , which carry digital signals . A digital signal may at any moment have only one of two possible values, 0 and 1. There are also various types of digital function boxes , which connect wires carrying input signals to other output wires. Such boxes produce output signals computed from their input signals. The output signal is delayed by a time that depends on the type of the function box. For example, an inverter is a primitive function box that inverts its input. If the input signal to an inverter changes to 0, then one inverter-delay later the inverter will change its output signal to 1. If the input signal to an inverter changes to 1, then one inverter-delay later the inverter will change its output signal to 0. We draw an inverter symbolically as in Figure 3-24. An and-gate , also shown in Figure 3-24, is a primitive function box with two inputs and one output. It drives its output signal to a value that is the logical and of the inputs. That is, if both of its input signals become 1, then one and-gate-delay time later the and-gate will force its output signal to be 1; otherwise the output will be 0. An or-gate is a similar two-input primitive function box that drives its output signal to a value that is the logical or of the inputs. That is, the output will become 1 if at least one of the input signals is 1; otherwise the output will become 0.

      Figure 3.24: Primitive functions in the digital logic simulator.

      -

      We can connect primitive functions together to construct more complex functions. To accomplish this we wire the outputs of some function boxes to the inputs of other function boxes. For example, the half-adder +

      We can connect primitive functions together to construct more complex functions. To accomplish this we wire the outputs of some function boxes to the inputs of other function boxes. For example, the half-adder

      circuit shown in Figure 3-25 consists of an or-gate, two and-gates, and an inverter. It takes two input signals, A and B, and has two output signals, S and C. S will become 1 whenever precisely one of A and B is 1, and C will become 1 whenever A and B are both 1. We can see from the figure that, because of the delays involved, the outputs may be generated at different times. Many of the difficulties in the design of digital circuits arise from this fact. @@ -852,7 +848,7 @@

      3.3.4 A Simulator for Digital Circuits

      We will now build a program for modeling the digital logic circuits we wish to study. The program will construct computational objects modeling the wires, which will ``hold'' the signals. Function boxes will be modeled by procedures that enforce the correct relationships among the signals. -

      One basic element of our simulation will be a procedure make-wire, which constructs wires. For example, we can construct six wires as follows: +

      One basic element of our simulation will be a procedure make-wire, which constructs wires. For example, we can construct six wires as follows:

      (define a (make-wire)) @@ -886,7 +882,7 @@

      3.3.4 A Simulator for Digital Circuits

      C.prompt("scheme-wire-up-wires"); -

      Better yet, we can explicitly name this operation by defining a procedure half-adder that constructs this circuit, given the four external wires to be attached to the half-adder: +

      Better yet, we can explicitly name this operation by defining a procedure half-adder that constructs this circuit, given the four external wires to be attached to the half-adder:

      (define (half-adder a b s c) @@ -901,7 +897,7 @@

      3.3.4 A Simulator for Digital Circuits

      C.prompt("scheme-define-half-adder"); -

      The advantage of making this definition is that we can use half-adder itself as a building block in creating more complex circuits. Figure 3-26, for example, shows a full-adder composed of two half-adders and an or-gate.26 We can construct a full-adder as follows: +

      The advantage of making this definition is that we can use half-adder itself as a building block in creating more complex circuits. Figure 3-26, for example, shows a full-adder composed of two half-adders and an or-gate.26 We can construct a full-adder as follows:

      (define (full-adder a b c-in sum c-out) @@ -917,7 +913,7 @@

      3.3.4 A Simulator for Digital Circuits

      C.prompt("scheme-define-full-adder", ["scheme-define-half-adder"]); -

      Having defined full-adder as a procedure, we can now use it as a building block for creating still more complex circuits. (For example, see Exercise 3-30.) +

      Having defined full-adder as a procedure, we can now use it as a building block for creating still more complex circuits. (For example, see Exercise 3-30.)

      @@ -972,9 +968,9 @@

      Primitive function boxes

    -

    In addition, we will make use of a procedure after-delay that takes a time delay and a procedure to be run and executes the given procedure after the given delay. +

    In addition, we will make use of a procedure after-delay that takes a time delay and a procedure to be run and executes the given procedure after the given delay. -

    Using these procedures, we can define the primitive digital logic functions. To connect an input to an output through an inverter, we use add-action! to associate with the input wire a procedure that will be run whenever the signal on the input wire changes value. The procedure computes the logical-not of the input signal, and then, after one inverter-delay, sets the output signal to be this new value: +

    Using these procedures, we can define the primitive digital logic functions. To connect an input to an output through an inverter, we use add-action! to associate with the input wire a procedure that will be run whenever the signal on the input wire changes value. The procedure computes the logical-not of the input signal, and then, after one inverter-delay, sets the output signal to be this new value:

    (define (inverter input output) @@ -995,9 +991,9 @@

    Primitive function boxes

    C.prompt("scheme-define-inverter"); -

    An and-gate is a little more complex. The action procedure must be run if either of the inputs to the gate changes. It computes the logical-and (using a procedure analogous to logical-not) of the values of the signals on the input wires and sets up a change to the new value to occur on the output wire after one and-gate-delay. +

    An and-gate is a little more complex. The action procedure must be run if either of the inputs to the gate changes. It computes the logical-and (using a procedure analogous to logical-not) of the values of the signals on the input wires and sets up a change to the new value to occur on the output wire after one and-gate-delay. -

    +
    (define (and-gate a1 a2 output) (define (and-action-procedure) (let ((new-value @@ -1013,16 +1009,16 @@

    Primitive function boxes

    C.prompt(); -
    -

    Exercise 3.28: Define an or-gate as a primitive function box. Your or-gate constructor should be similar to and-gate. +

    +

    Exercise 3.28: Define an or-gate as a primitive function box. Your or-gate constructor should be similar to and-gate.

    -
    -

    Exercise 3.29: Another way to construct an or-gate is as a compound digital logic device, built from and-gates and inverters. Define a procedure or-gate that accomplishes this. What is the delay time of the or-gate in terms of and-gate-delay and inverter-delay? +

    +

    Exercise 3.29: Another way to construct an or-gate is as a compound digital logic device, built from and-gates and inverters. Define a procedure or-gate that accomplishes this. What is the delay time of the or-gate in terms of and-gate-delay and inverter-delay?

    -
    -

    Exercise 3.30: Figure 3-27 shows a ripple-carry adder formed by stringing together n full-adders. This is the simplest form of parallel adder for adding two n-bit binary numbers. The inputs A_1, A_2, A_3, ..., A_n and B_1, B_2, B_3, ..., B_n are the two binary numbers to be added (each A_k and B_k is a 0 or a 1). The circuit generates S_1, S_2, S_3, ..., S_n, the n bits of the sum, and C, the carry from the addition. Write a procedure ripple-carry-adder that generates this circuit. The procedure should take as arguments three lists of n wires each---the A_k, the B_k, and the S_k---and also another wire C. The major drawback of the ripple-carry adder is the need to wait for the carry signals to propagate. What is the delay needed to obtain the complete output from an n-bit ripple-carry adder, expressed in terms of the delays for and-gates, or-gates, and inverters? +

    +

    Exercise 3.30: Figure 3-27 shows a ripple-carry adder formed by stringing together n full-adders. This is the simplest form of parallel adder for adding two n-bit binary numbers. The inputs A_1, A_2, A_3, ..., A_n and B_1, B_2, B_3, ..., B_n are the two binary numbers to be added (each A_k and B_k is a 0 or a 1). The circuit generates S_1, S_2, S_3, ..., S_n, the n bits of the sum, and C, the carry from the addition. Write a procedure ripple-carry-adder that generates this circuit. The procedure should take as arguments three lists of n wires each---the A_k, the B_k, and the S_k---and also another wire C. The major drawback of the ripple-carry adder is the need to wait for the carry signals to propagate. What is the delay needed to obtain the complete output from an n-bit ripple-carry adder, expressed in terms of the delays for and-gates, or-gates, and inverters?

    @@ -1046,7 +1042,7 @@

    Primitive function boxes

    Representing wires

    -

    A wire in our simulation will be a computational object with two local state variables: a signal-value (initially taken to be 0) and a collection of action-procedures to be run when the signal changes value. We implement the wire, using message-passing style, as a collection of local procedures together with a dispatch procedure that selects the appropriate local operation, just as we did with the simple bank-account object in section 3.1.1: +

    A wire in our simulation will be a computational object with two local state variables: a signal-value (initially taken to be 0) and a collection of action-procedures to be run when the signal changes value. We implement the wire, using message-passing style, as a collection of local procedures together with a dispatch procedure that selects the appropriate local operation, just as we did with the simple bank-account object in section 3.1.1:

    (define (make-wire) @@ -1072,7 +1068,7 @@

    Representing wires

    C.prompt("scheme-define-make-wire", ["scheme-define-call-each"]); -

    The local procedure set-my-signal! tests whether the new signal value changes the signal on the wire. If so, it runs each of the action procedures, using the following procedure call-each, which calls each of the items in a list of no-argument procedures: +

    The local procedure set-my-signal! tests whether the new signal value changes the signal on the wire. If so, it runs each of the action procedures, using the following procedure call-each, which calls each of the items in a list of no-argument procedures:

    (define (call-each procedures) @@ -1086,9 +1082,9 @@

    Representing wires

    C.prompt("scheme-define-call-each"); -

    The local procedure accept-action-procedure! adds the given procedure to the list of procedures to be run, and then runs the new procedure once. (See Exercise 3-31.) +

    The local procedure accept-action-procedure! adds the given procedure to the list of procedures to be run, and then runs the new procedure once. (See Exercise 3-31.) -

    With the local dispatch procedure set up as specified, we can provide the following procedures to access the local operations on wires:27 +

    With the local dispatch procedure set up as specified, we can provide the following procedures to access the local operations on wires:27

    (define (get-signal wire) @@ -1104,45 +1100,45 @@

    Representing wires

    C.prompt("scheme-define-get-set-signal-add-action"); -

    Wires, which have time-varying signals and may be incrementally attached to devices, are typical of mutable objects. We have modeled them as procedures with local state variables that are modified by assignment. When a new wire is created, a new set of state variables is allocated (by the let expression in make-wire) and a new dispatch procedure is constructed and returned, capturing the environment with the new state variables. +

    Wires, which have time-varying signals and may be incrementally attached to devices, are typical of mutable objects. We have modeled them as procedures with local state variables that are modified by assignment. When a new wire is created, a new set of state variables is allocated (by the let expression in make-wire) and a new dispatch procedure is constructed and returned, capturing the environment with the new state variables.

    The wires are shared among the various devices that have been connected to them. Thus, a change made by an interaction with one device will affect all the other devices attached to the wire. The wire communicates the change to its neighbors by calling the action procedures provided to it when the connections were established.

    The agenda

    -

    The only thing needed to complete the simulator is after-delay. The idea here is that we maintain a data structure, called an agenda , that contains a schedule of things to do. The following operations are defined for agendas: +

    The only thing needed to complete the simulator is after-delay. The idea here is that we maintain a data structure, called an agenda , that contains a schedule of things to do. The following operations are defined for agendas:

    • -

      (make-agenda) returns a new empty agenda. +

      (make-agenda) returns a new empty agenda.

    • -

      (empty-agenda? <agenda>) is true if the specified agenda is +

      (empty-agenda? <agenda>) is true if the specified agenda is empty.

    • -

      (first-agenda-item <agenda>) returns the first item on the +

      (first-agenda-item <agenda>) returns the first item on the agenda.

    • -

      (remove-first-agenda-item! <agenda>) modifies the agenda by +

      (remove-first-agenda-item! <agenda>) modifies the agenda by removing the first item.

    • -

      (add-to-agenda! <time> <action> <agenda>) modifies the agenda by adding the given action procedure to be run at the specified time. +

      (add-to-agenda! <time> <action> <agenda>) modifies the agenda by adding the given action procedure to be run at the specified time.

    • -

      (current-time <agenda>) returns the current simulation time. +

      (current-time <agenda>) returns the current simulation time.

    -

    The particular agenda that we use is denoted by the-agenda. The procedure after-delay adds new elements to the-agenda: +

    The particular agenda that we use is denoted by the-agenda. The procedure after-delay adds new elements to the-agenda:

    (define (after-delay delay action) @@ -1154,7 +1150,7 @@

    The agenda

    C.prompt("scheme-define-after-delay"); -

    The simulation is driven by the procedure propagate, which operates on the-agenda, executing each procedure on the agenda in sequence. In general, as the simulation runs, new items will be added to the agenda, and propagate will continue the simulation as long as there are items on the agenda: +

    The simulation is driven by the procedure propagate, which operates on the-agenda, executing each procedure on the agenda in sequence. In general, as the simulation runs, new items will be added to the agenda, and propagate will continue the simulation as long as there are items on the agenda:

    (define (propagate) @@ -1173,7 +1169,7 @@

    A sample simulation

    The following procedure, which places a ``probe'' on a wire, shows the simulator in action. The probe tells the wire that, whenever its signal changes value, it should print the new signal value, together with the current time and a name that identifies the wire: -

    +
    (define (probe name wire) (add-action! wire (lambda () @@ -1185,7 +1181,7 @@

    A sample simulation

    (display (get-signal wire)))))

    We begin by initializing the agenda and specifying delays for the primitive function boxes: @@ -1218,7 +1214,7 @@

    A sample simulation

    C.prompt("scheme-define-four-wires"); -

    Next we connect the wires in a half-adder circuit (as in Figure 3-25), set the signal on input-1 to 1, and run the simulation: +

    Next we connect the wires in a half-adder circuit (as in Figure 3-25), set the signal on input-1 to 1, and run the simulation:

    (half-adder input-1 input-2 sum carry) @@ -1235,7 +1231,7 @@

    A sample simulation

    C.prompt("scheme-connect-wires"); -

    The sum signal changes to 1 at time 8. We are now eight time units from the beginning of the simulation. At this point, we can set the signal on input-2 to 1 and allow the values to propagate: +

    The sum signal changes to 1 at time 8. We are now eight time units from the beginning of the simulation. At this point, we can set the signal on input-2 to 1 and allow the values to propagate:

    (set-signal! input-2 1) @@ -1250,12 +1246,12 @@

    A sample simulation

    C.prompt("scheme-set-signal"); -

    The carry changes to 1 at time 11 and the sum changes to 0 at time 16. +

    The carry changes to 1 at time 11 and the sum changes to 0 at time 16. -

    -

    Exercise 3.31: The internal procedure accept-action-procedure! defined in make-wire specifies that when a new action procedure is added to a wire, the procedure is immediately run. Explain why this initialization is necessary. In particular, trace through the half-adder example in the paragraphs above and say how the system's response would differ if we had defined accept-action-procedure! as +

    +

    Exercise 3.31: The internal procedure accept-action-procedure! defined in make-wire specifies that when a new action procedure is added to a wire, the procedure is immediately run. Explain why this initialization is necessary. In particular, trace through the half-adder example in the paragraphs above and say how the system's response would differ if we had defined accept-action-procedure! as -

    +
    (define (accept-action-procedure! proc) (set! action-procedures (cons proc action-procedures)))
    @@ -1268,7 +1264,7 @@

    Implementing the agenda

    Finally, we give details of the agenda data structure, which holds the procedures that are scheduled for future execution. -

    The agenda is made up of time segments . Each time segment is a pair consisting of a number (the time) and a queue (see Exercise 3-32) that holds the procedures that are scheduled to be run during that time segment. +

    The agenda is made up of time segments . Each time segment is a pair consisting of a number (the time) and a queue (see Exercise 3-32) that holds the procedures that are scheduled to be run during that time segment.

    (define (make-time-segment time queue) @@ -1284,9 +1280,9 @@

    Implementing the agenda

    We will operate on the time-segment queues using the queue operations described in section 3.3.2. -

    The agenda itself is a one-dimensional table of time segments. It differs from the tables described in section 3.3.3 in that the segments will be sorted in order of increasing time. In addition, we store the current time +

    The agenda itself is a one-dimensional table of time segments. It differs from the tables described in section 3.3.3 in that the segments will be sorted in order of increasing time. In addition, we store the current time -

    (i.e., the time of the last action that was processed) at the head of the agenda. A newly constructed agenda has no time segments and has a current time of 0:28 +

    (i.e., the time of the last action that was processed) at the head of the agenda. A newly constructed agenda has no time segments and has a current time of 0:28

    (define (make-agenda) (list 0)) @@ -1353,9 +1349,9 @@

    Implementing the agenda

    C.prompt("scheme-define-add-to-agenda"); -

    The procedure that removes the first item from the agenda deletes the item at the front of the queue in the first time segment. If this deletion makes the time segment empty, we remove it from the list of segments:29 +

    The procedure that removes the first item from the agenda deletes the item at the front of the queue in the first time segment. If this deletion makes the time segment empty, we remove it from the list of segments:29 -

    +
    (define (remove-first-agenda-item! agenda) (let ((q (segment-queue (first-segment agenda)))) (delete-queue! q) @@ -1366,8 +1362,8 @@

    Implementing the agenda

    C.prompt(); -

    The first agenda item is found at the head of the queue in the first time segment. Whenever we extract an item, we also update the current time:30 -

    +

    The first agenda item is found at the head of the queue in the first time segment. Whenever we extract an item, we also update the current time:30 +

    (define (first-agenda-item agenda) (if (empty-agenda? agenda) (error "Agenda is empty -- FIRST-AGENDA-ITEM") @@ -1379,13 +1375,12 @@

    Implementing the agenda

    C.prompt(); -
    +

    Exercise 3.32: The procedures to be run during each time segment of the agenda are kept in a queue. Thus, the procedures for each segment are called in the order in which they were added to the agenda (first in, first out). Explain why this order must be used. In particular, trace the behavior of an and-gate whose inputs change from 0,1 to 1,0 in the same segment and say how the behavior would differ if we stored a segment's procedures in an ordinary list, adding and removing procedures only at the front (last in, first out).

    - -

    3.3.5 Propagation of Constraints

    +

    3.3.5 Propagation of Constraints

    Computer programs are traditionally organized as one-directional computations, which perform operations on prespecified arguments to produce desired outputs. On the other hand, we often model systems in terms of relations among quantities. For example, a mathematical model of a mechanical structure might include the information that the deflection d of a metal rod is related to the force f on the rod, the length L of the rod, the cross-sectional area A, and the elastic modulus E via the equation @@ -1393,11 +1388,11 @@

    3.3.5 Propagation of Constraints

    dAE = FL
    -

    Such an equation is not one-directional. Given any four of the quantities, we can use it to compute the fifth. Yet translating the equation into a traditional computer language would force us to choose one of the quantities to be computed in terms of the other four. Thus, a procedure for computing the area A could not be used to compute the deflection d, even though the computations of A and d arise from the same equation.31 +

    Such an equation is not one-directional. Given any four of the quantities, we can use it to compute the fifth. Yet translating the equation into a traditional computer language would force us to choose one of the quantities to be computed in terms of the other four. Thus, a procedure for computing the area A could not be used to compute the deflection d, even though the computations of A and d arise from the same equation.31 -

    In this section, we sketch the design of a language that enables us to work in terms of relations themselves. The primitive elements of the language are primitive constraints , which state that certain relations hold between quantities. For example, (adder a b c) specifies that the quantities a, b, and c must be related by the equation a + b = c, (multiplier x y z) expresses the constraint xy = z, and (constant 3.14 x) says that the value of x must be 3.14. +

    In this section, we sketch the design of a language that enables us to work in terms of relations themselves. The primitive elements of the language are primitive constraints , which state that certain relations hold between quantities. For example, (adder a b c) specifies that the quantities a, b, and c must be related by the equation a + b = c, (multiplier x y z) expresses the constraint xy = z, and (constant 3.14 x) says that the value of x must be 3.14. -

    Our language provides a means of combining primitive constraints in order to express more complex relations. We combine constraints by constructing constraint networks , in which constraints are joined by connectors . A connector is an object that ``holds'' a value that may participate in one or more constraints. For example, we know that the relationship between Fahrenheit and Celsius temperatures is +

    Our language provides a means of combining primitive constraints in order to express more complex relations. We combine constraints by constructing constraint networks , in which constraints are joined by connectors . A connector is an object that ``holds'' a value that may participate in one or more constraints. For example, we know that the relationship between Fahrenheit and Celsius temperatures is $$ 9C = 5(F - 32) @@ -1417,7 +1412,7 @@

    3.3.5 Propagation of Constraints

    Using the constraint system

    -

    To use the constraint system to carry out the temperature computation outlined above, we first create two connectors, C and F, by calling the constructor make-connector, and link C and F in an appropriate network: +

    To use the constraint system to carry out the temperature computation outlined above, we first create two connectors, C and F, by calling the constructor make-connector, and link C and F in an appropriate network:

    (define C (make-connector)) @@ -1450,9 +1445,9 @@

    Using the constraint system

    C.prompt("scheme-define-celsius-fahrenheit-converter"); -

    This procedure creates the internal connectors u, v, w, x, and y, and links them as shown in Figure 3-28 using the primitive constraint constructors adder, multiplier, and constant. Just as with the digital-circuit simulator of section 3.3.4, expressing these combinations of primitive elements in terms of procedures automatically provides our language with a means of abstraction for compound objects. +

    This procedure creates the internal connectors u, v, w, x, and y, and links them as shown in Figure 3-28 using the primitive constraint constructors adder, multiplier, and constant. Just as with the digital-circuit simulator of section 3.3.4, expressing these combinations of primitive elements in terms of procedures automatically provides our language with a means of abstraction for compound objects. -

    To watch the network in action, we can place probes on the connectors C and F, using a probe procedure similar to the one we used to monitor wires in section 3.3.4. Placing a probe on a connector will cause a message to be printed whenever the connector is given a value: +

    To watch the network in action, we can place probes on the connectors C and F, using a probe procedure similar to the one we used to monitor wires in section 3.3.4. Placing a probe on a connector will cause a message to be printed whenever the connector is given a value:

    (probe "Celsius temp" C) @@ -1462,7 +1457,7 @@

    Using the constraint system

    C.prompt("scheme-probe"); -

    Next we set the value of C to 25. (The third argument to set-value! tells C that this directive comes from the user.) +

    Next we set the value of C to 25. (The third argument to set-value! tells C that this directive comes from the user.)

    (set-value! C 25 'user) @@ -1474,9 +1469,9 @@

    Using the constraint system

    C.prompt("scheme-set-C"); -

    The probe on C awakens and reports the value. C also propagates its value through the network as described above. This sets F to 77, which is reported by the probe on F. +

    The probe on C awakens and reports the value. C also propagates its value through the network as described above. This sets F to 77, which is reported by the probe on F. -

    Now we can try to set F to a new value, say 212: +

    Now we can try to set F to a new value, say 212:

    (set-value! F 212 'user) @@ -1486,7 +1481,7 @@

    Using the constraint system

    C.prompt("scheme-set-F"); -

    The connector complains that it has sensed a contradiction: Its value is 77, and someone is trying to set it to 212. If we really want to reuse the network with new values, we can tell C to forget its old value: +

    The connector complains that it has sensed a contradiction: Its value is 77, and someone is trying to set it to 212. If we really want to reuse the network with new values, we can tell C to forget its old value:

    (forget-value! C 'user) @@ -1498,9 +1493,9 @@

    Using the constraint system

    C.prompt("scheme-forget-value"); -

    C finds that the user, who set its value originally, is now retracting that value, so C agrees to lose its value, as shown by the probe, and informs the rest of the network of this fact. This information eventually propagates to F, which now finds that it has no reason for continuing to believe that its own value is 77. Thus, F also gives up its value, as shown by the probe. +

    C finds that the user, who set its value originally, is now retracting that value, so C agrees to lose its value, as shown by the probe, and informs the rest of the network of this fact. This information eventually propagates to F, which now finds that it has no reason for continuing to believe that its own value is 77. Thus, F also gives up its value, as shown by the probe. -

    Now that F has no value, we are free to set it to 212: +

    Now that F has no value, we are free to set it to 212:

    (set-value! F 212 'user) @@ -1512,7 +1507,7 @@

    Using the constraint system

    C.prompt("scheme-set-F-212"); -

    This new value, when propagated through the network, forces C to have a value of 100, and this is registered by the probe on C. Notice that the very same network is being used to compute C given F and to compute F given C. This nondirectionality of computation is the distinguishing feature of constraint-based systems. +

    This new value, when propagated through the network, forces C to have a value of 100, and this is registered by the probe on C. Notice that the very same network is being used to compute C given F and to compute F given C. This nondirectionality of computation is the distinguishing feature of constraint-based systems.

    Implementing the constraint system

    @@ -1523,33 +1518,33 @@

    Implementing the constraint system

    • -

      (has-value? <connector>) tells whether the connector has a value. +

      (has-value? <connector>) tells whether the connector has a value.

    • -

      (get-value <connector>) returns the connector's current value. +

      (get-value <connector>) returns the connector's current value.

    • -

      (set-value! <connector> <new-value> <informant>) +

      (set-value! <connector> <new-value> <informant>) indicates that the informant is requesting the connector to set its value to the new value.

    • -

      (forget-value! <connector> <retractor>) tells the connector +

      (forget-value! <connector> <retractor>) tells the connector that the retractor is requesting it to forget its value.

    • -

      (connect <connector> <new-constraint>) tells the connector to participate in the new constraint. +

      (connect <connector> <new-constraint>) tells the connector to participate in the new constraint.

    -

    The connectors communicate with the constraints by means of the procedures inform-about-value, which tells the given constraint that the connector has a value, and inform-about-no-value, which tells the constraint that the connector has lost its value. +

    The connectors communicate with the constraints by means of the procedures inform-about-value, which tells the given constraint that the connector has a value, and inform-about-no-value, which tells the constraint that the connector has lost its value. -

    Adder constructs an adder constraint among summand connectors a1 and a2 and a sum connector. An adder is implemented as a procedure with local state (the procedure me below): +

    Adder constructs an adder constraint among summand connectors a1 and a2 and a sum connector. An adder is implemented as a procedure with local state (the procedure me below):

    (define (adder a1 a2 sum) @@ -1587,7 +1582,7 @@

    Implementing the constraint system

    C.prompt("scheme-define-adder"); -

    Adder connects the new adder to the designated connectors and returns it as its value. The procedure me, which represents the adder, acts as a dispatch to the local procedures. The following ``syntax interfaces'' (see footnote Footnote 27 in section 3.3.4) are used in conjunction with the dispatch: +

    Adder connects the new adder to the designated connectors and returns it as its value. The procedure me, which represents the adder, acts as a dispatch to the local procedures. The following ``syntax interfaces'' (see footnote Footnote 27 in section 3.3.4) are used in conjunction with the dispatch:

    (define (inform-about-value constraint) @@ -1600,9 +1595,9 @@

    Implementing the constraint system

    C.prompt("scheme-define-inform-about-value"); -

    The adder's local procedure process-new-value is called when the adder is informed that one of its connectors has a value. The adder first checks to see if both a1 and a2 have values. If so, it tells sum to set its value to the sum of the two addends. The informant argument to set-value! is me, which is the adder object itself. If a1 and a2 do not both have values, then the adder checks to see if perhaps a1 and sum have values. If so, it sets a2 to the difference of these two. Finally, if a2 and sum have values, this gives the adder enough information to set a1. If the adder is told that one of its connectors has lost a value, it requests that all of its connectors now lose their values. (Only those values that were set by this adder are actually lost.) Then it runs process-new-value. The reason for this last step is that one or more connectors may still have a value (that is, a connector may have had a value that was not originally set by the adder), and these values may need to be propagated back through the adder. +

    The adder's local procedure process-new-value is called when the adder is informed that one of its connectors has a value. The adder first checks to see if both a1 and a2 have values. If so, it tells sum to set its value to the sum of the two addends. The informant argument to set-value! is me, which is the adder object itself. If a1 and a2 do not both have values, then the adder checks to see if perhaps a1 and sum have values. If so, it sets a2 to the difference of these two. Finally, if a2 and sum have values, this gives the adder enough information to set a1. If the adder is told that one of its connectors has lost a value, it requests that all of its connectors now lose their values. (Only those values that were set by this adder are actually lost.) Then it runs process-new-value. The reason for this last step is that one or more connectors may still have a value (that is, a connector may have had a value that was not originally set by the adder), and these values may need to be propagated back through the adder. -

    A multiplier is very similar to an adder. It will set its product to 0 if either of the factors is 0, even if the other factor is not known. +

    A multiplier is very similar to an adder. It will set its product to 0 if either of the factors is 0, even if the other factor is not known.

    (define (multiplier m1 m2 product) @@ -1643,7 +1638,7 @@

    Implementing the constraint system

    C.prompt("scheme-define-multiplier"); -

    A constant constructor simply sets the value of the designated connector. Any I-have-a-value or I-lost-my-value message sent to the constant box will produce an error. +

    A constant constructor simply sets the value of the designated connector. Any I-have-a-value or I-lost-my-value message sent to the constant box will produce an error.

    (define (constant value connector) @@ -1659,7 +1654,7 @@

    Implementing the constraint system

    Finally, a probe prints a message about the setting or unsetting of the designated connector: -

    +
    (define (probe name connector) (define (print-probe value) (newline) @@ -1682,12 +1677,12 @@

    Implementing the constraint system

    me)

    Representing connectors

    -

    A connector is represented as a procedural object with local state variables value, the current value of the connector; informant, the object that set the connector's value; and constraints, a list of the constraints in which the connector participates. +

    A connector is represented as a procedural object with local state variables value, the current value of the connector; informant, the object that set the connector's value; and constraints, a list of the constraints in which the connector participates.

    (define (make-connector) @@ -1731,7 +1726,7 @@

    Representing connectors

    C.prompt("scheme-define-make-connector"); -

    The connector's local procedure set-my-value is called when there is a request to set the connector's value. If the connector does not currently have a value, it will set its value and remember as informant the constraint that requested the value to be set.32 Then the connector will notify all of its participating constraints except the constraint that requested the value to be set. This is accomplished using the following iterator, which applies a designated procedure to all items in a list except a given one: +

    The connector's local procedure set-my-value is called when there is a request to set the connector's value. If the connector does not currently have a value, it will set its value and remember as informant the constraint that requested the value to be set.32 Then the connector will notify all of its participating constraints except the constraint that requested the value to be set. This is accomplished using the following iterator, which applies a designated procedure to all items in a list except a given one:

    (define (for-each-except exception procedure list) @@ -1746,11 +1741,11 @@

    Representing connectors

    C.prompt("scheme-define-for-each-except"); -

    If a connector is asked to forget its value, it runs the local procedure forget-my-value, which first checks to make sure that the request is coming from the same object that set the value originally. If so, the connector informs its associated constraints about the loss of the value. +

    If a connector is asked to forget its value, it runs the local procedure forget-my-value, which first checks to make sure that the request is coming from the same object that set the value originally. If so, the connector informs its associated constraints about the loss of the value. -

    The local procedure connect adds the designated new constraint to the list of constraints if it is not already in that list. Then, if the connector has a value, it informs the new constraint of this fact. +

    The local procedure connect adds the designated new constraint to the list of constraints if it is not already in that list. Then, if the connector has a value, it informs the new constraint of this fact. -

    The connector's procedure me serves as a dispatch to the other internal procedures and also represents the connector as an object. The following procedures provide a syntax interface for the dispatch: +

    The connector's procedure me serves as a dispatch to the other internal procedures and also represents the connector as an object. The following procedures provide a syntax interface for the dispatch:

    (define (has-value? connector) @@ -1772,12 +1767,12 @@

    Representing connectors

    C.prompt("scheme-define-connector-helpers"); -
    -

    Exercise 3.33: Using primitive multiplier, adder, and constant constraints, define a procedure averager that takes three connectors a, b, and c as inputs and establishes the constraint that the value of c is the average of the values of a and b. +

    +

    Exercise 3.33: Using primitive multiplier, adder, and constant constraints, define a procedure averager that takes three connectors a, b, and c as inputs and establishes the constraint that the value of c is the average of the values of a and b.

    -
    -

    Exercise 3.34: Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier: +

    +

    Exercise 3.34: Louis Reasoner wants to build a squarer, a constraint device with two terminals such that the value of connector b on the second terminal will always be the square of the value a on the first terminal. He proposes the following simple device made from a multiplier:

    (define (squarer a b) @@ -1792,7 +1787,7 @@

    Representing connectors

    -

    +

    Exercise 3.35: Ben Bitdiddle tells Louis that one way to avoid the trouble in Exercise 3-34 is to define a squarer as a new primitive constraint. Fill in the missing portions in Ben's outline for a procedure to implement such a constraint:

    @@ -1801,11 +1796,11 @@

    Representing connectors

    (if (has-value? b) (if (< (get-value b) 0) (error "square less than 0 -- SQUARER" (get-value b)) - ) - )) - (define (process-forget-value) ) - (define (me request) ) - ; + <alternative1>) + <alternative2>)) + (define (process-forget-value) <body1>) + (define (me request) <body2>) + ; <rest of definition> me)
    -

    At some time during evaluation of the set-value!, the following expression from the connector's local procedure is evaluated: +

    At some time during evaluation of the set-value!, the following expression from the connector's local procedure is evaluated:

    (for-each-except setter inform-about-value constraints) @@ -1841,8 +1836,8 @@

    Representing connectors

    -

    -

    Exercise 3.37: The celsius-fahrenheit-converter procedure is cumbersome when compared with a more expression-oriented style of definition, such as +

    +

    Exercise 3.37: The celsius-fahrenheit-converter procedure is cumbersome when compared with a more expression-oriented style of definition, such as

    (define (celsius-fahrenheit-converter x) @@ -1857,7 +1852,7 @@

    Representing connectors

    C.prompt("scheme-define-cfc-op"); -

    Here c+, c*, etc. are the ``constraint'' versions of the arithmetic operations. For example, c+ takes two connectors as arguments and returns a connector that is related to these by an adder constraint: +

    Here c+, c*, etc. are the ``constraint'' versions of the arithmetic operations. For example, c+ takes two connectors as arguments and returns a connector that is related to these by an adder constraint:

    (define (c+ x y) @@ -1869,7 +1864,7 @@

    Representing connectors

    C.prompt("scheme-define-cp"); -

    Define analogous procedures c-, c*, c/, and cv (constant value) that enable us to define compound constraints as in the converter example above.33 +

    Define analogous procedures c-, c*, c/, and cv (constant value) that enable us to define compound constraints as in the converter example above.33

    @@ @@ -1879,11 +1874,11 @@

    Representing connectors

    -

    17 We see from this that mutation operations on lists can create ``garbage'' that is not part of any accessible structure. We will see in section 5.3.2 that Lisp memory-management systems include a garbage collector, which identifies and recycles the memory space used by unneeded pairs. +

    17 We see from this that mutation operations on lists can create ``garbage'' that is not part of any accessible structure. We will see in section 5.3.2 that Lisp memory-management systems include a garbage collector, which identifies and recycles the memory space used by unneeded pairs.

    -

    18 Get-new-pair is one of the operations that must be implemented as part of the memory management required by a Lisp implementation. We will discuss this in section 5.3.1. +

    18 Get-new-pair is one of the operations that must be implemented as part of the memory management required by a Lisp implementation. We will discuss this in section 5.3.1.

    @@ -1891,7 +1886,7 @@

    Representing connectors

    -

    20 The subtleties of dealing with sharing of mutable data objects reflect the underlying issues of ``sameness'' and ``change'' that were raised in section 3.1.3. We mentioned there that admitting change to our language requires that a compound object must have an ``identity'' that is something different from the pieces from which it is composed. In Lisp, we consider this ``identity'' to be the quality that is tested by eq?, i.e., by equality of pointers. Since in most Lisp implementations a pointer is essentially a memory address, we are ``solving the problem'' of defining the identity of objects by stipulating that a data object ``itself'' is the information stored in some particular set of memory locations in the computer. This suffices for simple Lisp programs, but is hardly a general way to resolve the issue of ``sameness'' in computational models. +

    20 The subtleties of dealing with sharing of mutable data objects reflect the underlying issues of ``sameness'' and ``change'' that were raised in section 3.1.3. We mentioned there that admitting change to our language requires that a compound object must have an ``identity'' that is something different from the pieces from which it is composed. In Lisp, we consider this ``identity'' to be the quality that is tested by eq?, i.e., by equality of pointers. Since in most Lisp implementations a pointer is essentially a memory address, we are ``solving the problem'' of defining the identity of objects by stipulating that a data object ``itself'' is the information stored in some particular set of memory locations in the computer. This suffices for simple Lisp programs, but is hardly a general way to resolve the issue of ``sameness'' in computational models.

    @@ -1923,11 +1918,11 @@

    Representing connectors

    -

    28 The agenda is a headed list, like the tables in section 3.3.3, but since the list is headed by the time, we do not need an additional dummy header (such as the *table* symbol used with tables). +

    28 The agenda is a headed list, like the tables in section 3.3.3, but since the list is headed by the time, we do not need an additional dummy header (such as the *table* symbol used with tables).

    -

    29 Observe that the if expression in this procedure has no expression. Such a ``one-armed if statement'' is used to decide whether to do something, rather than to select between two expressions. An if expression returns an unspecified value if the predicate is false and there is no . +

    29 Observe that the if expression in this procedure has no <alternative> expression. Such a ``one-armed if statement'' is used to decide whether to do something, rather than to select between two expressions. An if expression returns an unspecified value if the predicate is false and there is no <alternative>.

    diff --git a/content/3-4-concurrency.content.html b/content/3-4-concurrency.content.html index 525ff1f..386dd41 100644 --- a/content/3-4-concurrency.content.html +++ b/content/3-4-concurrency.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

    @@ -14,9 +14,9 @@

    Concurrency: Time Is of the Essence

    We've seen the power of computational objects with local state as tools for modeling. Yet, as section 3.1.3 warned, this power extracts a price: the loss of referential transparency, giving rise to a thicket of questions about sameness and change, and the need to abandon the substitution model of evaluation in favor of the more intricate environment model. -

    The central issue lurking beneath the complexity of state, sameness, and change is that by introducing assignment we are forced to admit time into our computational models. Before we introduced assignment, all our programs were timeless, in the sense that any expression that has a value always has the same value. In contrast, recall the example of modeling withdrawals from a bank account and returning the resulting balance, introduced at the beginning of section 3.1.1: +

    The central issue lurking beneath the complexity of state, sameness, and change is that by introducing assignment we are forced to admit time into our computational models. Before we introduced assignment, all our programs were timeless, in the sense that any expression that has a value always has the same value. In contrast, recall the example of modeling withdrawals from a bank account and returning the resulting balance, introduced at the beginning of section 3.1.1: -

    +
    (withdraw 25) 75 @@ -27,24 +27,23 @@

    Concurrency: Time Is of the Essence

    C.prompt(); -

    Here successive evaluations of the same expression yield different values. This behavior arises from the fact that the execution of assignment statements (in this case, assignments to the variable balance) delineates moments in time when values change. The result of evaluating an expression depends not only on the expression itself, but also on whether the evaluation occurs before or after these moments. Building models in terms of computational objects with local state forces us to confront time as an essential concept in programming. +

    Here successive evaluations of the same expression yield different values. This behavior arises from the fact that the execution of assignment statements (in this case, assignments to the variable balance) delineates moments in time when values change. The result of evaluating an expression depends not only on the expression itself, but also on whether the evaluation occurs before or after these moments. Building models in terms of computational objects with local state forces us to confront time as an essential concept in programming. -

    We can go further in structuring computational models to match our perception of the physical world. Objects in the world do not change one at a time in sequence. Rather we perceive them as acting concurrently ---all at once. So it is often natural to model systems as collections of computational processes that execute concurrently. Just as we can make our programs modular by organizing models in terms of objects with separate local state, it is often appropriate to divide computational models into parts that evolve separately and concurrently. Even if the programs are to be executed on a sequential computer, the practice of writing programs as if they were to be executed concurrently forces the programmer to avoid inessential timing constraints and thus makes programs more modular. +

    We can go further in structuring computational models to match our perception of the physical world. Objects in the world do not change one at a time in sequence. Rather we perceive them as acting concurrently ---all at once. So it is often natural to model systems as collections of computational processes that execute concurrently. Just as we can make our programs modular by organizing models in terms of objects with separate local state, it is often appropriate to divide computational models into parts that evolve separately and concurrently. Even if the programs are to be executed on a sequential computer, the practice of writing programs as if they were to be executed concurrently forces the programmer to avoid inessential timing constraints and thus makes programs more modular. -

    In addition to making programs more modular, concurrent computation can provide a speed advantage over sequential computation. Sequential computers execute only one operation at a time, so the amount of time it takes to perform a task is proportional to the total number of operations performed.@footnote{Most real processors actually execute a few operations at a time, following a strategy called pipelining . Although this technique greatly improves the effective utilization of the hardware, it is used only to speed up the execution of a sequential instruction stream, while retaining the behavior of the sequential program.} However, if it is possible to decompose a problem into pieces that are relatively independent and need to communicate only rarely, it may be possible to allocate pieces to separate computing processors, producing a speed advantage proportional to the number of processors available. +

    In addition to making programs more modular, concurrent computation can provide a speed advantage over sequential computation. Sequential computers execute only one operation at a time, so the amount of time it takes to perform a task is proportional to the total number of operations performed.@footnote{Most real processors actually execute a few operations at a time, following a strategy called pipelining . Although this technique greatly improves the effective utilization of the hardware, it is used only to speed up the execution of a sequential instruction stream, while retaining the behavior of the sequential program.} However, if it is possible to decompose a problem into pieces that are relatively independent and need to communicate only rarely, it may be possible to allocate pieces to separate computing processors, producing a speed advantage proportional to the number of processors available.

    Unfortunately, the complexities introduced by assignment become even more problematic in the presence of concurrency. The fact of concurrent execution, either because the world operates in parallel or because our computers do, entails additional complexity in our understanding of time. - -

    3.4.1 The Nature of Time in Concurrent Systems

    +

    3.4.1 The Nature of Time in Concurrent Systems

    -

    On the surface, time seems straightforward. It is an ordering imposed on events.@footnote{To quote some graffiti seen on a Cambridge building wall: ``Time is a device that was invented to keep everything from happening at once.''} For any events A and B, either A occurs before B, A and B are simultaneous, or A occurs after B. For instance, returning to the bank account example, suppose that Peter withdraws $10 and Paul withdraws $25 from a joint account that initially contains $100, leaving $65 in the account. Depending on the order of the two withdrawals, the sequence of balances in the account is either $100 -> $90 -> $65 or $100 -> $75 -> $65. In a computer implementation of the banking system, this changing sequence of balances could be modeled by successive assignments to a variable balance. +

    On the surface, time seems straightforward. It is an ordering imposed on events.@footnote{To quote some graffiti seen on a Cambridge building wall: ``Time is a device that was invented to keep everything from happening at once.''} For any events A and B, either A occurs before B, A and B are simultaneous, or A occurs after B. For instance, returning to the bank account example, suppose that Peter withdraws $10 and Paul withdraws $25 from a joint account that initially contains $100, leaving $65 in the account. Depending on the order of the two withdrawals, the sequence of balances in the account is either $100 -> $90 -> $65 or $100 -> $75 -> $65. In a computer implementation of the banking system, this changing sequence of balances could be modeled by successive assignments to a variable balance.

    In complex situations, however, such a view can be problematic. Suppose that Peter and Paul, and other people besides, are accessing the same bank account through a network of banking machines distributed all over the world. The actual sequence of balances in the account will depend critically on the detailed timing of the accesses and the details of the communication among the machines. -

    This indeterminacy in the order of events can pose serious problems in the design of concurrent systems. For instance, suppose that the withdrawals made by Peter and Paul are implemented as two separate processes sharing a common variable balance, each process specified by the procedure given in section 3.1.1: +

    This indeterminacy in the order of events can pose serious problems in the design of concurrent systems. For instance, suppose that the withdrawals made by Peter and Paul are implemented as two separate processes sharing a common variable balance, each process specified by the procedure given in section 3.1.1: -

    +
    (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) @@ -59,22 +58,22 @@

    3.4.1 The Nature of Time in Concurrent Systems

    Things can be worse still. Consider the expression -

    +
    (set! balance (- balance amount))
    -

    executed as part of each withdrawal process. This consists of three steps: (1) accessing the value of the balance variable; (2) computing the new balance; (3) setting balance to this new value. If Peter and Paul's withdrawals execute this statement concurrently, then the two withdrawals might interleave the order in which they access balance and set it to the new value. +

    executed as part of each withdrawal process. This consists of three steps: (1) accessing the value of the balance variable; (2) computing the new balance; (3) setting balance to this new value. If Peter and Paul's withdrawals execute this statement concurrently, then the two withdrawals might interleave the order in which they access balance and set it to the new value. -

    The timing diagram in Figure 3-29 depicts an order of events where balance starts at 100, Peter withdraws 10, Paul withdraws 25, and yet the final value of balance is 75. As shown in the diagram, the reason for this anomaly is that Paul's assignment of 75 to balance is made under the assumption that the value of balance to be decremented is 100. That assumption, however, became invalid when Peter changed balance to 90. This is a catastrophic failure for the banking system, because the total amount of money in the system is not conserved. Before the transactions, the total amount of money was $100. Afterwards, Peter has $10, Paul has $25, and the bank has $75.@footnote{An even worse failure for this system could occur if the two set! operations attempt to change the balance simultaneously, in which case the actual data appearing in memory might end up being a random combination of the information being written by the two processes. Most computers have interlocks on the primitive memory-write operations, which protect against such simultaneous access. Even this seemingly simple kind of protection, however, raises implementation challenges in the design of multiprocessing computers, where elaborate cache-coherence protocols are required to ensure that the various processors will maintain a consistent view of memory contents, despite the fact that data may be replicated (``cached'') among the different processors to increase the speed of memory access.} +

    The timing diagram in Figure 3-29 depicts an order of events where balance starts at 100, Peter withdraws 10, Paul withdraws 25, and yet the final value of balance is 75. As shown in the diagram, the reason for this anomaly is that Paul's assignment of 75 to balance is made under the assumption that the value of balance to be decremented is 100. That assumption, however, became invalid when Peter changed balance to 90. This is a catastrophic failure for the banking system, because the total amount of money in the system is not conserved. Before the transactions, the total amount of money was $100. Afterwards, Peter has $10, Paul has $25, and the bank has $75.@footnote{An even worse failure for this system could occur if the two set! operations attempt to change the balance simultaneously, in which case the actual data appearing in memory might end up being a random combination of the information being written by the two processes. Most computers have interlocks on the primitive memory-write operations, which protect against such simultaneous access. Even this seemingly simple kind of protection, however, raises implementation challenges in the design of multiprocessing computers, where elaborate cache-coherence protocols are required to ensure that the various processors will maintain a consistent view of memory contents, despite the fact that data may be replicated (``cached'') among the different processors to increase the speed of memory access.}

    The general phenomenon illustrated here is that several processes may share a common state variable. What makes this complicated is that more than one process may be trying to manipulate the shared state at the same time. For the bank account example, during each transaction, each customer should be able to act as if the other customers did not exist. When a customer changes the balance in a way that depends on the balance, he must be able to assume that, just before the moment of change, the balance is still what he thought it was.

    Correct behavior of concurrent programs

    -

    The above example typifies the subtle bugs that can creep into concurrent programs. The root of this complexity lies in the assignments to variables that are shared among the different processes. We already know that we must be careful in writing programs that use set!, because the results of a computation depend on the order in which the assignments occur.@footnote{The factorial program in section 3.1.3 illustrates this for a single sequential process.} With concurrent processes we must be especially careful about assignments, because we may not be able to control the order of the assignments made by the different processes. If several such changes might be made concurrently (as with two depositors accessing a joint account) we need some way to ensure that our system behaves correctly. For example, in the case of withdrawals from a joint bank account, we must ensure that money is conserved. To make concurrent programs behave correctly, we may have to place some restrictions on concurrent execution. +

    The above example typifies the subtle bugs that can creep into concurrent programs. The root of this complexity lies in the assignments to variables that are shared among the different processes. We already know that we must be careful in writing programs that use set!, because the results of a computation depend on the order in which the assignments occur.@footnote{The factorial program in section 3.1.3 illustrates this for a single sequential process.} With concurrent processes we must be especially careful about assignments, because we may not be able to control the order of the assignments made by the different processes. If several such changes might be made concurrently (as with two depositors accessing a joint account) we need some way to ensure that our system behaves correctly. For example, in the case of withdrawals from a joint bank account, we must ensure that money is conserved. To make concurrent programs behave correctly, we may have to place some restrictions on concurrent execution.

    Figure 3.29: Timing diagram showing how interleaving the order of events in two banking withdrawals can lead to an incorrect final balance. @@ -103,13 +102,13 @@

    Correct behavior of concurrent programs

    | | new value: 100-25=75 | | ____ `----------+-----------' | / \ | - | | $ 90 |<------------' + | | $ 90 |<------------' V \____/ time
    -

    One possible restriction on concurrency would stipulate that no two operations that change any shared state variables can occur at the same time. This is an extremely stringent requirement. For distributed banking, it would require the system designer to ensure that only one transaction could proceed at a time. This would be both inefficient and overly conservative. Figure 3-30 shows Peter and Paul sharing a bank account, where Paul has a private account as well. The diagram illustrates two withdrawals from the shared account (one by Peter and one by Paul) and a deposit to Paul's private account.@footnote{The columns show the contents of Peter's wallet, the joint account (in Bank1), Paul's wallet, and Paul's private account (in Bank2), before and after each withdrawal (W) and deposit (D). Peter withdraws $10 from Bank1; Paul deposits $5 in Bank2, then withdraws $25 from Bank1.} The two withdrawals from the shared account must not be concurrent (since both access and update the same account), and Paul's deposit and withdrawal must not be concurrent (since both access and update the amount in Paul's wallet). But there should be no problem permitting Paul's deposit to his private account to proceed concurrently with Peter's withdrawal from the shared account. +

    One possible restriction on concurrency would stipulate that no two operations that change any shared state variables can occur at the same time. This is an extremely stringent requirement. For distributed banking, it would require the system designer to ensure that only one transaction could proceed at a time. This would be both inefficient and overly conservative. Figure 3-30 shows Peter and Paul sharing a bank account, where Paul has a private account as well. The diagram illustrates two withdrawals from the shared account (one by Peter and one by Paul) and a deposit to Paul's private account.@footnote{The columns show the contents of Peter's wallet, the joint account (in Bank1), Paul's wallet, and Paul's private account (in Bank2), before and after each withdrawal (W) and deposit (D). Peter withdraws $10 from Bank1; Paul deposits $5 in Bank2, then withdraws $25 from Bank1.} The two withdrawals from the shared account must not be concurrent (since both access and update the same account), and Paul's deposit and withdrawal must not be concurrent (since both access and update the amount in Paul's wallet). But there should be no problem permitting Paul's deposit to his private account to proceed concurrently with Peter's withdrawal from the shared account.

    Figure 3.30: Concurrent deposits and withdrawals from a joint account in Bank1 and a private account in Bank2. @@ -124,25 +123,25 @@

    Correct behavior of concurrent programs

    | | W | | D | | ____ ++-++ ____ ____ ++-++ ____ | / \ | | / \ / \ | | / \ - | | $17 |<-' `->| $90 |--. .--| $0 |<-' `->| $305 | + | | $17 |<-' `->| $90 |--. .--| $0 |<-' `->| $305 | | \____/ \____/ V V \____/ \____/ | +---+ | | W | | ____ ____ ++-++ ____ ____ | / \ / \ | | / \ / \ - | | $17 | | $65 |<-' `->| $25 | | $305 | + | | $17 | | $65 |<-' `->| $25 | | $305 | | \____/ \____/ \____/ \____/ V time
    -

    A less stringent restriction on concurrency would ensure that a concurrent system produces the same result as if the processes had run sequentially in some order. There are two important aspects to this requirement. First, it does not require the processes to actually run sequentially, but only to produce results that are the same as if they had run sequentially. For the example in Figure 3-30, the designer of the bank account system can safely allow Paul's deposit and Peter's withdrawal to happen concurrently, because the net result will be the same as if the two operations had happened sequentially. Second, there may be more than one possible ``correct'' result produced by a concurrent program, because we require only that the result be the same as for some sequential order. For example, suppose that Peter and Paul's joint account starts out with $100, and Peter deposits $40 while Paul concurrently withdraws half the money in the account. Then sequential execution could result in the account balance being either $70 or $90 (see Exercise 3-38).@footnote{[Footnote 39] A more formal way to express this idea is to say that concurrent programs are inherently nondeterministic . That is, they are described not by single-valued functions, but by functions whose results are sets of possible values. In section 4.3 we will study a language for expressing nondeterministic computations.} +

    A less stringent restriction on concurrency would ensure that a concurrent system produces the same result as if the processes had run sequentially in some order. There are two important aspects to this requirement. First, it does not require the processes to actually run sequentially, but only to produce results that are the same as if they had run sequentially. For the example in Figure 3-30, the designer of the bank account system can safely allow Paul's deposit and Peter's withdrawal to happen concurrently, because the net result will be the same as if the two operations had happened sequentially. Second, there may be more than one possible ``correct'' result produced by a concurrent program, because we require only that the result be the same as for some sequential order. For example, suppose that Peter and Paul's joint account starts out with $100, and Peter deposits $40 while Paul concurrently withdraws half the money in the account. Then sequential execution could result in the account balance being either $70 or $90 (see Exercise 3-38).@footnote{[Footnote 39] A more formal way to express this idea is to say that concurrent programs are inherently nondeterministic . That is, they are described not by single-valued functions, but by functions whose results are sets of possible values. In section 4.3 we will study a language for expressing nondeterministic computations.}

    There are still weaker requirements for correct execution of concurrent programs. A program for simulating diffusion (say, the flow of heat in an object) might consist of a large number of processes, each one representing a small volume of space, that update their values concurrently. Each process repeatedly changes its value to the average of its own value and its neighbors'values. This algorithm converges to the right answer independent of the order in which the operations are done; there is no need for any restrictions on concurrent use of the shared values.

    -

    Exercise 3.38: Suppose that Peter, Paul, and Mary share a joint bank account that initially contains $100. Concurrently, Peter deposits $10, Paul withdraws $20, and Mary withdraws half the money in the account, by executing the following commands: +

    Exercise 3.38: Suppose that Peter, Paul, and Mary share a joint bank account that initially contains $100. Concurrently, Peter deposits $10, Paul withdraws $20, and Mary withdraws half the money in the account, by executing the following commands:

     Peter: (set! balance (+ balance 10))
    @@ -153,7 +152,7 @@ 

    Correct behavior of concurrent programs

    • -List all the different possible values for balance after these three transactions have been completed, assuming that the banking system forces the three processes to run sequentially in some order. +List all the different possible values for balance after these three transactions have been completed, assuming that the banking system forces the three processes to run sequentially in some order.
    • @@ -163,8 +162,7 @@

      Correct behavior of concurrent programs

    - -

    3.4.2 Mechanisms for Controlling Concurrency

    +

    3.4.2 Mechanisms for Controlling Concurrency

    We've seen that the difficulty in dealing with concurrent processes is rooted in the need to consider the interleaving of the order of events in the different processes. For example, suppose we have two processes, one with three ordered events (a,b,c) and one with three ordered events (x,y,z). If the two processes run concurrently, with no constraints on how their execution is interleaved, then there are 20 different possible orderings for the events that are consistent with the individual orderings for the two processes: @@ -178,7 +176,7 @@

    3.4.2 Mechanisms for Controlling Concurrency

    As programmers designing this system, we would have to consider the effects of each of these 20 orderings and check that each behavior is acceptable. Such an approach rapidly becomes unwieldy as the numbers of processes and events increase. -

    A more practical approach to the design of concurrent systems is to devise general mechanisms that allow us to constrain the interleaving of concurrent processes so that we can be sure that the program behavior is correct. Many mechanisms have been developed for this purpose. In this section, we describe one of them, the serializer. +

    A more practical approach to the design of concurrent systems is to devise general mechanisms that allow us to constrain the interleaving of concurrent processes so that we can be sure that the program behavior is correct. Many mechanisms have been developed for this purpose. In this section, we describe one of them, the serializer.

    Serializing access to shared state

    @@ -188,20 +186,20 @@

    Serializing access to shared state

    Serializers in Scheme

    -

    To make the above mechanism more concrete, suppose that we have extended Scheme to include a procedure called parallel-execute: +

    To make the above mechanism more concrete, suppose that we have extended Scheme to include a procedure called parallel-execute: -

    -(parallel-execute ... ) +
    +(parallel-execute <p_1> <p_2> ... <p_k>)
    -

    Each

    must be a procedure of no arguments. Parallel-execute creates a separate process for each

    , which applies

    (to no arguments). These processes all run concurrently.@footnote{Parallel-execute is not part of standard Scheme, but it can be implemented in @acronym{MIT} Scheme. In our implementation, the new concurrent processes also run concurrently with the original Scheme process. Also, in our implementation, the value returned by parallel-execute is a special control object that can be used to halt the newly created processes.} +

    Each

    must be a procedure of no arguments. Parallel-execute creates a separate process for each

    , which applies

    (to no arguments). These processes all run concurrently.@footnote{Parallel-execute is not part of standard Scheme, but it can be implemented in @acronym{MIT} Scheme. In our implementation, the new concurrent processes also run concurrently with the original Scheme process. Also, in our implementation, the value returned by parallel-execute is a special control object that can be used to halt the newly created processes.}

    As an example of how this is used, consider -

    +
    (define x 10) (parallel-execute (lambda () (set! x (* x x))) @@ -211,27 +209,27 @@

    Serializers in Scheme

    C.prompt(); -

    This creates two concurrent processes---P_1, which sets x to x times x, and P_2, which increments x. After execution is complete, x will be left with one of five possible values, depending on the interleaving of the events of P_1 and P_2: +

    This creates two concurrent processes---P_1, which sets x to x times x, and P_2, which increments x. After execution is complete, x will be left with one of five possible values, depending on the interleaving of the events of P_1 and P_2:

    -101: P_1 sets x to 100 and then P_2 increments
    -     x to 101.
    -121: P_2 increments x to 11 and then P_1 sets
    -     x to x times x.
    -110: P_2 changes x from 10 to 11 between the two
    -     times that P_1 accesses the value of x during
    -     the evaluation of (* x x).
    -11:  P_2 accesses x, then P_1 sets x to
    -     100, then P_2 sets x.
    -100: P_1 accesses x (twice), then P_2 sets
    -     x to 11, then P_1 sets x.
    +101: P_1 sets x to 100 and then P_2 increments
    +     x to 101.
    +121: P_2 increments x to 11 and then P_1 sets
    +     x to x times x.
    +110: P_2 changes x from 10 to 11 between the two
    +     times that P_1 accesses the value of x during
    +     the evaluation of (* x x).
    +11:  P_2 accesses x, then P_1 sets x to
    +     100, then P_2 sets x.
    +100: P_1 accesses x (twice), then P_2 sets
    +     x to 11, then P_1 sets x.
     
    -

    We can constrain the concurrency by using serialized procedures, which are created by serializers . Serializers are constructed by make-serializer, whose implementation is given below. A serializer takes a procedure as argument and returns a serialized procedure that behaves like the original procedure. All calls to a given serializer return serialized procedures in the same set. +

    We can constrain the concurrency by using serialized procedures, which are created by serializers . Serializers are constructed by make-serializer, whose implementation is given below. A serializer takes a procedure as argument and returns a serialized procedure that behaves like the original procedure. All calls to a given serializer return serialized procedures in the same set.

    Thus, in contrast to the example above, executing -

    +
    (define x 10) (define s (make-serializer)) @@ -243,11 +241,11 @@

    Serializers in Scheme

    C.prompt(); -

    can produce only two possible values for x, 101 or 121. The other possibilities are eliminated, because the execution of P_1 and P_2 cannot be interleaved. +

    can produce only two possible values for x, 101 or 121. The other possibilities are eliminated, because the execution of P_1 and P_2 cannot be interleaved. -

    Here is a version of the make-account procedure from section 3.1.1, where the deposits and withdrawals have been serialized: +

    Here is a version of the make-account procedure from section 3.1.1, where the deposits and withdrawals have been serialized: -

    +
    (define (make-account balance) (define (withdraw amount) (if (>= balance amount) @@ -272,10 +270,10 @@

    Serializers in Scheme

    With this implementation, two processes cannot be withdrawing from or depositing into a single account concurrently. This eliminates the source of the error illustrated in Figure 3-29, where Peter changes the account balance between the times when Paul accesses the balance to compute the new value and when Paul actually performs the assignment. On the other hand, each account has its own serializer, so that deposits and withdrawals for different accounts can proceed concurrently. -

    +

    Exercise 3.39: Which of the five possibilities in the parallel execution shown above remain if we instead serialize execution as follows: -

    +
    (define x 10) (define s (make-serializer)) @@ -290,9 +288,9 @@

    Serializers in Scheme

    Exercise 3.40: Give all possible values of -x that can result from executing +x that can result from executing -
    +
    (define x 10) (parallel-execute (lambda () (set! x (* x x))) @@ -304,7 +302,7 @@

    Serializers in Scheme

    Which of these possibilities remain if we instead use serialized procedures: -

    +
    (define x 10) (define s (make-serializer)) @@ -317,10 +315,10 @@

    Serializers in Scheme

    -
    +

    Exercise 3.41: Ben Bitdiddle worries that it would be better to implement the bank account as follows (where the commented line has been changed): -

    +
    (define (make-account balance) (define (withdraw amount) (if (>= balance amount) @@ -350,10 +348,10 @@

    Serializers in Scheme

    because allowing unserialized access to the bank balance can result in anomalous behavior. Do you agree? Is there any scenario that demonstrates Ben's concern?

    -
    -

    Exercise 3.42: Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure. +

    +

    Exercise 3.42: Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every withdraw and deposit message. He says that make-account could be changed so that the calls to protected are done outside the dispatch procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure. -

    +
    (define (make-account balance) (define (withdraw amount) (if (>= balance amount) @@ -378,16 +376,16 @@

    Serializers in Scheme

    C.prompt(); -

    Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account? +

    Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of make-account?

    Complexity of using multiple shared resources

    Serializers provide a powerful abstraction that helps isolate the complexities of concurrent programs so that they can be dealt with carefully and (hopefully) correctly. However, while using serializers is relatively straightforward when there is only a single shared resource (such as a single bank account), concurrent programming can be treacherously difficult when there are multiple shared resources. -

    To illustrate one of the difficulties that can arise, suppose we wish to swap the balances in two bank accounts. We access each account to find the balance, compute the difference between the balances, withdraw this difference from one account, and deposit it in the other account. We could implement this as follows:@footnote{We have simplified exchange by exploiting the fact that our deposit message accepts negative amounts. (This is a serious bug in our banking system!)} +

    To illustrate one of the difficulties that can arise, suppose we wish to swap the balances in two bank accounts. We access each account to find the balance, compute the difference between the balances, withdraw this difference from one account, and deposit it in the other account. We could implement this as follows:@footnote{We have simplified exchange by exploiting the fact that our deposit message accepts negative amounts. (This is a serious bug in our banking system!)} -

    +
    (define (exchange account1 account2) (let ((difference (- (account1 'balance) (account2 'balance)))) @@ -398,11 +396,11 @@

    Complexity of using multiple shared resources

    C.prompt(); -

    This procedure works well when only a single process is trying to do the exchange. Suppose, however, that Peter and Paul both have access to accounts a1, a2, and a3, and that Peter exchanges a1 and a2 while Paul concurrently exchanges a1 and a3. Even with account deposits and withdrawals serialized for individual accounts (as in the make-account procedure shown above in this section), exchange can still produce incorrect results. For example, Peter might compute the difference in the balances for a1 and a2, but then Paul might change the balance in a1 before Peter is able to complete the exchange.@footnote{If the account balances start out as $10, $20, and $30, then after any number of concurrent exchanges, the balances should still be $10, $20, and $30 in some order. Serializing the deposits to individual accounts is not sufficient to guarantee this. See Exercise 3-43.} For correct behavior, we must arrange for the exchange procedure to lock out any other concurrent accesses to the accounts during the entire time of the exchange. +

    This procedure works well when only a single process is trying to do the exchange. Suppose, however, that Peter and Paul both have access to accounts a1, a2, and a3, and that Peter exchanges a1 and a2 while Paul concurrently exchanges a1 and a3. Even with account deposits and withdrawals serialized for individual accounts (as in the make-account procedure shown above in this section), exchange can still produce incorrect results. For example, Peter might compute the difference in the balances for a1 and a2, but then Paul might change the balance in a1 before Peter is able to complete the exchange.@footnote{If the account balances start out as $10, $20, and $30, then after any number of concurrent exchanges, the balances should still be $10, $20, and $30 in some order. Serializing the deposits to individual accounts is not sufficient to guarantee this. See Exercise 3-43.} For correct behavior, we must arrange for the exchange procedure to lock out any other concurrent accesses to the accounts during the entire time of the exchange.

    -

    One way we can accomplish this is by using both accounts' serializers to serialize the entire exchange procedure. To do this, we will arrange for access to an account's serializer. Note that we are deliberately breaking the modularity of the bank-account object by exposing the serializer. The following version of make-account is identical to the original version given in section 3.1.1, except that a serializer is provided to protect the balance variable, and the serializer is exported via message passing: +

    One way we can accomplish this is by using both accounts' serializers to serialize the entire exchange procedure. To do this, we will arrange for access to an account's serializer. Note that we are deliberately breaking the modularity of the bank-account object by exposing the serializer. The following version of make-account is identical to the original version given in section 3.1.1, except that a serializer is provided to protect the balance variable, and the serializer is exported via message passing: -

    +
    (define (make-account-and-serializer balance) (define (withdraw amount) (if (>= balance amount) @@ -428,7 +426,7 @@

    Complexity of using multiple shared resources

    We can use this to do serialized deposits and withdrawals. However, unlike our earlier serialized account, it is now the responsibility of each user of bank-account objects to explicitly manage the serialization, for example as follows:@footnote{Exercise 3-45 investigates why deposits and withdrawals are no longer automatically serialized by the account.} -

    +
    (define (deposit account amount) (let ((s (account 'serializer)) (d (account 'deposit))) @@ -438,9 +436,9 @@

    Complexity of using multiple shared resources

    C.prompt(); -

    Exporting the serializer in this way gives us enough flexibility to implement a serialized exchange program. We simply serialize the original exchange procedure with the serializers for both accounts: +

    Exporting the serializer in this way gives us enough flexibility to implement a serialized exchange program. We simply serialize the original exchange procedure with the serializers for both accounts: -

    +
    (define (serialized-exchange account1 account2) (let ((serializer1 (account1 'serializer)) (serializer2 (account2 'serializer))) @@ -452,14 +450,14 @@

    Complexity of using multiple shared resources

    C.prompt(); -
    -

    Exercise 3.43: Suppose that the balances in three accounts start out as $10, $20, and $30, and that multiple processes run, exchanging the balances in the accounts. Argue that if the processes are run sequentially, after any number of concurrent exchanges, the account balances should be $10, $20, and $30 in some order. Draw a timing diagram like the one in Figure 3-29 to show how this condition can be violated if the exchanges are implemented using the first version of the account-exchange program in this section. On the other hand, argue that even with this exchange program, the sum of the balances in the accounts will be preserved. Draw a timing diagram to show how even this condition would be violated if we did not serialize the transactions on individual accounts. -

    +
    +

    Exercise 3.43: Suppose that the balances in three accounts start out as $10, $20, and $30, and that multiple processes run, exchanging the balances in the accounts. Argue that if the processes are run sequentially, after any number of concurrent exchanges, the account balances should be $10, $20, and $30 in some order. Draw a timing diagram like the one in Figure 3-29 to show how this condition can be violated if the exchanges are implemented using the first version of the account-exchange program in this section. On the other hand, argue that even with this exchange program, the sum of the balances in the accounts will be preserved. Draw a timing diagram to show how even this condition would be violated if we did not serialize the transactions on individual accounts. +

    -
    -

    Exercise 3.44: Consider the problem of transferring an amount from one account to another. Ben Bitdiddle claims that this can be accomplished with the following procedure, even if there are multiple people concurrently transferring money among multiple accounts, using any account mechanism that serializes deposit and withdrawal transactions, for example, the version of make-account in the text above. +

    +

    Exercise 3.44: Consider the problem of transferring an amount from one account to another. Ben Bitdiddle claims that this can be accomplished with the following procedure, even if there are multiple people concurrently transferring money among multiple accounts, using any account mechanism that serializes deposit and withdrawal transactions, for example, the version of make-account in the text above. -

    +
    (define (transfer from-account to-account amount) ((from-account 'withdraw) amount) ((to-account 'deposit) amount)) @@ -468,13 +466,13 @@

    Complexity of using multiple shared resources

    C.prompt(); -

    Louis Reasoner claims that there is a problem here, and that we need to use a more sophisticated method, such as the one required for dealing with the exchange problem. Is Louis right? If not, what is the essential difference between the transfer problem and the exchange problem? (You should assume that the balance in from-account is at least amount.) +

    Louis Reasoner claims that there is a problem here, and that we need to use a more sophisticated method, such as the one required for dealing with the exchange problem. Is Louis right? If not, what is the essential difference between the transfer problem and the exchange problem? (You should assume that the balance in from-account is at least amount.)

    -
    -

    Exercise 3.45: Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren't automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of) using it to serialize accounts and deposits as make-account did. He proposes to redefine accounts as follows: +

    +

    Exercise 3.45: Louis Reasoner thinks our bank-account system is unnecessarily complex and error-prone now that deposits and withdrawals aren't automatically serialized. He suggests that make-account-and-serializer should have exported the serializer (for use by such procedures as serialized-exchange) in addition to (rather than instead of) using it to serialize accounts and deposits as make-account did. He proposes to redefine accounts as follows: -

    +
    (define (make-account-and-serializer balance) (define (withdraw amount) (if (>= balance amount) @@ -498,9 +496,9 @@

    Complexity of using multiple shared resources

    C.prompt(); -

    Then deposits are handled as with the original make-account: +

    Then deposits are handled as with the original make-account: -

    +
    (define (deposit account amount) ((account 'deposit) amount))
    @@ -508,14 +506,14 @@

    Complexity of using multiple shared resources

    C.prompt(); -

    Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called. +

    Explain what is wrong with Louis's reasoning. In particular, consider what happens when serialized-exchange is called.

    Implementing serializers

    -

    We implement serializers in terms of a more primitive synchronization mechanism called a mutex . A mutex is an object that supports two operations---the mutex can be acquired , and the mutex can be released . Once a mutex has been acquired, no other acquire operations on that mutex may proceed until the mutex is released.@footnote{The term ``mutex'' is an abbreviation for mutual exclusion . The general problem of arranging a mechanism that permits concurrent processes to safely share resources is called the mutual exclusion problem. Our mutex is a simple variant of the semaphore mechanism (see Exercise 3-47), which was introduced in the ``THE'' Multiprogramming System developed at the Technological University of Eindhoven and named for the university's initials in Dutch (Dijkstra 1968a). The acquire and release operations were originally called P and V, from the Dutch words passeren (to pass) and vrijgeven (to release), in reference to the semaphores used on railroad systems. Dijkstra's classic exposition (1968b) was one of the first to clearly present the issues of concurrency control, and showed how to use semaphores to handle a variety of concurrency problems.} In our implementation, each serializer has an associated mutex. Given a procedure p, the serializer returns a procedure that acquires the mutex, runs p, and then releases the mutex. This ensures that only one of the procedures produced by the serializer can be running at once, which is precisely the serialization property that we need to guarantee. +

    We implement serializers in terms of a more primitive synchronization mechanism called a mutex . A mutex is an object that supports two operations---the mutex can be acquired , and the mutex can be released . Once a mutex has been acquired, no other acquire operations on that mutex may proceed until the mutex is released.@footnote{The term ``mutex'' is an abbreviation for mutual exclusion . The general problem of arranging a mechanism that permits concurrent processes to safely share resources is called the mutual exclusion problem. Our mutex is a simple variant of the semaphore mechanism (see Exercise 3-47), which was introduced in the ``THE'' Multiprogramming System developed at the Technological University of Eindhoven and named for the university's initials in Dutch (Dijkstra 1968a). The acquire and release operations were originally called P and V, from the Dutch words passeren (to pass) and vrijgeven (to release), in reference to the semaphores used on railroad systems. Dijkstra's classic exposition (1968b) was one of the first to clearly present the issues of concurrency control, and showed how to use semaphores to handle a variety of concurrency problems.} In our implementation, each serializer has an associated mutex. Given a procedure p, the serializer returns a procedure that acquires the mutex, runs p, and then releases the mutex. This ensures that only one of the procedures produced by the serializer can be running at once, which is precisely the serialization property that we need to guarantee. -

    +
    (define (make-serializer) (let ((mutex (make-mutex))) (lambda (p) @@ -530,11 +528,11 @@

    Implementing serializers

    C.prompt(); -

    The mutex is a mutable object (here we'll use a one-element list, which we'll refer to as a cell ) that can hold the value true or false. When the value is false, the mutex is available to be acquired. When the value is true, the mutex is unavailable, and any process that attempts to acquire the mutex must wait. +

    The mutex is a mutable object (here we'll use a one-element list, which we'll refer to as a cell ) that can hold the value true or false. When the value is false, the mutex is available to be acquired. When the value is true, the mutex is unavailable, and any process that attempts to acquire the mutex must wait. -

    Our mutex constructor make-mutex begins by initializing the cell contents to false. To acquire the mutex, we test the cell. If the mutex is available, we set the cell contents to true and proceed. Otherwise, we wait in a loop, attempting to acquire over and over again, until we find that the mutex is available.@footnote{In most time-shared operating systems, processes that are blocked by a mutex do not waste time ``busy-waiting'' as above. Instead, the system schedules another process to run while the first is waiting, and the blocked process is awakened when the mutex becomes available.} To release the mutex, we set the cell contents to false. +

    Our mutex constructor make-mutex begins by initializing the cell contents to false. To acquire the mutex, we test the cell. If the mutex is available, we set the cell contents to true and proceed. Otherwise, we wait in a loop, attempting to acquire over and over again, until we find that the mutex is available.@footnote{In most time-shared operating systems, processes that are blocked by a mutex do not waste time ``busy-waiting'' as above. Instead, the system schedules another process to run while the first is waiting, and the blocked process is awakened when the mutex becomes available.} To release the mutex, we set the cell contents to false. -

    +
    (define (make-mutex) (let ((cell (list false))) (define (the-mutex m) @@ -551,9 +549,9 @@

    Implementing serializers

    C.prompt(); -

    Test-and-set! tests the cell and returns the result of the test. In addition, if the test was false, test-and-set! sets the cell contents to true before returning false. We can express this behavior as the following procedure: +

    Test-and-set! tests the cell and returns the result of the test. In addition, if the test was false, test-and-set! sets the cell contents to true before returning false. We can express this behavior as the following procedure: -

    +
    (define (test-and-set! cell) (if (car cell) true @@ -564,11 +562,11 @@

    Implementing serializers

    C.prompt(); -

    However, this implementation of test-and-set! does not suffice as it stands. There is a crucial subtlety here, which is the essential place where concurrency control enters the system: The test-and-set! operation must be performed atomically . That is, we must guarantee that, once a process has tested the cell and found it to be false, the cell contents will actually be set to true before any other process can test the cell. If we do not make this guarantee, then the mutex can fail in a way similar to the bank-account failure in Figure 3-29. (See Exercise 3-46.) +

    However, this implementation of test-and-set! does not suffice as it stands. There is a crucial subtlety here, which is the essential place where concurrency control enters the system: The test-and-set! operation must be performed atomically . That is, we must guarantee that, once a process has tested the cell and found it to be false, the cell contents will actually be set to true before any other process can test the cell. If we do not make this guarantee, then the mutex can fail in a way similar to the bank-account failure in Figure 3-29. (See Exercise 3-46.) -

    The actual implementation of test-and-set! depends on the details of how our system runs concurrent processes. For example, we might be executing concurrent processes on a sequential processor using a time-slicing mechanism that cycles through the processes, permitting each process to run for a short time before interrupting it and moving on to the next process. In that case, test-and-set! can work by disabling time slicing during the testing and setting.@footnote{In @acronym{MIT} Scheme for a single processor, which uses a time-slicing model, test-and-set! can be implemented as follows: +

    The actual implementation of test-and-set! depends on the details of how our system runs concurrent processes. For example, we might be executing concurrent processes on a sequential processor using a time-slicing mechanism that cycles through the processes, permitting each process to run for a short time before interrupting it and moving on to the next process. In that case, test-and-set! can work by disabling time slicing during the testing and setting.@footnote{In @acronym{MIT} Scheme for a single processor, which uses a time-slicing model, test-and-set! can be implemented as follows: -

    +
    (define (test-and-set! cell) (without-interrupts (lambda () @@ -581,13 +579,13 @@

    Implementing serializers

    C.prompt(); -

    Without-interrupts disables time-slicing interrupts while its procedure argument is being executed.} Alternatively, multiprocessing computers provide instructions that support atomic operations directly in hardware.@footnote{There are many variants of such instructions---including test-and-set, test-and-clear, swap, compare-and-exchange, load-reserve, and store-conditional---whose design must be carefully matched to the machine's processor-memory interface. One issue that arises here is to determine what happens if two processes attempt to acquire the same resource at exactly the same time by using such an instruction. This requires some mechanism for making a decision about which process gets control. Such a mechanism is called an arbiter . Arbiters usually boil down to some sort of hardware device. Unfortunately, it is possible to prove that one cannot physically construct a fair arbiter that works 100% of the time unless one allows the arbiter an arbitrarily long time to make its decision. The fundamental phenomenon here was originally observed by the fourteenth-century French philosopher Jean Buridan in his commentary on Aristotle's De caelo. Buridan argued that a perfectly rational dog placed between two equally attractive sources of food will starve to death, because it is incapable of deciding which to go to first.} +

    Without-interrupts disables time-slicing interrupts while its procedure argument is being executed.} Alternatively, multiprocessing computers provide instructions that support atomic operations directly in hardware.@footnote{There are many variants of such instructions---including test-and-set, test-and-clear, swap, compare-and-exchange, load-reserve, and store-conditional---whose design must be carefully matched to the machine's processor-memory interface. One issue that arises here is to determine what happens if two processes attempt to acquire the same resource at exactly the same time by using such an instruction. This requires some mechanism for making a decision about which process gets control. Such a mechanism is called an arbiter . Arbiters usually boil down to some sort of hardware device. Unfortunately, it is possible to prove that one cannot physically construct a fair arbiter that works 100% of the time unless one allows the arbiter an arbitrarily long time to make its decision. The fundamental phenomenon here was originally observed by the fourteenth-century French philosopher Jean Buridan in his commentary on Aristotle's De caelo. Buridan argued that a perfectly rational dog placed between two equally attractive sources of food will starve to death, because it is incapable of deciding which to go to first.} -

    -

    Exercise 3.46: Suppose that we implement test-and-set! using an ordinary procedure as shown in the text, without attempting to make the operation atomic. Draw a timing diagram like the one in Figure 3-29 to demonstrate how the mutex implementation can fail by allowing two processes to acquire the mutex at the same time. +

    +

    Exercise 3.46: Suppose that we implement test-and-set! using an ordinary procedure as shown in the text, without attempting to make the operation atomic. Draw a timing diagram like the one in Figure 3-29 to demonstrate how the mutex implementation can fail by allowing two processes to acquire the mutex at the same time.

    -
    +

    Exercise 3.47: A semaphore (of size n) is a generalization of a mutex. Like a mutex, a semaphore supports acquire and release operations, but it is more general in that up to n processes can acquire it concurrently. Additional processes that attempt to acquire the semaphore must wait for release operations. Give implementations of semaphores

      @@ -596,19 +594,19 @@

      Implementing serializers

    • -in terms of atomic test-and-set! operations. +in terms of atomic test-and-set! operations.

    Deadlock

    -

    Now that we have seen how to implement serializers, we can see that account exchanging still has a problem, even with the serialized-exchange procedure above. Imagine that Peter attempts to exchange a1 with a2 while Paul concurrently attempts to exchange a2 with a1. Suppose that Peter's process reaches the point where it has entered a serialized procedure protecting a1 and, just after that, Paul's process enters a serialized procedure protecting a2. Now Peter cannot proceed (to enter a serialized procedure protecting a2) until Paul exits the serialized procedure protecting a2. Similarly, Paul cannot proceed until Peter exits the serialized procedure protecting a1. Each process is stalled forever, waiting for the other. This situation is called a deadlock . Deadlock is always a danger in systems that provide concurrent access to multiple shared resources. +

    Now that we have seen how to implement serializers, we can see that account exchanging still has a problem, even with the serialized-exchange procedure above. Imagine that Peter attempts to exchange a1 with a2 while Paul concurrently attempts to exchange a2 with a1. Suppose that Peter's process reaches the point where it has entered a serialized procedure protecting a1 and, just after that, Paul's process enters a serialized procedure protecting a2. Now Peter cannot proceed (to enter a serialized procedure protecting a2) until Paul exits the serialized procedure protecting a2. Similarly, Paul cannot proceed until Peter exits the serialized procedure protecting a1. Each process is stalled forever, waiting for the other. This situation is called a deadlock . Deadlock is always a danger in systems that provide concurrent access to multiple shared resources. -

    One way to avoid the deadlock in this situation is to give each account a unique identification number and rewrite serialized-exchange so that a process will always attempt to enter a procedure protecting the lowest-numbered account first. Although this method works well for the exchange problem, there are other situations that require more sophisticated deadlock-avoidance techniques, or where deadlock cannot be avoided at all. (See Exercise 3-48 and Exercise 3-49.)@footnote{The general technique for avoiding deadlock by numbering the shared resources and acquiring them in order is due to Havender (1968). Situations where deadlock cannot be avoided require deadlock-recovery methods, which entail having processes ``back out''of the deadlocked state and try again. Deadlock-recovery mechanisms are widely used in database management systems, a topic that is treated in detail in Gray and Reuter 1993.} +

    One way to avoid the deadlock in this situation is to give each account a unique identification number and rewrite serialized-exchange so that a process will always attempt to enter a procedure protecting the lowest-numbered account first. Although this method works well for the exchange problem, there are other situations that require more sophisticated deadlock-avoidance techniques, or where deadlock cannot be avoided at all. (See Exercise 3-48 and Exercise 3-49.)@footnote{The general technique for avoiding deadlock by numbering the shared resources and acquiring them in order is due to Havender (1968). Situations where deadlock cannot be avoided require deadlock-recovery methods, which entail having processes ``back out''of the deadlocked state and try again. Deadlock-recovery mechanisms are widely used in database management systems, a topic that is treated in detail in Gray and Reuter 1993.} -

    -

    Exercise 3.48: Explain in detail why the deadlock-avoidance method described above, (i.e., the accounts are numbered, and each process attempts to acquire the smaller-numbered account first) avoids deadlock in the exchange problem. Rewrite serialized-exchange to incorporate this idea. (You will also need to modify make-account so that each account is created with a number, which can be accessed by sending an appropriate message.) +

    +

    Exercise 3.48: Explain in detail why the deadlock-avoidance method described above, (i.e., the accounts are numbered, and each process attempts to acquire the smaller-numbered account first) avoids deadlock in the exchange problem. Rewrite serialized-exchange to incorporate this idea. (You will also need to modify make-account so that each account is created with a number, which can be accessed by sending an appropriate message.)

    @@ -619,7 +617,7 @@

    Concurrency, time, and communication

    We've seen how programming concurrent systems requires controlling the ordering of events when different processes access shared state, and we've seen how to achieve this control through judicious use of serializers. But the problems of concurrency lie deeper than this, because, from a fundamental point of view, it's not always clear what is meant by ``shared state.'' -

    Mechanisms such as test-and-set! require processes to examine a global shared flag at arbitrary times. This is problematic and inefficient to implement in modern high-speed processors, where due to optimization techniques such as pipelining and cached memory, the contents of memory may not be in a consistent state at every instant. In contemporary multiprocessing systems, therefore, the serializer paradigm is being supplanted by new approaches to concurrency control.@footnote{One such alternative to serialization is called barrier synchronization . The programmer permits concurrent processes to execute as they please, but establishes certain synchronization points (``barriers'') through which no process can proceed until all the processes have reached the barrier. Modern processors provide machine instructions that permit programmers to establish synchronization points at places where consistency is required. The PowerPC^( TM), for example, includes for this purpose two instructions called SYNC and EIEIO (Enforced In-order Execution of Input/Output).} +

    Mechanisms such as test-and-set! require processes to examine a global shared flag at arbitrary times. This is problematic and inefficient to implement in modern high-speed processors, where due to optimization techniques such as pipelining and cached memory, the contents of memory may not be in a consistent state at every instant. In contemporary multiprocessing systems, therefore, the serializer paradigm is being supplanted by new approaches to concurrency control.@footnote{One such alternative to serialization is called barrier synchronization . The programmer permits concurrent processes to execute as they please, but establishes certain synchronization points (``barriers'') through which no process can proceed until all the processes have reached the barrier. Modern processors provide machine instructions that permit programmers to establish synchronization points at places where consistency is required. The PowerPC^( TM), for example, includes for this purpose two instructions called SYNC and EIEIO (Enforced In-order Execution of Input/Output).}

    The problematic aspects of shared state also arise in large, distributed systems. For instance, imagine a distributed banking system where individual branch banks maintain local values for bank balances and periodically compare these with values maintained by other branches. In such a system the value of ``the account balance'' would be undetermined, except right after synchronization. If Peter deposits money in an account he holds jointly with Paul, when should we say that the account balance has changed---when the balance in the local branch changes, or not until after the synchronization? And if Paul accesses the account from a different branch, what are the reasonable constraints to place on the banking system such that the behavior is ``correct''? The only thing that might matter for correctness is the behavior observed by Peter and Paul individually and the ``state'' of the account immediately after synchronization. Questions about the ``real'' account balance or the order of events between synchronizations may be irrelevant or meaningless.@footnote{This may seem like a strange point of view, but there are systems that work this way. International charges to credit-card accounts, for example, are normally cleared on a per-country basis, and the charges made in different countries are periodically reconciled. Thus the account balance may be different in different countries.} diff --git a/content/3-5-streams.content.html b/content/3-5-streams.content.html index 91e2237..242bdea 100644 --- a/content/3-5-streams.content.html +++ b/content/3-5-streams.content.html @@ -3,35 +3,34 @@ @@ {{main_text}} - + - - + +

    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. +

    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.

    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.@footnote{Physicists sometimes adopt this view by introducing the ``world lines'' of particles as a device for reasoning about motion. We've also already mentioned (section 2.2.3) that this is the natural way to think about signal-processing systems. We will explore applications of streams to signal processing in section 3.5.3.} -

    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 section 2.2.1) doesn't fully reveal the power of stream processing. As an alternative, we introduce the technique of delayed evaluation , which enables us to represent very large (even infinite) sequences as streams. +

    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 section 2.2.1) doesn't fully reveal the power of stream processing. As an alternative, we introduce the technique of delayed evaluation , which enables us to represent very large (even infinite) sequences as streams.

    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. - -

    3.5.1 Streams Are Delayed Lists

    +

    3.5.1 Streams Are Delayed Lists

    -

    As we saw in section 2.2.3, sequences can serve as standard interfaces for combining program modules. We formulated powerful abstractions for manipulating sequences, such as map, filter, and accumulate, that capture a wide variety of operations in a manner that is both succinct and elegant. +

    As we saw in section 2.2.3, sequences can serve as standard interfaces for combining program modules. We formulated powerful abstractions for manipulating sequences, such as map, filter, and accumulate, that capture a wide variety of operations in a manner that is both succinct and elegant.

    Unfortunately, if we represent sequences as lists, this elegance is bought at the price of severe inefficiency with respect to both the time and space required by our computations. When we represent manipulations on sequences as transformations of lists, our programs must construct and copy data structures (which may be huge) at every step of a process. -

    To see why this is true, let us compare two programs for computing the sum of all the prime numbers in an interval. The first program is written in standard iterative style:@footnote{Assume that we have a predicate prime? (e.g., as in section 1.2.6) that tests for primality.} +

    To see why this is true, let us compare two programs for computing the sum of all the prime numbers in an interval. The first program is written in standard iterative style:@footnote{Assume that we have a predicate prime? (e.g., as in section 1.2.6) that tests for primality.} -

    +
    (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) @@ -45,7 +44,7 @@

    3.5.1 Streams Are Delayed Lists

    The second program performs the same computation using the sequence operations of section 2.2.3: -

    +
    (define (sum-primes a b) (accumulate + 0 @@ -55,11 +54,11 @@

    3.5.1 Streams Are Delayed Lists

    C.prompt(); -

    In carrying out the computation, the first program needs to store only the sum being accumulated. In contrast, the filter in the second program cannot do any testing until enumerate-interval has constructed a complete list of the numbers in the interval. The filter generates another list, which in turn is passed to accumulate before being collapsed to form a sum. Such large intermediate storage is not needed by the first program, which we can think of as enumerating the interval incrementally, adding each prime to the sum as it is generated. +

    In carrying out the computation, the first program needs to store only the sum being accumulated. In contrast, the filter in the second program cannot do any testing until enumerate-interval has constructed a complete list of the numbers in the interval. The filter generates another list, which in turn is passed to accumulate before being collapsed to form a sum. Such large intermediate storage is not needed by the first program, which we can think of as enumerating the interval incrementally, adding each prime to the sum as it is generated.

    The inefficiency in using lists becomes painfully apparent if we use the sequence paradigm to compute the second prime in the interval from 10,000 to 1,000,000 by evaluating the expression -

    +
    (car (cdr (filter prime? (enumerate-interval 10000 1000000))))
    @@ -71,16 +70,16 @@

    3.5.1 Streams Are Delayed Lists

    Streams are a clever idea that allows one to use sequence manipulations without incurring the costs of manipulating sequences as lists. With streams we can achieve the best of both worlds: We can formulate programs elegantly as sequence manipulations, while attaining the efficiency of incremental computation. The basic idea is to arrange to construct a stream only partially, and to pass the partial construction to the program that consumes the stream. If the consumer attempts to access a part of the stream that has not yet been constructed, the stream will automatically construct just enough more of itself to produce the required part, thus preserving the illusion that the entire stream exists. In other words, although we will write programs as if we were processing complete sequences, we design our stream implementation to automatically and transparently interleave the construction of the stream with its use. -

    On the surface, streams are just lists with different names for the procedures that manipulate them. There is a constructor, cons-stream, and two selectors, stream-car and stream-cdr, which satisfy the constraints +

    On the surface, streams are just lists with different names for the procedures that manipulate them. There is a constructor, cons-stream, and two selectors, stream-car and stream-cdr, which satisfy the constraints

     (stream-car (cons-stream x y)) = x
     (stream-cdr (cons-stream x y)) = y
     
    -

    There is a distinguishable object, the-empty-stream, which cannot be the result of any cons-stream operation, and which can be identified with the predicate stream-null?.@footnote{In the @acronym{MIT} implementation, the-empty-stream is the same as the empty list '(), and stream-null? is the same as null?.} Thus we can make and use streams, in just the same way as we can make and use lists, to represent aggregate data arranged in a sequence. In particular, we can build stream analogs of the list operations from Chapter 2, such as list-ref, map, and for-each:@footnote{This should bother you. The fact that we are defining such similar procedures for streams and lists indicates that we are missing some underlying abstraction. Unfortunately, in order to exploit this abstraction, we will need to exert finer control over the process of evaluation than we can at present. We will discuss this point further at the end of section 3.5.4. In section 4.2, we'll develop a framework that unifies lists and streams.} +

    There is a distinguishable object, the-empty-stream, which cannot be the result of any cons-stream operation, and which can be identified with the predicate stream-null?.@footnote{In the @acronym{MIT} implementation, the-empty-stream is the same as the empty list '(), and stream-null? is the same as null?.} Thus we can make and use streams, in just the same way as we can make and use lists, to represent aggregate data arranged in a sequence. In particular, we can build stream analogs of the list operations from Chapter 2, such as list-ref, map, and for-each:@footnote{This should bother you. The fact that we are defining such similar procedures for streams and lists indicates that we are missing some underlying abstraction. Unfortunately, in order to exploit this abstraction, we will need to exert finer control over the process of evaluation than we can at present. We will discuss this point further at the end of section 3.5.4. In section 4.2, we'll develop a framework that unifies lists and streams.} -

    +
    (define (stream-ref s n) (if (= n 0) (stream-car s) @@ -102,9 +101,9 @@

    3.5.1 Streams Are Delayed Lists

    C.prompt(); -Stream-for-each is useful for viewing streams: +Stream-for-each is useful for viewing streams: -
    +
    (define (display-stream s) (stream-for-each display-line s)) @@ -116,14 +115,14 @@

    3.5.1 Streams Are Delayed Lists

    C.prompt(); -

    To make the stream implementation automatically and transparently interleave the construction of a stream with its use, we will arrange for the cdr of a stream to be evaluated when it is accessed by the stream-cdr procedure rather than when the stream is constructed by cons-stream. This implementation choice is reminiscent of our discussion of rational numbers in section 2.1.2, where we saw that we can choose to implement rational numbers so that the reduction of numerator and denominator to lowest terms is performed either at construction time or at selection time. The two rational-number implementations produce the same data abstraction, but the choice has an effect on efficiency. There is a similar relationship between streams and ordinary lists. As a data abstraction, streams are the same as lists. The difference is the time at which the elements are evaluated. With ordinary lists, both the car and the cdr are evaluated at construction time. With streams, the cdr is evaluated at selection time. +

    To make the stream implementation automatically and transparently interleave the construction of a stream with its use, we will arrange for the cdr of a stream to be evaluated when it is accessed by the stream-cdr procedure rather than when the stream is constructed by cons-stream. This implementation choice is reminiscent of our discussion of rational numbers in section 2.1.2, where we saw that we can choose to implement rational numbers so that the reduction of numerator and denominator to lowest terms is performed either at construction time or at selection time. The two rational-number implementations produce the same data abstraction, but the choice has an effect on efficiency. There is a similar relationship between streams and ordinary lists. As a data abstraction, streams are the same as lists. The difference is the time at which the elements are evaluated. With ordinary lists, both the car and the cdr are evaluated at construction time. With streams, the cdr is evaluated at selection time. -

    Our implementation of streams will be based on a special form called delay. Evaluating (delay >) does not evaluate the expression , but rather returns a so-called delayed object , which we can think of as a ``promise'' to evaluate at some future time. As a companion to delay, there is a procedure called force that takes a delayed object as argument and performs the evaluation---in effect, forcing the delay to fulfill its promise. We will see below how delay and force can be implemented, but first let us use these to construct streams. +

    Our implementation of streams will be based on a special form called delay. Evaluating (delay <exp>) does not evaluate the expression <exp>, but rather returns a so-called delayed object , which we can think of as a ``promise'' to evaluate <exp> at some future time. As a companion to delay, there is a procedure called force that takes a delayed object as argument and performs the evaluation---in effect, forcing the delay to fulfill its promise. We will see below how delay and force can be implemented, but first let us use these to construct streams. -Cons-stream is a special form defined so that +Cons-stream is a special form defined so that -

    -(cons-stream ) +
    +(cons-stream <a> <b>)
    -

    What this means is that we will construct streams using pairs. However, rather than placing the value of the rest of the stream into the cdr of the pair we will put there a promise to compute the rest if it is ever requested. Stream-car and stream-cdr can now be defined as procedures: +

    What this means is that we will construct streams using pairs. However, rather than placing the value of the rest of the stream into the cdr of the pair we will put there a promise to compute the rest if it is ever requested. Stream-car and stream-cdr can now be defined as procedures: -

    +
    (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream))) @@ -150,13 +149,13 @@

    3.5.1 Streams Are Delayed Lists

    C.prompt(); -

    Stream-car selects the car of the pair; stream-cdr selects the cdr of the pair and evaluates the delayed expression found there to obtain the rest of the stream.@footnote{Although stream-car and stream-cdr can be defined as procedures, cons-stream must be a special form. If cons-stream were a procedure, then, according to our model of evaluation, evaluating (cons-stream > <@var{b>)} would automatically cause to be evaluated, which is precisely what we do not want to happen. For the same reason, delay must be a special form, though force can be an ordinary procedure.} +

    Stream-car selects the car of the pair; stream-cdr selects the cdr of the pair and evaluates the delayed expression found there to obtain the rest of the stream.@footnote{Although stream-car and stream-cdr can be defined as procedures, cons-stream must be a special form. If cons-stream were a procedure, then, according to our model of evaluation, evaluating (cons-stream <a> <b>) would automatically cause <b> to be evaluated, which is precisely what we do not want to happen. For the same reason, delay must be a special form, though force can be an ordinary procedure.}

    The stream implementation in action

    To see how this implementation behaves, let us analyze the ``outrageous'' prime computation we saw above, reformulated in terms of streams: -

    +
    (stream-car (stream-cdr (stream-filter prime? @@ -168,9 +167,9 @@

    The stream implementation in action

    We will see that it does indeed work efficiently. -

    We begin by calling stream-enumerate-interval with the arguments 10,000 and 1,000,000. Stream-enumerate-interval is the stream analog of enumerate-interval (section 2.2.3): +

    We begin by calling stream-enumerate-interval with the arguments 10,000 and 1,000,000. Stream-enumerate-interval is the stream analog of enumerate-interval (section 2.2.3): -

    +
    (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream @@ -182,9 +181,9 @@

    The stream implementation in action

    C.prompt(); -

    and thus the result returned by stream-enumerate-interval, formed by the cons-stream, is@footnote{The numbers shown here do not really appear in the delayed expression. What actually appears is the original expression, in an environment in which the variables are bound to the appropriate numbers. For example, (+ low 1) with low bound to 10,000 actually appears where 10001 is shown.} +

    and thus the result returned by stream-enumerate-interval, formed by the cons-stream, is@footnote{The numbers shown here do not really appear in the delayed expression. What actually appears is the original expression, in an environment in which the variables are bound to the appropriate numbers. For example, (+ low 1) with low bound to 10,000 actually appears where 10001 is shown.} -

    +
    (cons 10000 (delay (stream-enumerate-interval 10001 1000000)))
    @@ -192,9 +191,9 @@

    The stream implementation in action

    C.prompt(); -

    That is, stream-enumerate-interval returns a stream represented as a pair whose car is 10,000 and whose cdr is a promise to enumerate more of the interval if so requested. This stream is now filtered for primes, using the stream analog of the filter procedure (section 2.2.3): +

    That is, stream-enumerate-interval returns a stream represented as a pair whose car is 10,000 and whose cdr is a promise to enumerate more of the interval if so requested. This stream is now filtered for primes, using the stream analog of the filter procedure (section 2.2.3): -

    +
    (define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) @@ -207,9 +206,9 @@

    The stream implementation in action

    C.prompt(); -

    Stream-filter tests the stream-car of the stream (the car of the pair, which is 10,000). Since this is not prime, stream-filter examines the stream-cdr of its input stream. The call to stream-cdr forces evaluation of the delayed stream-enumerate-interval, which now returns +

    Stream-filter tests the stream-car of the stream (the car of the pair, which is 10,000). Since this is not prime, stream-filter examines the stream-cdr of its input stream. The call to stream-cdr forces evaluation of the delayed stream-enumerate-interval, which now returns -

    +
    (cons 10001 (delay (stream-enumerate-interval 10002 1000000)))
    @@ -217,9 +216,9 @@

    The stream implementation in action

    C.prompt(); -

    Stream-filter now looks at the stream-car of this stream, 10,001, sees that this is not prime either, forces another stream-cdr, and so on, until stream-enumerate-interval yields the prime 10,007, whereupon stream-filter, according to its definition, returns +

    Stream-filter now looks at the stream-car of this stream, 10,001, sees that this is not prime either, forces another stream-cdr, and so on, until stream-enumerate-interval yields the prime 10,007, whereupon stream-filter, according to its definition, returns -

    +
    (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))
    @@ -230,7 +229,7 @@

    The stream implementation in action

    which in this case is -
    +
    (cons 10007 (delay (stream-filter @@ -244,9 +243,9 @@

    The stream implementation in action

    C.prompt(); -

    This result is now passed to stream-cdr in our original expression. This forces the delayed stream-filter, which in turn keeps forcing the delayed stream-enumerate-interval until it finds the next prime, which is 10,009. Finally, the result passed to stream-car in our original expression is +

    This result is now passed to stream-cdr in our original expression. This forces the delayed stream-filter, which in turn keeps forcing the delayed stream-enumerate-interval until it finds the next prime, which is 10,009. Finally, the result passed to stream-car in our original expression is -

    +
    (cons 10009 (delay (stream-filter @@ -260,16 +259,16 @@

    The stream implementation in action

    C.prompt(); -

    Stream-car returns 10,009, and the computation is complete. Only as many integers were tested for primality as were necessary to find the second prime, and the interval was enumerated only as far as was necessary to feed the prime filter. +

    Stream-car returns 10,009, and the computation is complete. Only as many integers were tested for primality as were necessary to find the second prime, and the interval was enumerated only as far as was necessary to feed the prime filter.

    In general, we can think of delayed evaluation as ``demand-driven''programming, whereby each stage in the stream process is activated only enough to satisfy the next stage. What we have done is to decouple the actual order of events in the computation from the apparent structure of our procedures. We write procedures as if the streams existed ``all at once'' when, in reality, the computation is performed incrementally, as in traditional programming styles. -

    Implementing delay and force

    +

    Implementing delay and force

    -

    Although delay and force may seem like mysterious operations, their implementation is really quite straightforward. Delay must package an expression so that it can be evaluated later on demand, and we can accomplish this simply by treating the expression as the body of a procedure. Delay can be a special form such that +

    Although delay and force may seem like mysterious operations, their implementation is really quite straightforward. Delay must package an expression so that it can be evaluated later on demand, and we can accomplish this simply by treating the expression as the body of a procedure. Delay can be a special form such that -

    -(delay ) +
    +(delay <exp>)
    -

    Force simply calls the procedure (of no arguments) produced by delay, so we can implement force as a procedure: +

    Force simply calls the procedure (of no arguments) produced by delay, so we can implement force as a procedure: -

    +
    (define (force delayed-object) (delayed-object))
    @@ -294,9 +293,9 @@

    Implementing delay and force

    C.prompt(); -

    This implementation suffices for delay and force to work as advertised, but there is an important optimization that we can include. In many applications, we end up forcing the same delayed object many times. This can lead to serious inefficiency in recursive programs involving streams. (See Exercise 3-57.) The solution is to build delayed objects so that the first time they are forced, they store the value that is computed. Subsequent forcings will simply return the stored value without repeating the computation. In other words, we implement delay as a special-purpose memoized procedure similar to the one described in Exercise 3-27. One way to accomplish this is to use the following procedure, which takes as argument a procedure (of no arguments) and returns a memoized version of the procedure. The first time the memoized procedure is run, it saves the computed result. On subsequent evaluations, it simply returns the result. +

    This implementation suffices for delay and force to work as advertised, but there is an important optimization that we can include. In many applications, we end up forcing the same delayed object many times. This can lead to serious inefficiency in recursive programs involving streams. (See Exercise 3-57.) The solution is to build delayed objects so that the first time they are forced, they store the value that is computed. Subsequent forcings will simply return the stored value without repeating the computation. In other words, we implement delay as a special-purpose memoized procedure similar to the one described in Exercise 3-27. One way to accomplish this is to use the following procedure, which takes as argument a procedure (of no arguments) and returns a memoized version of the procedure. The first time the memoized procedure is run, it saves the computed result. On subsequent evaluations, it simply returns the result. -

    +
    (define (memo-proc proc) (let ((already-run? false) (result false)) (lambda () @@ -310,38 +309,38 @@

    Implementing delay and force

    C.prompt(); -

    Delay is then defined so that (delay >) is equivalent to +

    Delay is then defined so that (delay <exp>) is equivalent to -

    -(memo-proc (lambda () )) +
    +(memo-proc (lambda () <exp>))
    -

    and force is as defined previously.@footnote{There are many possible implementations of streams other than the one described in this section. Delayed evaluation, which is the key to making streams practical, was inherent in Algol 60's call-by-name parameter-passing method. The use of this mechanism to implement streams was first described by Landin (1965). Delayed evaluation for streams was introduced into Lisp by Friedman and Wise (1976). In their implementation, cons always delays evaluating its arguments, so that lists automatically behave as streams. The memoizing optimization is also known as call-by-need . The Algol community would refer to our original delayed objects as call-by-name thunks and to the optimized versions as call-by-need thunks .} +

    and force is as defined previously.@footnote{There are many possible implementations of streams other than the one described in this section. Delayed evaluation, which is the key to making streams practical, was inherent in Algol 60's call-by-name parameter-passing method. The use of this mechanism to implement streams was first described by Landin (1965). Delayed evaluation for streams was introduced into Lisp by Friedman and Wise (1976). In their implementation, cons always delays evaluating its arguments, so that lists automatically behave as streams. The memoizing optimization is also known as call-by-need . The Algol community would refer to our original delayed objects as call-by-name thunks and to the optimized versions as call-by-need thunks .} -

    -

    Exercise 3.50: Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in section 2.2.3, footnote Footnote 12. +

    +

    Exercise 3.50: Complete the following definition, which generalizes stream-map to allow procedures that take multiple arguments, analogous to map in section 2.2.3, footnote Footnote 12. -

    +
    (define (stream-map proc . argstreams) - (if ( (car argstreams)) + (if (<??> (car argstreams)) the-empty-stream - ( - (apply proc (map argstreams)) + (<??> + (apply proc (map <??> argstreams)) (apply stream-map - (cons proc (map argstreams)))))) + (cons proc (map <??> argstreams))))))
    -
    +

    Exercise 3.51: In order to take a closer look at delayed evaluation, we will use the following procedure, which simply returns its argument after printing it: -

    +
    (define (show x) (display-line x) x) @@ -350,9 +349,9 @@

    Implementing delay and force

    C.prompt(); -

    What does the interpreter print in response to evaluating each expression in the following sequence?@footnote{Exercises such as Exercise 3-51 and Exercise 3-52 are valuable for testing our understanding of how delay works. On the other hand, intermixing delayed evaluation with printing---and, even worse, with assignment---is extremely confusing, and instructors of courses on computer languages have traditionally tormented their students with examination questions such as the ones in this section. Needless to say, writing programs that depend on such subtleties is odious programming style. Part of the power of stream processing is that it lets us ignore the order in which events actually happen in our programs. Unfortunately, this is precisely what we cannot afford to do in the presence of assignment, which forces us to be concerned with time and change.} +

    What does the interpreter print in response to evaluating each expression in the following sequence?@footnote{Exercises such as Exercise 3-51 and Exercise 3-52 are valuable for testing our understanding of how delay works. On the other hand, intermixing delayed evaluation with printing---and, even worse, with assignment---is extremely confusing, and instructors of courses on computer languages have traditionally tormented their students with examination questions such as the ones in this section. Needless to say, writing programs that depend on such subtleties is odious programming style. Part of the power of stream processing is that it lets us ignore the order in which events actually happen in our programs. Unfortunately, this is precisely what we cannot afford to do in the presence of assignment, which forces us to be concerned with time and change.} -

    +
    (define x (stream-map show (stream-enumerate-interval 0 10))) (stream-ref x 5) @@ -368,7 +367,7 @@

    Implementing delay and force

    Exercise 3.52: Consider the sequence of expressions -
    +
    (define sum 0) (define (accum x) @@ -388,15 +387,14 @@

    Implementing delay and force

    C.prompt(); -

    What is the value of sum after each of the above expressions is evaluated? What is the printed response to evaluating the stream-ref and display-stream expressions? Would these responses differ if we had implemented (delay >) simply as (lambda () >) without using the optimization provided by memo-proc? Explain +

    What is the value of sum after each of the above expressions is evaluated? What is the printed response to evaluating the stream-ref and display-stream expressions? Would these responses differ if we had implemented (delay <exp>) simply as (lambda () <exp>) without using the optimization provided by memo-proc? Explain

    - -

    3.5.2 Infinite Streams

    +

    3.5.2 Infinite Streams

    We have seen how to support the illusion of manipulating streams as complete entities even though, in actuality, we compute only as much of the stream as we need to access. We can exploit this technique to represent sequences efficiently as streams, even if the sequences are very long. What is more striking, we can use streams to represent sequences that are infinitely long. For instance, consider the following definition of the stream of positive integers: -

    +
    (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) @@ -406,11 +404,11 @@

    3.5.2 Infinite Streams

    C.prompt(); -

    This makes sense because integers will be a pair whose car is 1 and whose cdr is a promise to produce the integers beginning with 2. This is an infinitely long stream, but in any given time we can examine only a finite portion of it. Thus, our programs will never know that the entire infinite stream is not there. +

    This makes sense because integers will be a pair whose car is 1 and whose cdr is a promise to produce the integers beginning with 2. This is an infinitely long stream, but in any given time we can examine only a finite portion of it. Thus, our programs will never know that the entire infinite stream is not there. -

    Using integers we can define other infinite streams, such as the stream of integers that are not divisible by 7: +

    Using integers we can define other infinite streams, such as the stream of integers that are not divisible by 7: -

    +
    (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens @@ -423,7 +421,7 @@

    3.5.2 Infinite Streams

    Then we can find integers not divisible by 7 simply by accessing elements of this stream: -

    +
    (stream-ref no-sevens 100) 117
    @@ -431,9 +429,9 @@

    3.5.2 Infinite Streams

    C.prompt(); -

    In analogy with integers, we can define the infinite stream of Fibonacci numbers: +

    In analogy with integers, we can define the infinite stream of Fibonacci numbers: -

    +
    (define (fibgen a b) (cons-stream a (fibgen b (+ a b)))) @@ -443,11 +441,11 @@

    3.5.2 Infinite Streams

    C.prompt(); -

    Fibs is a pair whose car is 0 and whose cdr is a promise to evaluate (fibgen 1 1). When we evaluate this delayed (fibgen 1 1), it will produce a pair whose car is 1 and whose cdr is a promise to evaluate (fibgen 1 2), and so on. +

    Fibs is a pair whose car is 0 and whose cdr is a promise to evaluate (fibgen 1 1). When we evaluate this delayed (fibgen 1 1), it will produce a pair whose car is 1 and whose cdr is a promise to evaluate (fibgen 1 2), and so on. -

    For a look at a more exciting infinite stream, we can generalize the no-sevens example to construct the infinite stream of prime numbers, using a method known as the sieve of Eratosthenes .@footnote{Eratosthenes, a third-century @acronym{B.C.} Alexandrian Greek philosopher, is famous for giving the first accurate estimate of the circumference of the Earth, which he computed by observing shadows cast at noon on the day of the summer solstice. Eratosthenes's sieve method, although ancient, has formed the basis for special-purpose hardware ``sieves''that, until recently, were the most powerful tools in existence for locating large primes. Since the 70s, however, these methods have been superseded by outgrowths of the probabilistic techniques discussed in section 1.2.6.} We start with the integers beginning with 2, which is the first prime. To get the rest of the primes, we start by filtering the multiples of 2 from the rest of the integers. This leaves a stream beginning with 3, which is the next prime. Now we filter the multiples of 3 from the rest of this stream. This leaves a stream beginning with 5, which is the next prime, and so on. In other words, we construct the primes by a sieving process, described as follows: To sieve a stream S, form a stream whose first element is the first element of S and the rest of which is obtained by filtering all multiples of the first element of S out of the rest of S and sieving the result. This process is readily described in terms of stream operations: +

    For a look at a more exciting infinite stream, we can generalize the no-sevens example to construct the infinite stream of prime numbers, using a method known as the sieve of Eratosthenes .@footnote{Eratosthenes, a third-century @acronym{B.C.} Alexandrian Greek philosopher, is famous for giving the first accurate estimate of the circumference of the Earth, which he computed by observing shadows cast at noon on the day of the summer solstice. Eratosthenes's sieve method, although ancient, has formed the basis for special-purpose hardware ``sieves''that, until recently, were the most powerful tools in existence for locating large primes. Since the 70s, however, these methods have been superseded by outgrowths of the probabilistic techniques discussed in section 1.2.6.} We start with the integers beginning with 2, which is the first prime. To get the rest of the primes, we start by filtering the multiples of 2 from the rest of the integers. This leaves a stream beginning with 3, which is the next prime. Now we filter the multiples of 3 from the rest of this stream. This leaves a stream beginning with 5, which is the next prime, and so on. In other words, we construct the primes by a sieving process, described as follows: To sieve a stream S, form a stream whose first element is the first element of S and the rest of which is obtained by filtering all multiples of the first element of S out of the rest of S and sieving the result. This process is readily described in terms of stream operations: -

    +
    (define (sieve stream) (cons-stream (stream-car stream) @@ -464,7 +462,7 @@

    3.5.2 Infinite Streams

    Now to find a particular prime we need only ask for it: -

    +
    (stream-ref primes 50) 233
    @@ -472,7 +470,7 @@

    3.5.2 Infinite Streams

    C.prompt(); -

    It is interesting to contemplate the signal-processing system set up by sieve, shown in the ``Henderson diagram'' in Figure 3-31.@footnote{We have named these figures after Peter Henderson, who was the first person to show us diagrams of this sort as a way of thinking about stream processing. Each solid line represents a stream of values being transmitted. The dashed line from the car to the cons and the filter indicates that this is a single value rather than a stream.} The input stream feeds into an ``unconser'' that separates the first element of the stream from the rest of the stream. The first element is used to construct a divisibility filter, through which the rest is passed, and the output of the filter is fed to another sieve box. Then the original first element is consed onto the output of the internal sieve to form the output stream. Thus, not only is the stream infinite, but the signal processor is also infinite, because the sieve contains a sieve within it. +

    It is interesting to contemplate the signal-processing system set up by sieve, shown in the ``Henderson diagram'' in Figure 3-31.@footnote{We have named these figures after Peter Henderson, who was the first person to show us diagrams of this sort as a way of thinking about stream processing. Each solid line represents a stream of values being transmitted. The dashed line from the car to the cons and the filter indicates that this is a single value rather than a stream.} The input stream feeds into an ``unconser'' that separates the first element of the stream from the rest of the stream. The first element is used to construct a divisibility filter, through which the rest is passed, and the output of the filter is fed to another sieve box. Then the original first element is consed onto the output of the internal sieve to form the output stream. Thus, not only is the stream infinite, but the signal processor is also infinite, because the sieve contains a sieve within it.

    Figure 3.31: The prime sieve viewed as a @@ -485,7 +483,7 @@

    3.5.2 Infinite Streams

    | __/| |\__ | | __/car|........................................| \__ | | _/ | : | \_ | -----><_ | V | cons _>----> +----><_ | V | cons _>----> | \__ | +------------+ +------------+ | __/ | | \cdr|--->| filter: | | sieve |--->| __/ | | \| | |--->| | |/ | @@ -498,20 +496,20 @@

    3.5.2 Infinite Streams

    Defining streams implicitly

    -

    The integers and fibs streams above were defined by specifying ``generating'' procedures that explicitly compute the stream elements one by one. An alternative way to specify streams is to take advantage of delayed evaluation to define streams implicitly. For example, the following expression defines the stream ones to be an infinite stream of ones: +

    The integers and fibs streams above were defined by specifying ``generating'' procedures that explicitly compute the stream elements one by one. An alternative way to specify streams is to take advantage of delayed evaluation to define streams implicitly. For example, the following expression defines the stream ones to be an infinite stream of ones: -

    +
    (define ones (cons-stream 1 ones))
    -

    This works much like the definition of a recursive procedure: ones is a pair whose car is 1 and whose cdr is a promise to evaluate ones. Evaluating the cdr gives us again a 1 and a promise to evaluate ones, and so on. +

    This works much like the definition of a recursive procedure: ones is a pair whose car is 1 and whose cdr is a promise to evaluate ones. Evaluating the cdr gives us again a 1 and a promise to evaluate ones, and so on. -

    We can do more interesting things by manipulating streams with operations such as add-streams, which produces the elementwise sum of two given streams:@footnote{This uses the generalized version of stream-map from Exercise 3-50.} +

    We can do more interesting things by manipulating streams with operations such as add-streams, which produces the elementwise sum of two given streams:@footnote{This uses the generalized version of stream-map from Exercise 3-50.} -

    +
    (define (add-streams s1 s2) (stream-map + s1 s2))
    @@ -521,18 +519,18 @@

    Defining streams implicitly

    Now we can define the integers as follows: -

    +
    (define integers (cons-stream 1 (add-streams ones integers)))
    -

    This defines integers to be a stream whose first element is 1 and the rest of which is the sum of ones and integers. Thus, the second element of integers is 1 plus the first element of integers, or 2; the third element of integers is 1 plus the second element of integers, or 3; and so on. This definition works because, at any point, enough of the integers stream has been generated so that we can feed it back into the definition to produce the next integer. +

    This defines integers to be a stream whose first element is 1 and the rest of which is the sum of ones and integers. Thus, the second element of integers is 1 plus the first element of integers, or 2; the third element of integers is 1 plus the second element of integers, or 3; and so on. This definition works because, at any point, enough of the integers stream has been generated so that we can feed it back into the definition to produce the next integer.

    We can define the Fibonacci numbers in the same style: -

    +
    (define fibs (cons-stream 0 (cons-stream 1 @@ -543,17 +541,17 @@

    Defining streams implicitly

    C.prompt(); -

    This definition says that fibs is a stream beginning with 0 and 1, such that the rest of the stream can be generated by adding fibs to itself shifted by one place: +

    This definition says that fibs is a stream beginning with 0 and 1, such that the rest of the stream can be generated by adding fibs to itself shifted by one place:

    -      1  1  2  3  5  8   13  21  ... = (stream-cdr fibs)
    -      0  1  1  2  3  5   8   13  ... = fibs
    -0  1  1  2  3  5  8  13  21  34  ... = fibs
    +      1  1  2  3  5  8   13  21  ... = (stream-cdr fibs)
    +      0  1  1  2  3  5   8   13  ... = fibs
    +0  1  1  2  3  5  8  13  21  34  ... = fibs
     
    -

    Scale-stream is another useful procedure in formulating such stream definitions. This multiplies each item in a stream by a given constant: +

    Scale-stream is another useful procedure in formulating such stream definitions. This multiplies each item in a stream by a given constant: -

    +
    (define (scale-stream stream factor) (stream-map (lambda (x) (* x factor)) stream))
    @@ -563,7 +561,7 @@

    Defining streams implicitly

    For example, -
    +
    (define double (cons-stream 1 (scale-stream double 2)))
    -

    This is a recursive definition, since primes is defined in terms of the prime? predicate, which itself uses the primes stream. The reason this procedure works is that, at any point, enough of the primes stream has been generated to test the primality of the numbers we need to check next. That is, for every n we test for primality, either n is not prime (in which case there is a prime already generated that divides it) or n is prime (in which case there is a prime already generated---i.e., a prime less than n---that is greater than [sqrt](n)).@footnote{This last point is very subtle and relies on the fact that p_(n+1) <= p_n^2. (Here, p_k denotes the kth prime.) Estimates such as these are very difficult to establish. The ancient proof by Euclid that there are an infinite number of primes shows that p_(n+1)<= p_1 p_2...p_n + 1, and no substantially better result was proved until 1851, when the Russian mathematician P. L. Chebyshev established that p_(n+1)<= 2p_n for all n. This result, originally conjectured in 1845, is known as Bertrand's hypothesis . A proof can be found in section 22.3 of Hardy and Wright 1960.} +

    This is a recursive definition, since primes is defined in terms of the prime? predicate, which itself uses the primes stream. The reason this procedure works is that, at any point, enough of the primes stream has been generated to test the primality of the numbers we need to check next. That is, for every n we test for primality, either n is not prime (in which case there is a prime already generated that divides it) or n is prime (in which case there is a prime already generated---i.e., a prime less than n---that is greater than [sqrt](n)).@footnote{This last point is very subtle and relies on the fact that p_(n+1) <= p_n^2. (Here, p_k denotes the kth prime.) Estimates such as these are very difficult to establish. The ancient proof by Euclid that there are an infinite number of primes shows that p_(n+1)<= p_1 p_2...p_n + 1, and no substantially better result was proved until 1851, when the Russian mathematician P. L. Chebyshev established that p_(n+1)<= 2p_n for all n. This result, originally conjectured in 1845, is known as Bertrand's hypothesis . A proof can be found in section 22.3 of Hardy and Wright 1960.}

    Exercise 3.53: Without running the program, describe the elements of the stream defined by -
    +
    (define s (cons-stream 1 (add-streams s s)))
    -Then the required stream may be constructed with merge, as follows: +Then the required stream may be constructed with merge, as follows: -
    -(define S (cons-stream 1 (merge ))) +
    +(define S (cons-stream 1 (merge <??> <??>)))
    -Fill in the missing expressions in the places marked above. +Fill in the missing expressions in the places marked <??> above.
    -Exercise 3.57: How many additions are performed when we compute the nth Fibonacci number using the definition of fibs based on the add-streams procedure? Show that the number of additions would be exponentially greater if we had implemented (delay >) simply as (lambda () >), without using the optimization provided by the memo-proc procedure described in section 3.5.1.@footnote{This exercise shows how call-by-need is closely related to ordinary memoization as described in Exercise 3-27. In that exercise, we used assignment to explicitly construct a local table. Our call-by-need stream optimization effectively constructs such a table automatically, storing values in the previously forced parts of the stream.} +Exercise 3.57: How many additions are performed when we compute the nth Fibonacci number using the definition of fibs based on the add-streams procedure? Show that the number of additions would be exponentially greater if we had implemented (delay <exp>) simply as (lambda () <exp>), without using the optimization provided by the memo-proc procedure described in section 3.5.1.@footnote{This exercise shows how call-by-need is closely related to ordinary memoization as described in Exercise 3-27. In that exercise, we used assignment to explicitly construct a local table. Our call-by-need stream optimization effectively constructs such a table automatically, storing values in the previously forced parts of the stream.}
    Exercise 3.58: Give an interpretation of the stream computed by the following procedure: -
    +
    (define (expand num den radix) (cons-stream (quotient (* num radix) den) @@ -701,11 +699,11 @@

    Defining streams implicitly

    C.prompt(); -(Quotient is a primitive that returns the integer quotient of two integers.) What are the successive elements produced by (expand 1 7 10)? What is produced by (expand 3 8 10)? +(Quotient is a primitive that returns the integer quotient of two integers.) What are the successive elements produced by (expand 1 7 10)? What is produced by (expand 3 8 10)?
    -Exercise 3.59: In section 2.5.3 we saw how to implement a polynomial arithmetic system representing polynomials as lists of terms. In a similar way, we can work with power series , such as +Exercise 3.59: In section 2.5.3 we saw how to implement a polynomial arithmetic system representing polynomials as lists of terms. In a similar way, we can work with power series , such as
                    x^2     x^3       x^4
    @@ -724,7 +722,8 @@ 

    Defining streams implicitly

    represented as infinite streams. We will represent the series a_0 + a_1 x + a_2 x^2 + a_3 x^3 + ... as the stream whose elements are the coefficients a_0, a_1, a_2, a_3, ....
      -@item +
    • + The integral of the series a_0 + a_1 x + a_2 x^2 + a_3 x^3 + ... is the series
      @@ -733,12 +732,13 @@ 

      Defining streams implicitly

      2 3 4
      -where c is any constant. Define a procedure integrate-series that takes as input a stream a_0, a_1, a_2, ... representing a power series and returns the stream a_0, (1/2)a_1, (1/3)a_2, ... of coefficients of the non-constant terms of the integral of the series. (Since the result has no constant term, it doesn't represent a power series; when we use integrate-series, we will cons on the appropriate constant.) - -@item +where c is any constant. Define a procedure integrate-series that takes as input a stream a_0, a_1, a_2, ... representing a power series and returns the stream a_0, (1/2)a_1, (1/3)a_2, ... of coefficients of the non-constant terms of the integral of the series. (Since the result has no constant term, it doesn't represent a power series; when we use integrate-series, we will cons on the appropriate constant.) +
    • +
    • + The function x |-> e^x is its own derivative. This implies that e^x and the integral of e^x are the same series, except for the constant term, which is e^0 = 1. Accordingly, we can generate the series for e^x as -
      +
      (define exp-series (cons-stream 1 (integrate-series exp-series)))
      @@ -748,25 +748,26 @@

      Defining streams implicitly

      Show how to generate the series for sine and cosine, starting from the facts that the derivative of sine is cosine and the derivative of cosine is the negative of sine: -
      +
      (define cosine-series - (cons-stream 1 )) + (cons-stream 1 <??>)) (define sine-series - (cons-stream 0 )) + (cons-stream 0 <??>))
      +
    -Exercise 3.60: With power series represented as streams of coefficients as in Exercise 3-59, adding series is implemented by add-streams. Complete the definition of the following procedure for multiplying series: +Exercise 3.60: With power series represented as streams of coefficients as in Exercise 3-59, adding series is implemented by add-streams. Complete the definition of the following procedure for multiplying series: -
    +
    (define (mul-series s1 s2) - (cons-stream (add-streams ))) + (cons-stream <??> (add-streams <??> <??>)))
    -In our original sqrt procedure, we made these guesses be the successive values of a state variable. Instead we can generate the infinite stream of guesses, starting with an initial guess of 1:@footnote{We can't use let to bind the local variable guesses, because the value of guesses depends on guesses itself. Exercise 3-63 addresses why we want a local variable here.} +In our original sqrt procedure, we made these guesses be the successive values of a state variable. Instead we can generate the infinite stream of guesses, starting with an initial guess of 1:@footnote{We can't use let to bind the local variable guesses, because the value of guesses depends on guesses itself. Exercise 3-63 addresses why we want a local variable here.} -
    +
    (define (sqrt-stream x) (define guesses (cons-stream 1.0 @@ -844,9 +844,9 @@

    Formulating iterations as stream processes

    4 3 5 7 -We first generate the stream of summands of the series (the reciprocals of the odd integers, with alternating signs). Then we take the stream of sums of more and more terms (using the partial-sums procedure of Exercise 3-55) and scale the result by 4: +We first generate the stream of summands of the series (the reciprocals of the odd integers, with alternating signs). Then we take the stream of sums of more and more terms (using the partial-sums procedure of Exercise 3-55) and scale the result by 4: -
    +
    (define (pi-summands n) (cons-stream (/ 1.0 n) (stream-map - (pi-summands (+ n 2))))) @@ -871,7 +871,7 @@

    Formulating iterations as stream processes

    This gives us a stream of better and better approximations to [pi], although the approximations converge rather slowly. Eight terms of the sequence bound the value of [pi] between 3.284 and 3.017. -So far, our use of the stream of states approach is not much different from updating state variables. But streams give us an opportunity to do some interesting tricks. For example, we can transform a stream with a sequence accelerator that converts a sequence of approximations to a new sequence that converges to the same value as the original, only faster. +So far, our use of the stream of states approach is not much different from updating state variables. But streams give us an opportunity to do some interesting tricks. For example, we can transform a stream with a sequence accelerator that converts a sequence of approximations to a new sequence that converges to the same value as the original, only faster. One such accelerator, due to the eighteenth-century Swiss mathematician Leonhard Euler, works well with sequences that are partial sums of alternating series (series of terms with alternating signs). In Euler's technique, if S_n is the nth term of the original sum sequence, then the accelerated sequence has terms @@ -883,7 +883,7 @@

    Formulating iterations as stream processes

    Thus, if the original sequence is represented as a stream of values, the transformed sequence is given by -
    +
    (define (euler-transform s) (let ((s0 (stream-ref s 0)) ; S_(n-1) (s1 (stream-ref s 1)) ; S_n @@ -898,7 +898,7 @@

    Formulating iterations as stream processes

    We can demonstrate Euler acceleration with our sequence of approximations to [pi]: -
    +
    (display-stream (euler-transform pi-stream)) 3.166666666666667 3.1333333333333337 @@ -914,9 +914,9 @@

    Formulating iterations as stream processes

    C.prompt(); -Even better, we can accelerate the accelerated sequence, and recursively accelerate that, and so on. Namely, we create a stream of streams (a structure we'll call a tableau ) in which each stream is the transform of the preceding one: +Even better, we can accelerate the accelerated sequence, and recursively accelerate that, and so on. Namely, we create a stream of streams (a structure we'll call a tableau ) in which each stream is the transform of the preceding one: -
    +
    (define (make-tableau transform s) (cons-stream s (make-tableau transform @@ -937,7 +937,7 @@

    Formulating iterations as stream processes

    Finally, we form a sequence by taking the first term in each row of the tableau: -
    +
    (define (accelerated-sequence transform s) (stream-map stream-car (make-tableau transform s))) @@ -948,7 +948,7 @@

    Formulating iterations as stream processes

    We can demonstrate this kind of ``super-acceleration'' of the [pi] sequence: -
    +
    (display-stream (accelerated-sequence euler-transform pi-stream)) 4. @@ -970,9 +970,9 @@

    Formulating iterations as stream processes

    We could have implemented these acceleration techniques without using streams. But the stream formulation is particularly elegant and convenient because the entire sequence of states is available to us as a data structure that can be manipulated with a uniform set of operations.
    -Exercise 3.63: Louis Reasoner asks why the sqrt-stream procedure was not written in the following more straightforward way, without the local variable guesses: +Exercise 3.63: Louis Reasoner asks why the sqrt-stream procedure was not written in the following more straightforward way, without the local variable guesses: -
    +
    (define (sqrt-stream x) (cons-stream 1.0 (stream-map (lambda (guess) @@ -983,13 +983,13 @@

    Formulating iterations as stream processes

    C.prompt(); -Alyssa P. Hacker replies that this version of the procedure is considerably less efficient because it performs redundant computation. Explain Alyssa's answer. Would the two versions still differ in efficiency if our implementation of delay used only (lambda () >) without using the optimization provided by memo-proc (section 3.5.1)? +Alyssa P. Hacker replies that this version of the procedure is considerably less efficient because it performs redundant computation. Explain Alyssa's answer. Would the two versions still differ in efficiency if our implementation of delay used only (lambda () <exp>) without using the optimization provided by memo-proc (section 3.5.1)?
    -Exercise 3.64: Write a procedure stream-limit that takes as arguments a stream and a number (the tolerance). It should examine the stream until it finds two successive elements that differ in absolute value by less than the tolerance, and return the second of the two elements. Using this, we could compute square roots up to a given tolerance by +Exercise 3.64: Write a procedure stream-limit that takes as arguments a stream and a number (the tolerance). It should examine the stream until it finds two successive elements that differ in absolute value by less than the tolerance, and return the second of the two elements. Using this, we could compute square roots up to a given tolerance by -
    +
    (define (sqrt x tolerance) (stream-limit (sqrt-stream x) tolerance))
    @@ -1014,9 +1014,9 @@

    Infinite streams of pairs

    In section 2.2.3, we saw how the sequence paradigm handles traditional nested loops as processes defined on sequences of pairs. If we generalize this technique to infinite streams, then we can write programs that are not easily represented as loops, because the ``looping'' must range over an infinite set. -For example, suppose we want to generalize the prime-sum-pairs procedure of section 2.2.3 to produce the stream of pairs of all integers (i,j) with i <= j such that i + j is prime. If int-pairs is the sequence of all pairs of integers (i,j) with i <= j, then our required stream is simply@footnote{As in section 2.2.3, we represent a pair of integers as a list rather than a Lisp pair.} +For example, suppose we want to generalize the prime-sum-pairs procedure of section 2.2.3 to produce the stream of pairs of all integers (i,j) with i <= j such that i + j is prime. If int-pairs is the sequence of all pairs of integers (i,j) with i <= j, then our required stream is simply@footnote{As in section 2.2.3, we represent a pair of integers as a list rather than a Lisp pair.} -
    +
    (stream-filter (lambda (pair) (prime? (+ (car pair) (cadr pair)))) int-pairs) @@ -1025,7 +1025,7 @@

    Infinite streams of pairs

    C.prompt(); -Our problem, then, is to produce the stream int-pairs. More generally, suppose we have two streams S = (S_i) and T = (T_j), and imagine the infinite rectangular array +Our problem, then, is to produce the stream int-pairs. More generally, suppose we have two streams S = (S_i) and T = (T_j), and imagine the infinite rectangular array
     (S_0, T_0)  (S_0, T_1)  (S_0, T_2)  ...
    @@ -1043,9 +1043,9 @@ 

    Infinite streams of pairs

    ...
    -(If we take both S and T to be the stream of integers, then this will be our desired stream int-pairs.) +(If we take both S and T to be the stream of integers, then this will be our desired stream int-pairs.) -Call the general stream of pairs (pairs S T), and consider it to be composed of three parts: the pair (S_0,T_0), the rest of the pairs in the first row, and the remaining pairs:@footnote{See Exercise 3-68 for some insight into why we chose this decomposition.} +Call the general stream of pairs (pairs S T), and consider it to be composed of three parts: the pair (S_0,T_0), the rest of the pairs in the first row, and the remaining pairs:@footnote{See Exercise 3-68 for some insight into why we chose this decomposition.}
     (S_0, T_0) | (S_0, T_1)  (S_0, T_2)  ...
    @@ -1055,9 +1055,9 @@ 

    Infinite streams of pairs

    | ...
    -Observe that the third piece in this decomposition (pairs that are not in the first row) is (recursively) the pairs formed from (stream-cdr S) and (stream-cdr T). Also note that the second piece (the rest of the first row) is +Observe that the third piece in this decomposition (pairs that are not in the first row) is (recursively) the pairs formed from (stream-cdr S) and (stream-cdr T). Also note that the second piece (the rest of the first row) is -
    +
    (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t))
    @@ -1067,11 +1067,11 @@

    Infinite streams of pairs

    Thus we can form our stream of pairs as follows: -
    +
    (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) - ( + (<combine-in-some-way> (stream-map (lambda (x) (list (stream-car s) x)) (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) @@ -1080,9 +1080,9 @@

    Infinite streams of pairs

    C.prompt(); -In order to complete the procedure, we must choose some way to combine the two inner streams. One idea is to use the stream analog of the append procedure from section 2.2.1: +In order to complete the procedure, we must choose some way to combine the two inner streams. One idea is to use the stream analog of the append procedure from section 2.2.1: -
    +
    (define (stream-append s1 s2) (if (stream-null? s1) s2 @@ -1095,7 +1095,7 @@

    Infinite streams of pairs

    This is unsuitable for infinite streams, however, because it takes all the elements from the first stream before incorporating the second stream. In particular, if we try to generate all pairs of positive integers using -
    +
    (pairs integers integers)
    -Since interleave takes elements alternately from the two streams, every element of the second stream will eventually find its way into the interleaved stream, even if the first stream is infinite. +Since interleave takes elements alternately from the two streams, every element of the second stream will eventually find its way into the interleaved stream, even if the first stream is infinite. We can thus generate the required stream of pairs as -
    +
    (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) @@ -1135,17 +1135,17 @@

    Infinite streams of pairs

    -Exercise 3.66: Examine the stream (pairs integers integers). Can you make any general comments about the order in which the pairs are placed into the stream? For example, about how many pairs precede the pair (1,100)? the pair (99,100)? the pair (100,100)? (If you can make precise mathematical statements here, all the better. But feel free to give more qualitative answers if you find yourself getting bogged down.) +Exercise 3.66: Examine the stream (pairs integers integers). Can you make any general comments about the order in which the pairs are placed into the stream? For example, about how many pairs precede the pair (1,100)? the pair (99,100)? the pair (100,100)? (If you can make precise mathematical statements here, all the better. But feel free to give more qualitative answers if you find yourself getting bogged down.)
    -Exercise 3.67: Modify the pairs procedure so that (pairs integers integers) will produce the stream of all pairs of integers (i,j) (without the condition i <= j). Hint: You will need to mix in an additional stream. +Exercise 3.67: Modify the pairs procedure so that (pairs integers integers) will produce the stream of all pairs of integers (i,j) (without the condition i <= j). Hint: You will need to mix in an additional stream.
    Exercise 3.68: Louis Reasoner thinks that building a stream of pairs from three parts is unnecessarily complicated. Instead of separating the pair (S_0,T_0) from the rest of the pairs in the first row, he proposes to work with the whole first row, as follows: -
    +
    (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) @@ -1156,31 +1156,33 @@

    Infinite streams of pairs

    C.prompt(); -Does this work? Consider what happens if we evaluate (pairs integers integers) using Louis's definition of pairs. +Does this work? Consider what happens if we evaluate (pairs integers integers) using Louis's definition of pairs.
    -Exercise 3.69: Write a procedure triples that takes three infinite streams, S, T, and U, and produces the stream of triples (S_i,T_j,U_k) such that i <= j <= k. Use triples to generate the stream of all Pythagorean triples of positive integers, i.e., the triples (i,j,k) such that i <= j and i^2 + j^2 = k^2. +Exercise 3.69: Write a procedure triples that takes three infinite streams, S, T, and U, and produces the stream of triples (S_i,T_j,U_k) such that i <= j <= k. Use triples to generate the stream of all Pythagorean triples of positive integers, i.e., the triples (i,j,k) such that i <= j and i^2 + j^2 = k^2.
    -Exercise 3.70: It would be nice to be able to generate streams in which the pairs appear in some useful order, rather than in the order that results from an ad hoc interleaving process. We can use a technique similar to the merge procedure of Exercise 3-56, if we define a way to say that one pair of integers is ``less than'' another. One way to do this is to define a ``weighting function'' W(i,j) and stipulate that (i_1,j_1) is less than (i_2,j_2) if W(i_1,j_1) < W(i_2,j_2). Write a procedure merge-weighted that is like merge, except that merge-weighted takes an additional argument weight, which is a procedure that computes the weight of a pair, and is used to determine the order in which elements should appear in the resulting merged stream.@footnote{We will require that the weighting function be such that the weight of a pair increases as we move out along a row or down along a column of the array of pairs.} Using this, generalize pairs to a procedure weighted-pairs that takes two streams, together with a procedure that computes a weighting function, and generates the stream of pairs, ordered according to weight. Use your procedure to generate +Exercise 3.70: It would be nice to be able to generate streams in which the pairs appear in some useful order, rather than in the order that results from an ad hoc interleaving process. We can use a technique similar to the merge procedure of Exercise 3-56, if we define a way to say that one pair of integers is ``less than'' another. One way to do this is to define a ``weighting function'' W(i,j) and stipulate that (i_1,j_1) is less than (i_2,j_2) if W(i_1,j_1) < W(i_2,j_2). Write a procedure merge-weighted that is like merge, except that merge-weighted takes an additional argument weight, which is a procedure that computes the weight of a pair, and is used to determine the order in which elements should appear in the resulting merged stream.@footnote{We will require that the weighting function be such that the weight of a pair increases as we move out along a row or down along a column of the array of pairs.} Using this, generalize pairs to a procedure weighted-pairs that takes two streams, together with a procedure that computes a weighting function, and generates the stream of pairs, ordered according to weight. Use your procedure to generate
      -@item -the stream of all pairs of positive integers (i,j) with i <= j +
    • + +the stream of all pairs of positive integers (i,j) with i <= j ordered according to the sum i + j - -@item -the stream of all pairs of positive integers (i,j) with i <= j, +
    • +
    • + +the stream of all pairs of positive integers (i,j) with i <= j, where neither i nor j is divisible by 2, 3, or 5, and the pairs are ordered according to the sum 2 i + 3 j + 5 i j. - +
    -Exercise 3.71: Numbers that can be expressed as the sum of two cubes in more than one way are sometimes called Ramanujan numbers , in honor of the mathematician Srinivasa Ramanujan.@footnote{To quote from G. H. Hardy's obituary of Ramanujan (Hardy 1921): ``It was Mr. Littlewood (I believe) who remarked that `every positive integer was one of his friends.' I remember once going to see him when he was lying ill at Putney. I had ridden in taxi-cab No. 1729, and remarked that the number seemed to me a rather dull one, and that I hoped it was not an unfavorable omen. `No,' he replied, `it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways.''' The trick of using weighted pairs to generate the Ramanujan numbers was shown to us by Charles Leiserson.} Ordered streams of pairs provide an elegant solution to the problem of computing these numbers. To find a number that can be written as the sum of two cubes in two different ways, we need only generate the stream of pairs of integers (i,j) weighted according to the sum i^3 + j^3 (see Exercise 3-70), then search the stream for two consecutive pairs with the same weight. Write a procedure to generate the Ramanujan numbers. The first such number is 1,729. What are the next five? +Exercise 3.71: Numbers that can be expressed as the sum of two cubes in more than one way are sometimes called Ramanujan numbers , in honor of the mathematician Srinivasa Ramanujan.@footnote{To quote from G. H. Hardy's obituary of Ramanujan (Hardy 1921): ``It was Mr. Littlewood (I believe) who remarked that `every positive integer was one of his friends.' I remember once going to see him when he was lying ill at Putney. I had ridden in taxi-cab No. 1729, and remarked that the number seemed to me a rather dull one, and that I hoped it was not an unfavorable omen. `No,' he replied, `it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways.''' The trick of using weighted pairs to generate the Ramanujan numbers was shown to us by Charles Leiserson.} Ordered streams of pairs provide an elegant solution to the problem of computing these numbers. To find a number that can be written as the sum of two cubes in two different ways, we need only generate the stream of pairs of integers (i,j) weighted according to the sum i^3 + j^3 (see Exercise 3-70), then search the stream for two consecutive pairs with the same weight. Write a procedure to generate the Ramanujan numbers. The first such number is 1,729. What are the next five?
    @@ -1189,7 +1191,7 @@

    Infinite streams of pairs

    Streams as signals

    -We began our discussion of streams by describing them as computational analogs of the ``signals'' in signal-processing systems. In fact, we can use streams to model signal-processing systems in a very direct way, representing the values of a signal at successive time intervals as consecutive elements of a stream. For instance, we can implement an integrator or summer that, for an input stream x = (x_i), an initial value C, and a small increment dt, accumulates the sum +We began our discussion of streams by describing them as computational analogs of the ``signals'' in signal-processing systems. In fact, we can use streams to model signal-processing systems in a very direct way, representing the values of a signal at successive time intervals as consecutive elements of a stream. For instance, we can implement an integrator or summer that, for an input stream x = (x_i), an initial value C, and a small increment dt, accumulates the sum
                i
    @@ -1199,9 +1201,9 @@ 

    Streams as signals

    j=1
    -and returns the stream of values S = (S_i). The following integral procedure is reminiscent of the ``implicit style'' definition of the stream of integers (section 3.5.2): +and returns the stream of values S = (S_i). The following integral procedure is reminiscent of the ``implicit style'' definition of the stream of integers (section 3.5.2): -
    +
    (define (integral integrand initial-value dt) (define int (cons-stream initial-value @@ -1213,10 +1215,10 @@

    Streams as signals

    C.prompt(); -Figure 3-32 is a picture of a signal-processing system that corresponds to the integral procedure. The input stream is scaled by dt and passed through an adder, whose output is passed back through the same adder. The self-reference in the definition of int is reflected in the figure by the feedback loop that connects the output of the adder to one of the inputs. +Figure 3-32 is a picture of a signal-processing system that corresponds to the integral procedure. The input stream is scaled by dt and passed through an adder, whose output is passed back through the same adder. The self-reference in the definition of int is reflected in the figure by the feedback loop that connects the output of the adder to one of the inputs.
    -Figure 3.32: The integral procedure viewed +Figure 3.32: The integral procedure viewed as a signal-processing system.
    @@ -1234,9 +1236,9 @@ 

    Streams as signals

    -Exercise 3.73: We can model electrical circuits using streams to represent the values of currents or voltages at a sequence of times. For instance, suppose we have an RC circuit consisting of a resistor of resistance R and a capacitor of capacitance C in series. The voltage response v of the circuit to an injected current i is determined by the formula in Figure 3-33, whose structure is shown by the accompanying signal-flow diagram. +Exercise 3.73: We can model electrical circuits using streams to represent the values of currents or voltages at a sequence of times. For instance, suppose we have an RC circuit consisting of a resistor of resistance R and a capacitor of capacitance C in series. The voltage response v of the circuit to an injected current i is determined by the formula in Figure 3-33, whose structure is shown by the accompanying signal-flow diagram. -Write a procedure RC that models this circuit. RC should take as inputs the values of R, C, and dt and should return a procedure that takes as inputs a stream representing the current i and an initial value for the capacitor voltage v_0 and produces as output the stream of voltages v. For example, you should be able to use RC to model an RC circuit with R = 5 ohms, C = 1 farad, and a 0.5-second time step by evaluating (define RC1 (RC 5 1 0.5)). This defines RC1 as a procedure that takes a stream representing the time sequence of currents and an initial capacitor voltage and produces the output stream of voltages. +Write a procedure RC that models this circuit. RC should take as inputs the values of R, C, and dt and should return a procedure that takes as inputs a stream representing the current i and an initial value for the capacitor voltage v_0 and produces as output the stream of voltages v. For example, you should be able to use RC to model an RC circuit with R = 5 ohms, C = 1 farad, and a 0.5-second time step by evaluating (define RC1 (RC 5 1 0.5)). This defines RC1 as a procedure that takes a stream representing the time sequence of currents and an initial capacitor voltage and produces the output stream of voltages.
    @@ -1266,11 +1268,11 @@

    Streams as signals

    -Exercise 3.74: Alyssa P. Hacker is designing a system to process signals coming from physical sensors. One important feature she wishes to produce is a signal that describes the zero crossings +Exercise 3.74: Alyssa P. Hacker is designing a system to process signals coming from physical sensors. One important feature she wishes to produce is a signal that describes the zero crossings of the input signal. That is, the resulting signal should be + 1 whenever the input signal changes from negative to positive, - 1 whenever the input signal changes from positive to negative, and 0 otherwise. (Assume that the sign of a 0 input is positive.) For example, a typical input signal with its associated zero-crossing signal would be -
    +
    ... 1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4 ... ... 0 0 0 0 0 -1 0 0 0 0 1 0 0 ...
    @@ -1278,9 +1280,9 @@

    Streams as signals

    C.prompt(); -In Alyssa's system, the signal from the sensor is represented as a stream sense-data and the stream zero-crossings is the corresponding stream of zero crossings. Alyssa first writes a procedure sign-change-detector that takes two values as arguments and compares the signs of the values to produce an appropriate 0, 1, or - 1. She then constructs her zero-crossing stream as follows: +In Alyssa's system, the signal from the sensor is represented as a stream sense-data and the stream zero-crossings is the corresponding stream of zero crossings. Alyssa first writes a procedure sign-change-detector that takes two values as arguments and compares the signs of the values to produce an appropriate 0, 1, or - 1. She then constructs her zero-crossing stream as follows: -
    +
    (define (make-zero-crossings input-stream last-value) (cons-stream (sign-change-detector (stream-car input-stream) last-value) @@ -1293,23 +1295,23 @@

    Streams as signals

    C.prompt(); -Alyssa's boss, Eva Lu Ator, walks by and suggests that this program is approximately equivalent to the following one, which uses the generalized version of stream-map from Exercise 3-50: +Alyssa's boss, Eva Lu Ator, walks by and suggests that this program is approximately equivalent to the following one, which uses the generalized version of stream-map from Exercise 3-50: -
    +
    (define zero-crossings - (stream-map sign-change-detector sense-data )) + (stream-map sign-change-detector sense-data <expression>))
    -Complete the program by supplying the indicated . +Complete the program by supplying the indicated <expression>.
    Exercise 3.75: Unfortunately, Alyssa's zero-crossing detector in Exercise 3-74 proves to be insufficient, because the noisy signal from the sensor leads to spurious zero crossings. Lem E. Tweakit, a hardware specialist, suggests that Alyssa smooth the signal to filter out the noise before extracting the zero crossings. Alyssa takes his advice and decides to extract the zero crossings from the signal constructed by averaging each value of the sense data with the previous value. She explains the problem to her assistant, Louis Reasoner, who attempts to implement the idea, altering Alyssa's program as follows: -
    +
    (define (make-zero-crossings input-stream last-value) (let ((avpt (/ (+ (stream-car input-stream) last-value) 2))) (cons-stream (sign-change-detector avpt last-value) @@ -1320,19 +1322,18 @@

    Streams as signals

    C.prompt(); -This does not correctly implement Alyssa's plan. Find the bug that Louis has installed and fix it without changing the structure of the program. (Hint: You will need to increase the number of arguments to make-zero-crossings.) +This does not correctly implement Alyssa's plan. Find the bug that Louis has installed and fix it without changing the structure of the program. (Hint: You will need to increase the number of arguments to make-zero-crossings.)
    -Exercise 3.76: Eva Lu Ator has a criticism of Louis's approach in Exercise 3-75. The program he wrote is not modular, because it intermixes the operation of smoothing with the zero-crossing extraction. For example, the extractor should not have to be changed if Alyssa finds a better way to condition her input signal. Help Louis by writing a procedure smooth that takes a stream as input and produces a stream in which each element is the average of two successive input stream elements. Then use smooth as a component to implement the zero-crossing detector in a more modular style. +Exercise 3.76: Eva Lu Ator has a criticism of Louis's approach in Exercise 3-75. The program he wrote is not modular, because it intermixes the operation of smoothing with the zero-crossing extraction. For example, the extractor should not have to be changed if Alyssa finds a better way to condition her input signal. Help Louis by writing a procedure smooth that takes a stream as input and produces a stream in which each element is the average of two successive input stream elements. Then use smooth as a component to implement the zero-crossing detector in a more modular style.
    - -

    3.5.4 Streams and Delayed Evaluation

    +

    3.5.4 Streams and Delayed Evaluation

    -The integral procedure at the end of the preceding section shows how we can use streams to model signal-processing systems that contain feedback loops. The feedback loop for the adder shown in Figure 3-32 is modeled by the fact that integral's internal stream int is defined in terms of itself: +The integral procedure at the end of the preceding section shows how we can use streams to model signal-processing systems that contain feedback loops. The feedback loop for the adder shown in Figure 3-32 is modeled by the fact that integral's internal stream int is defined in terms of itself: -
    +
    (define int (cons-stream initial-value (add-streams (scale-stream integrand dt) @@ -1342,9 +1343,9 @@

    3.5.4 Streams and Delayed Evaluation

    C.prompt(); -The interpreter's ability to deal with such an implicit definition depends on the delay that is incorporated into cons-stream. Without this delay, the interpreter could not construct int before evaluating both arguments to cons-stream, which would require that int already be defined. In general, delay is crucial for using streams to model signal-processing systems that contain loops. Without delay, our models would have to be formulated so that the inputs to any signal-processing component would be fully evaluated before the output could be produced. This would outlaw loops. +The interpreter's ability to deal with such an implicit definition depends on the delay that is incorporated into cons-stream. Without this delay, the interpreter could not construct int before evaluating both arguments to cons-stream, which would require that int already be defined. In general, delay is crucial for using streams to model signal-processing systems that contain loops. Without delay, our models would have to be formulated so that the inputs to any signal-processing component would be fully evaluated before the output could be produced. This would outlaw loops. -Unfortunately, stream models of systems with loops may require uses of delay beyond the ``hidden'' delay supplied by cons-stream. For instance, Figure 3-34 shows a signal-processing system for solving the differential equation dy/dt = f(y) where f is a given function. The figure shows a mapping component, which applies f to its input signal, linked in a feedback loop to an integrator in a manner very similar to that of the analog computer circuits that are actually used to solve such equations. +Unfortunately, stream models of systems with loops may require uses of delay beyond the ``hidden'' delay supplied by cons-stream. For instance, Figure 3-34 shows a signal-processing system for solving the differential equation dy/dt = f(y) where f is a given function. The figure shows a mapping component, which applies f to its input signal, linked in a feedback loop to an integrator in a manner very similar to that of the analog computer circuits that are actually used to solve such equations.
    Figure 3.34: An ``analog computer circuit'' that @@ -1364,7 +1365,7 @@

    3.5.4 Streams and Delayed Evaluation

    Assuming we are given an initial value y_0 for y, we could try to model this system using the procedure -
    +
    (define (solve f y0 dt) (define y (integral dy y0 dt)) (define dy (stream-map f y)) @@ -1374,13 +1375,13 @@

    3.5.4 Streams and Delayed Evaluation

    C.prompt(); -This procedure does not work, because in the first line of solve the call to integral requires that the input dy be defined, which does not happen until the second line of solve. +This procedure does not work, because in the first line of solve the call to integral requires that the input dy be defined, which does not happen until the second line of solve. -On the other hand, the intent of our definition does make sense, because we can, in principle, begin to generate the y stream without knowing dy. Indeed, integral and many other stream operations have properties similar to those of cons-stream, in that we can generate part of the answer given only partial information about the arguments. For integral, the first element of the output stream is the specified initial-value. Thus, we can generate the first element of the output stream without evaluating the integrand dy. Once we know the first element of y, the stream-map in the second line of solve can begin working to generate the first element of dy, which will produce the next element of y, and so on. +On the other hand, the intent of our definition does make sense, because we can, in principle, begin to generate the y stream without knowing dy. Indeed, integral and many other stream operations have properties similar to those of cons-stream, in that we can generate part of the answer given only partial information about the arguments. For integral, the first element of the output stream is the specified initial-value. Thus, we can generate the first element of the output stream without evaluating the integrand dy. Once we know the first element of y, the stream-map in the second line of solve can begin working to generate the first element of dy, which will produce the next element of y, and so on. -To take advantage of this idea, we will redefine integral to expect the integrand stream to be a delayed argument . Integral will force the integrand to be evaluated only when it is required to generate more than the first element of the output stream: +To take advantage of this idea, we will redefine integral to expect the integrand stream to be a delayed argument . Integral will force the integrand to be evaluated only when it is required to generate more than the first element of the output stream: -
    +
    (define (integral delayed-integrand initial-value dt) (define int (cons-stream initial-value @@ -1393,9 +1394,9 @@

    3.5.4 Streams and Delayed Evaluation

    C.prompt(); -Now we can implement our solve procedure by delaying the evaluation of dy in the definition of y:@footnote{This procedure is not guaranteed to work in all Scheme implementations, although for any implementation there is a simple variation that will work. The problem has to do with subtle differences in the ways that Scheme implementations handle internal definitions. (See section
    4.1.6.)} +Now we can implement our solve procedure by delaying the evaluation of dy in the definition of y:@footnote{This procedure is not guaranteed to work in all Scheme implementations, although for any implementation there is a simple variation that will work. The problem has to do with subtle differences in the ways that Scheme implementations handle internal definitions. (See section 4.1.6.)} -
    +
    (define (solve f y0 dt) (define y (integral (delay dy) y0 dt)) (define dy (stream-map f y)) @@ -1405,9 +1406,9 @@

    3.5.4 Streams and Delayed Evaluation

    C.prompt(); -In general, every caller of integral must now delay the integrand argument. We can demonstrate that the solve procedure works by approximating eapprox 2.718 by computing the value at y = 1 of the solution to the differential equation dy/dt = y with initial condition y(0) = 1: +In general, every caller of integral must now delay the integrand argument. We can demonstrate that the solve procedure works by approximating eapprox 2.718 by computing the value at y = 1 of the solution to the differential equation dy/dt = y with initial condition y(0) = 1: -
    +
    (stream-ref (solve (lambda (y) y) 1 0.001) 1000) 2.716924
    @@ -1416,9 +1417,9 @@

    3.5.4 Streams and Delayed Evaluation

    -Exercise 3.77: The integral procedure used above was analogous to the ``implicit'' definition of the infinite stream of integers in section 3.5.2. Alternatively, we can give a definition of integral that is more like integers-starting-from (also in section 3.5.2): +Exercise 3.77: The integral procedure used above was analogous to the ``implicit'' definition of the infinite stream of integers in section 3.5.2. Alternatively, we can give a definition of integral that is more like integers-starting-from (also in section 3.5.2): -
    +
    (define (integral integrand initial-value dt) (cons-stream initial-value (if (stream-null? integrand) @@ -1432,7 +1433,7 @@

    3.5.4 Streams and Delayed Evaluation

    C.prompt(); -When used in systems with loops, this procedure has the same problem as does our original version of integral. Modify the procedure so that it expects the integrand as a delayed argument and hence can be used in the solve procedure shown above. +When used in systems with loops, this procedure has the same problem as does our original version of integral. Modify the procedure so that it expects the integrand as a delayed argument and hence can be used in the solve procedure shown above.
    @@ -1448,11 +1449,11 @@

    3.5.4 Streams and Delayed Evaluation

    | +----------+ | +----------+ | | | | | +----------+ | | -| __/|<--+ scale: a |<--+ | +| __/|<--+ scale: a |<--+ | | _/ | +----------+ | -+--<_add | | ++--<_add | | \__ | +----------+ | - \|<--+ scale: b |<-------------------+ + \|<--+ scale: b |<-------------------+ +----------+
    @@ -1466,15 +1467,15 @@

    3.5.4 Streams and Delayed Evaluation

    d t^2 d t -The output stream, modeling y, is generated by a network that contains a loop. This is because the value of d^2y/dt^2 depends upon the values of y and dy/dt and both of these are determined by integrating d^2y/dt^2. The diagram we would like to encode is shown in Figure 3-35. Write a procedure solve-2nd that takes as arguments the constants a, b, and dt and the initial values y_0 and dy_0 for y and dy/dt and generates the stream of successive values of y. +The output stream, modeling y, is generated by a network that contains a loop. This is because the value of d^2y/dt^2 depends upon the values of y and dy/dt and both of these are determined by integrating d^2y/dt^2. The diagram we would like to encode is shown in Figure 3-35. Write a procedure solve-2nd that takes as arguments the constants a, b, and dt and the initial values y_0 and dy_0 for y and dy/dt and generates the stream of successive values of y.
    -Exercise 3.79: Generalize the solve-2nd procedure of Exercise 3-78 so that it can be used to solve general second-order differential equations d^2 y/dt^2 = f(dy/dt, y). +Exercise 3.79: Generalize the solve-2nd procedure of Exercise 3-78 so that it can be used to solve general second-order differential equations d^2 y/dt^2 = f(dy/dt, y).
    -Exercise 3.80: A series RLC circuit +Exercise 3.80: A series RLC circuit consists of a resistor, a capacitor, and an inductor connected in series, as shown in Figure 3-36. If R, L, and C are the resistance, inductance, and capacitance, then the relations between voltage (v) and current (i) for the three components are described by the equations @@ -1537,7 +1538,7 @@

    3.5.4 Streams and Delayed Evaluation

                      +-------------+
    -+----------------+  scale: l/L |<--+
    ++----------------+  scale: l/L |<--+
     |                +-------------+   |
     |                                  |
     |                +-------------+   |  v_C
    @@ -1547,7 +1548,7 @@ 

    3.5.4 Streams and Delayed Evaluation

    | | | v_(C_0) | | | | +-------------+ -| +---+ scale: -l/C |<--+ +| +---+ scale: -l/C |<--+ | +-------------+ | | |\__ | +->| \_ di_L +-------------+ | i_L @@ -1557,27 +1558,26 @@

    3.5.4 Streams and Delayed Evaluation

    | | i_(L_0) | | | | +-------------+ | -+----------------+ scale: -R/L |<--+ ++----------------+ scale: -R/L |<--+ +-------------+
    -Write a procedure RLC that takes as arguments the parameters R, L, and C of the circuit and the time increment dt. In a manner similar to that of the RC procedure of Exercise 3-73, RLC should produce a procedure that takes the initial values of the state variables, v_(C_0) and i_(L_0), and produces a pair (using cons) of the streams of states v_C and i_L. Using RLC, generate the pair of streams that models the behavior of a series RLC circuit with R = 1 ohm, C = 0.2 farad, L = 1 henry, dt = 0.1 second, and initial values i_(L_0) = 0 amps and v_(C_0) = 10 volts. +Write a procedure RLC that takes as arguments the parameters R, L, and C of the circuit and the time increment dt. In a manner similar to that of the RC procedure of Exercise 3-73, RLC should produce a procedure that takes the initial values of the state variables, v_(C_0) and i_(L_0), and produces a pair (using cons) of the streams of states v_C and i_L. Using RLC, generate the pair of streams that models the behavior of a series RLC circuit with R = 1 ohm, C = 0.2 farad, L = 1 henry, dt = 0.1 second, and initial values i_(L_0) = 0 amps and v_(C_0) = 10 volts.

    Normal-order evaluation

    -The examples in this section illustrate how the explicit use of delay and force provides great programming flexibility, but the same examples also show how this can make our programs more complex. Our new integral procedure, for instance, gives us the power to model systems with loops, but we must now remember that integral should be called with a delayed integrand, and every procedure that uses integral must be aware of this. In effect, we have created two classes of procedures: ordinary procedures and procedures that take delayed arguments. In general, creating separate classes of procedures forces us to create separate classes of higher-order procedures as well.@footnote{This is a small reflection, in Lisp, of the difficulties that conventional strongly typed languages such as Pascal have in coping with higher-order procedures. In such languages, the programmer must specify the data types of the arguments and the result of each procedure: number, logical value, sequence, and so on. Consequently, we could not express an abstraction such as ``map a given procedure proc over all the elements in a sequence'' by a single higher-order procedure such as stream-map. Rather, we would need a different mapping procedure for each different combination of argument and result data types that might be specified for a proc. Maintaining a practical notion of ``data type'' in the presence of higher-order procedures raises many difficult issues. One way of dealing with this problem is illustrated by the language ML (Gordon, Milner, and Wadsworth 1979), whose ``polymorphic data types'' include templates for higher-order transformations between data types. Moreover, data types for most procedures in ML are never explicitly declared by the programmer. Instead, ML includes a type-inferencing mechanism that uses information in the environment to deduce the data types for newly defined procedures.} +The examples in this section illustrate how the explicit use of delay and force provides great programming flexibility, but the same examples also show how this can make our programs more complex. Our new integral procedure, for instance, gives us the power to model systems with loops, but we must now remember that integral should be called with a delayed integrand, and every procedure that uses integral must be aware of this. In effect, we have created two classes of procedures: ordinary procedures and procedures that take delayed arguments. In general, creating separate classes of procedures forces us to create separate classes of higher-order procedures as well.@footnote{This is a small reflection, in Lisp, of the difficulties that conventional strongly typed languages such as Pascal have in coping with higher-order procedures. In such languages, the programmer must specify the data types of the arguments and the result of each procedure: number, logical value, sequence, and so on. Consequently, we could not express an abstraction such as ``map a given procedure proc over all the elements in a sequence'' by a single higher-order procedure such as stream-map. Rather, we would need a different mapping procedure for each different combination of argument and result data types that might be specified for a proc. Maintaining a practical notion of ``data type'' in the presence of higher-order procedures raises many difficult issues. One way of dealing with this problem is illustrated by the language ML (Gordon, Milner, and Wadsworth 1979), whose ``polymorphic data types'' include templates for higher-order transformations between data types. Moreover, data types for most procedures in ML are never explicitly declared by the programmer. Instead, ML includes a type-inferencing mechanism that uses information in the environment to deduce the data types for newly defined procedures.} -One way to avoid the need for two different classes of procedures is to make all procedures take delayed arguments. We could adopt a model of evaluation in which all arguments to procedures are automatically delayed and arguments are forced only when they are actually needed (for example, when they are required by a primitive operation). This would transform our language to use normal-order evaluation, which we first described when we introduced the substitution model for evaluation in section 1.1.5. Converting to normal-order evaluation provides a uniform and elegant way to simplify the use of delayed evaluation, and this would be a natural strategy to adopt if we were concerned only with stream processing. In section 4.2, after we have studied the evaluator, we will see how to transform our language in just this way. Unfortunately, including delays in procedure calls wreaks havoc with our ability to design programs that depend on the order of events, such as programs that use assignment, mutate data, or perform input or output. Even the single delay in cons-stream can cause great confusion, as illustrated by Exercise 3-51 and Exercise 3-52. As far as anyone knows, mutability and delayed evaluation do not mix well in programming languages, and devising ways to deal with both of these at once is an active area of research. +One way to avoid the need for two different classes of procedures is to make all procedures take delayed arguments. We could adopt a model of evaluation in which all arguments to procedures are automatically delayed and arguments are forced only when they are actually needed (for example, when they are required by a primitive operation). This would transform our language to use normal-order evaluation, which we first described when we introduced the substitution model for evaluation in section 1.1.5. Converting to normal-order evaluation provides a uniform and elegant way to simplify the use of delayed evaluation, and this would be a natural strategy to adopt if we were concerned only with stream processing. In section 4.2, after we have studied the evaluator, we will see how to transform our language in just this way. Unfortunately, including delays in procedure calls wreaks havoc with our ability to design programs that depend on the order of events, such as programs that use assignment, mutate data, or perform input or output. Even the single delay in cons-stream can cause great confusion, as illustrated by Exercise 3-51 and Exercise 3-52. As far as anyone knows, mutability and delayed evaluation do not mix well in programming languages, and devising ways to deal with both of these at once is an active area of research. - -

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    +

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    As we saw in section
    3.1.2, one of the major benefits of introducing assignment is that we can increase the modularity of our systems by encapsulating, or ``hiding,'' parts of the state of a large system within local variables. Stream models can provide an equivalent modularity without the use of assignment. As an illustration, we can reimplement the Monte Carlo estimation of [pi], which we examined in section 3.1.2, from a stream-processing point of view. -The key modularity issue was that we wished to hide the internal state of a random-number generator from programs that used random numbers. We began with a procedure rand-update, whose successive values furnished our supply of random numbers, and used this to produce a random-number generator: +The key modularity issue was that we wished to hide the internal state of a random-number generator from programs that used random numbers. We began with a procedure rand-update, whose successive values furnished our supply of random numbers, and used this to produce a random-number generator: -
    +
    (define rand (let ((x random-init)) (lambda () @@ -1588,9 +1588,9 @@

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    C.prompt(); -In the stream formulation there is no random-number generator per se, just a stream of random numbers produced by successive calls to rand-update: +In the stream formulation there is no random-number generator per se, just a stream of random numbers produced by successive calls to rand-update: -
    +
    (define random-numbers (cons-stream random-init (stream-map rand-update random-numbers))) @@ -1599,9 +1599,9 @@

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    C.prompt(); -We use this to construct the stream of outcomes of the Ces@`aro experiment performed on consecutive pairs in the random-numbers stream: +We use this to construct the stream of outcomes of the Ces@`aro experiment performed on consecutive pairs in the random-numbers stream: -
    +
    (define cesaro-stream (map-successive-pairs (lambda (r1 r2) (= (gcd r1 r2) 1)) random-numbers)) @@ -1615,9 +1615,9 @@

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    C.prompt(); -The cesaro-stream is now fed to a monte-carlo procedure, which produces a stream of estimates of probabilities. The results are then converted into a stream of estimates of [pi]. This version of the program doesn't need a parameter telling how many trials to perform. Better estimates of [pi] (from performing more experiments) are obtained by looking farther into the pi stream: +The cesaro-stream is now fed to a monte-carlo procedure, which produces a stream of estimates of probabilities. The results are then converted into a stream of estimates of [pi]. This version of the program doesn't need a parameter telling how many trials to perform. Better estimates of [pi] (from performing more experiments) are obtained by looking farther into the pi stream: -
    +
    (define (monte-carlo experiment-stream passed failed) (define (next passed failed) (cons-stream @@ -1636,25 +1636,25 @@

    3.5.5 Modularity of Functional Programs and Modularity of Objects

    C.prompt(); -There is considerable modularity in this approach, because we still can formulate a general monte-carlo procedure that can deal with arbitrary experiments. Yet there is no assignment or local state. +There is considerable modularity in this approach, because we still can formulate a general monte-carlo procedure that can deal with arbitrary experiments. Yet there is no assignment or local state.
    -Exercise 3.81: Exercise 3-6 discussed generalizing the random-number generator to allow one to reset the random-number sequence so as to produce repeatable sequences of ``random''numbers. Produce a stream formulation of this same generator that operates on an input stream of requests to generate a new random number or to reset the sequence to a specified value and that produces the desired stream of random numbers. Don't use assignment in your solution. +Exercise 3.81: Exercise 3-6 discussed generalizing the random-number generator to allow one to reset the random-number sequence so as to produce repeatable sequences of ``random''numbers. Produce a stream formulation of this same generator that operates on an input stream of requests to generate a new random number or to reset the sequence to a specified value and that produces the desired stream of random numbers. Don't use assignment in your solution.
    -Exercise 3.82: Redo Exercise 3-5 on Monte Carlo integration in terms of streams. The stream version of estimate-integral will not have an argument telling how many trials to perform. Instead, it will produce a stream of estimates based on successively more trials. +Exercise 3.82: Redo Exercise 3-5 on Monte Carlo integration in terms of streams. The stream version of estimate-integral will not have an argument telling how many trials to perform. Instead, it will produce a stream of estimates based on successively more trials.

    A functional-programming view of time

    Let us now return to the issues of objects and state that were raised at the beginning of this chapter and examine them in a new light. We introduced assignment and mutable objects to provide a mechanism for modular construction of programs that model systems with state. We constructed computational objects with local state variables and used assignment to modify these variables. We modeled the temporal behavior of the objects in the world by the temporal behavior of the corresponding computational objects. -Now we have seen that streams provide an alternative way to model objects with local state. We can model a changing quantity, such as the local state of some object, using a stream that represents the time history of successive states. In essence, we represent time explicitly, using streams, so that we decouple time in our simulated world from the sequence of events that take place during evaluation. Indeed, because of the presence of delay there may be little relation between simulated time in the model and the order of events during the evaluation. +Now we have seen that streams provide an alternative way to model objects with local state. We can model a changing quantity, such as the local state of some object, using a stream that represents the time history of successive states. In essence, we represent time explicitly, using streams, so that we decouple time in our simulated world from the sequence of events that take place during evaluation. Indeed, because of the presence of delay there may be little relation between simulated time in the model and the order of events during the evaluation. In order to contrast these two approaches to modeling, let us reconsider the implementation of a ``withdrawal processor'' that monitors the balance in a bank account. In section 3.1.3 we implemented a simplified version of such a processor: -
    +
    (define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) @@ -1664,11 +1664,11 @@

    A functional-programming view of time

    C.prompt(); -Calls to make-simplified-withdraw produce computational objects, each with a local state variable balance that is decremented by successive calls to the object. The object takes an amount as an argument and returns the new balance. We can imagine the user of a bank account typing a sequence of inputs to such an object and observing the sequence of returned values shown on a display screen. +Calls to make-simplified-withdraw produce computational objects, each with a local state variable balance that is decremented by successive calls to the object. The object takes an amount as an argument and returns the new balance. We can imagine the user of a bank account typing a sequence of inputs to such an object and observing the sequence of returned values shown on a display screen. Alternatively, we can model a withdrawal processor as a procedure that takes as input a balance and a stream of amounts to withdraw and produces the stream of successive balances in the account: -
    +
    (define (stream-withdraw balance amount-stream) (cons-stream balance @@ -1679,13 +1679,13 @@

    A functional-programming view of time

    C.prompt(); -Stream-withdraw implements a well-defined mathematical function whose output is fully determined by its input. Suppose, however, that the input amount-stream is the stream of successive values typed by the user and that the resulting stream of balances is displayed. Then, from the perspective of the user who is typing values and watching results, the stream process has the same behavior as the object created by make-simplified-withdraw. However, with the stream version, there is no assignment, no local state variable, and consequently none of the theoretical difficulties that we encountered in section 3.1.3. Yet the system has state! +Stream-withdraw implements a well-defined mathematical function whose output is fully determined by its input. Suppose, however, that the input amount-stream is the stream of successive values typed by the user and that the resulting stream of balances is displayed. Then, from the perspective of the user who is typing values and watching results, the stream process has the same behavior as the object created by make-simplified-withdraw. However, with the stream version, there is no assignment, no local state variable, and consequently none of the theoretical difficulties that we encountered in section 3.1.3. Yet the system has state! -This is really remarkable. Even though stream-withdraw implements a well-defined mathematical function whose behavior does not change, the user's perception here is one of interacting with a system that has a changing state. One way to resolve this paradox is to realize that it is the user's temporal existence that imposes state on the system. If the user could step back from the interaction and think in terms of streams of balances rather than individual transactions, the system would appear stateless.@footnote{Similarly in physics, when we observe a moving particle, we say that the position (state) of the particle is changing. However, from the perspective of the particle's world line in space-time there is no change involved.} +This is really remarkable. Even though stream-withdraw implements a well-defined mathematical function whose behavior does not change, the user's perception here is one of interacting with a system that has a changing state. One way to resolve this paradox is to realize that it is the user's temporal existence that imposes state on the system. If the user could step back from the interaction and think in terms of streams of balances rather than individual transactions, the system would appear stateless.@footnote{Similarly in physics, when we observe a moving particle, we say that the position (state) of the particle is changing. However, from the perspective of the particle's world line in space-time there is no change involved.} From the point of view of one part of a complex process, the other parts appear to change with time. They have hidden time-varying local state. If we wish to write programs that model this kind of natural decomposition in our world (as we see it from our viewpoint as a part of that world) with structures in our computer, we make computational objects that are not functional---they must change with time. We model state with local state variables, and we model the changes of state with assignments to those variables. By doing this we make the time of execution of a computation model time in the world that we are part of, and thus we get ``objects'' in our computer. -Modeling with objects is powerful and intuitive, largely because this matches the perception of interacting with a world of which we are part. However, as we've seen repeatedly throughout this chapter, these models raise thorny problems of constraining the order of events and of synchronizing multiple processes. The possibility of avoiding these problems has stimulated the development of functional programming languages , which do not include any provision for assignment or mutable data. In such a language, all procedures implement well-defined mathematical functions of their arguments, whose behavior does not change. The functional approach is extremely attractive for dealing with concurrent systems.@footnote{John Backus, the inventor of Fortran, gave high visibility to functional programming when he was awarded the @acronym{ACM} Turing award in 1978. His acceptance speech (Backus 1978) strongly advocated the functional approach. A good overview of functional programming is given in Henderson 1980 and in Darlington, Henderson, and Turner 1982.} +Modeling with objects is powerful and intuitive, largely because this matches the perception of interacting with a world of which we are part. However, as we've seen repeatedly throughout this chapter, these models raise thorny problems of constraining the order of events and of synchronizing multiple processes. The possibility of avoiding these problems has stimulated the development of functional programming languages , which do not include any provision for assignment or mutable data. In such a language, all procedures implement well-defined mathematical functions of their arguments, whose behavior does not change. The functional approach is extremely attractive for dealing with concurrent systems.@footnote{John Backus, the inventor of Fortran, gave high visibility to functional programming when he was awarded the @acronym{ACM} Turing award in 1978. His acceptance speech (Backus 1978) strongly advocated the functional approach. A good overview of functional programming is given in Henderson 1980 and in Darlington, Henderson, and Turner 1982.} On the other hand, if we look closely, we can see time-related problems creeping into functional models as well. One particularly troublesome area arises when we wish to design interactive systems, especially ones that model interactions between independent entities. For instance, consider once more the implementation a banking system that permits joint bank accounts. In a conventional system using assignment and objects, we would model the fact that Peter and Paul share an account by having both Peter and Paul send their transaction requests to the same bank-account object, as we saw in section 3.1.3. From the stream point of view, where there are no ``objects''per se, we have already indicated that a bank account can be modeled as a process that operates on a stream of transaction requests to produce a stream of responses. Accordingly, we could model the fact that Peter and Paul have a joint bank account by merging Peter's stream of transaction requests with Paul's stream of requests and feeding the result to the bank-account stream process, as shown in Figure 3-38. @@ -1702,7 +1702,7 @@

    A functional-programming view of time

    -The trouble with this formulation is in the notion of merge . It will not do to merge the two streams by simply taking alternately one request from Peter and one request from Paul. Suppose Paul accesses the account only very rarely. We could hardly force Peter to wait for Paul to access the account before he could issue a second transaction. However such a merge is implemented, it must interleave the two transaction streams in some way that is constrained by ``real time'' as perceived by Peter and Paul, in the sense that, if Peter and Paul meet, they can agree that certain transactions were processed before the meeting, and other transactions were processed after the meeting.@footnote{Observe that, for any two streams, there is in general more than one acceptable order of interleaving. Thus, technically, ``merge'' is a relation rather than a function---the answer is not a deterministic function of the inputs. We already mentioned (Footnote 39) that nondeterminism is essential when dealing with concurrency. The merge relation illustrates the same essential nondeterminism, from the functional perspective. In section 4.3, we will look at nondeterminism from yet another point of view.} This is precisely the same constraint that we had to deal with in section 3.4.1, where we found the need to introduce explicit synchronization to ensure a ``correct'' order of events in concurrent processing of objects with state. Thus, in an attempt to support the functional style, the need to merge inputs from different agents reintroduces the same problems that the functional style was meant to eliminate. +The trouble with this formulation is in the notion of merge . It will not do to merge the two streams by simply taking alternately one request from Peter and one request from Paul. Suppose Paul accesses the account only very rarely. We could hardly force Peter to wait for Paul to access the account before he could issue a second transaction. However such a merge is implemented, it must interleave the two transaction streams in some way that is constrained by ``real time'' as perceived by Peter and Paul, in the sense that, if Peter and Paul meet, they can agree that certain transactions were processed before the meeting, and other transactions were processed after the meeting.@footnote{Observe that, for any two streams, there is in general more than one acceptable order of interleaving. Thus, technically, ``merge'' is a relation rather than a function---the answer is not a deterministic function of the inputs. We already mentioned (Footnote 39) that nondeterminism is essential when dealing with concurrency. The merge relation illustrates the same essential nondeterminism, from the functional perspective. In section 4.3, we will look at nondeterminism from yet another point of view.} This is precisely the same constraint that we had to deal with in section 3.4.1, where we found the need to introduce explicit synchronization to ensure a ``correct'' order of events in concurrent processing of objects with state. Thus, in an attempt to support the functional style, the need to merge inputs from different agents reintroduces the same problems that the functional style was meant to eliminate. We began this chapter with the goal of building computational models whose structure matches our perception of the real world we are trying to model. We can model the world as a collection of separate, time-bound, interacting objects with state, or we can model the world as a single, timeless, stateless unity. Each view has powerful advantages, but neither view alone is completely satisfactory. A grand unification has yet to emerge.@footnote{The object model approximates the world by dividing it into separate pieces. The functional model does not modularize along object boundaries. The object model is useful when the unshared state of the ``objects'' is much larger than the state that they share. An example of a place where the object viewpoint fails is quantum mechanics, where thinking of things as individual particles leads to paradoxes and confusions. Unifying the object view with the functional view may have little to do with programming, but rather with fundamental epistemological issues.} @@ diff --git a/content/4-0-metalinguistic-abstraction.content.html b/content/4-0-metalinguistic-abstraction.content.html index 77dcd5c..2650ca1 100644 --- a/content/4-0-metalinguistic-abstraction.content.html +++ b/content/4-0-metalinguistic-abstraction.content.html @@ -3,10 +3,10 @@ @@ {{main_text}} - + - - + +

    Metalinguistic Abstraction

    @@ -32,7 +32,7 @@

    Metalinguistic Abstraction

    To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others. -

    In fact, we can regard almost any program as the evaluator for some language. For instance, the polynomial manipulation system of section 2.5.3 embodies the rules of polynomial arithmetic and implements them in terms of operations on list-structured data. If we augment this system with procedures to read and print polynomial expressions, we have the core of a special-purpose language for dealing with problems in symbolic mathematics. The digital-logic simulator of section 3.3.4 and the constraint propagator of section 3.3.5 are legitimate languages in their own right, each with its own primitives, means of combination, and means of abstraction. Seen from this perspective, the technology for coping with large-scale computer systems merges with the technology for building new computer languages, and computer science itself becomes no more (and no less) than the discipline of constructing appropriate descriptive languages. +

    In fact, we can regard almost any program as the evaluator for some language. For instance, the polynomial manipulation system of section 2.5.3 embodies the rules of polynomial arithmetic and implements them in terms of operations on list-structured data. If we augment this system with procedures to read and print polynomial expressions, we have the core of a special-purpose language for dealing with problems in symbolic mathematics. The digital-logic simulator of section 3.3.4 and the constraint propagator of section 3.3.5 are legitimate languages in their own right, each with its own primitives, means of combination, and means of abstraction. Seen from this perspective, the technology for coping with large-scale computer systems merges with the technology for building new computer languages, and computer science itself becomes no more (and no less) than the discipline of constructing appropriate descriptive languages.

    We now embark on a tour of the technology by which languages are established in terms of other languages. In this chapter we shall use Lisp as a base, implementing evaluators as Lisp procedures. Lisp is particularly well suited to this task, because of its ability to represent and manipulate symbolic expressions. We will take the first step in understanding how languages are implemented by building an evaluator for Lisp itself. The language implemented by our evaluator will be a subset of the Scheme dialect of Lisp that we use in this book. Although the evaluator described in this chapter is written for a particular dialect of Lisp, it contains the essential structure of an evaluator for any expression-oriented language designed for writing programs for a sequential machine. (In fact, most language processors contain, deep within them, a little ``Lisp'' evaluator.) The evaluator has been simplified for the purposes of illustration and discussion, and some features have been left out that would be important to include in a production-quality Lisp system. Nevertheless, this simple evaluator is adequate to execute most of the programs in this book.2 diff --git a/content/4-1-metacircular.content.html b/content/4-1-metacircular.content.html index 95b0b28..6d8850c 100644 --- a/content/4-1-metacircular.content.html +++ b/content/4-1-metacircular.content.html @@ -3,16 +3,16 @@ @@ {{main_text}} - + - - + +

    The Metacircular Evaluator

    -

    Our evaluator for Lisp will be implemented as a Lisp program. It may seem circular to think about evaluating Lisp programs using an evaluator that is itself implemented in Lisp. However, evaluation is a process, so it is appropriate to describe the evaluation process using Lisp, which, after all, is our tool for describing processes.3 An evaluator that is written in the same language that it evaluates is said to be metacircular . +

    Our evaluator for Lisp will be implemented as a Lisp program. It may seem circular to think about evaluating Lisp programs using an evaluator that is itself implemented in Lisp. However, evaluation is a process, so it is appropriate to describe the evaluation process using Lisp, which, after all, is our tool for describing processes.3 An evaluator that is written in the same language that it evaluates is said to be metacircular . The metacircular evaluator is essentially a Scheme formulation of the environment model of evaluation described in section 3.2. Recall that the model has two basic parts: @@ -27,35 +27,34 @@

    The Metacircular Evaluator

    -

    These two rules describe the essence of the evaluation process, a basic cycle in which expressions to be evaluated in environments are reduced to procedures to be applied to arguments, which in turn are reduced to new expressions to be evaluated in new environments, and so on, until we get down to symbols, whose values are looked up in the environment, and to primitive procedures, which are applied directly (see Figure 4-1).4 This evaluation cycle will be embodied by the interplay between the two critical procedures in the evaluator, eval and apply, which are +

    These two rules describe the essence of the evaluation process, a basic cycle in which expressions to be evaluated in environments are reduced to procedures to be applied to arguments, which in turn are reduced to new expressions to be evaluated in new environments, and so on, until we get down to symbols, whose values are looked up in the environment, and to primitive procedures, which are applied directly (see Figure 4-1).4 This evaluation cycle will be embodied by the interplay between the two critical procedures in the evaluator, eval and apply, which are described in section 4.1.1 (see Figure 4-1). -

    The implementation of the evaluator will depend upon procedures that define the syntax of the expressions to be evaluated. We will use data abstraction to make the evaluator independent of the representation of the language. For example, rather than committing to a choice that an assignment is to be represented by a list beginning with the symbol set! we use an abstract predicate assignment? to test for an assignment, and we use abstract selectors assignment-variable and assignment-value to access the parts of an assignment. Implementation of expressions will be described in detail in section 4.1.2. There are also operations, described in section 4.1.3, that specify the representation of procedures and environments. For example, make-procedure constructs compound procedures, lookup-variable-value accesses the values of variables, and apply-primitive-procedure applies a primitive procedure to a given list of arguments. +

    The implementation of the evaluator will depend upon procedures that define the syntax of the expressions to be evaluated. We will use data abstraction to make the evaluator independent of the representation of the language. For example, rather than committing to a choice that an assignment is to be represented by a list beginning with the symbol set! we use an abstract predicate assignment? to test for an assignment, and we use abstract selectors assignment-variable and assignment-value to access the parts of an assignment. Implementation of expressions will be described in detail in section 4.1.2. There are also operations, described in section 4.1.3, that specify the representation of procedures and environments. For example, make-procedure constructs compound procedures, lookup-variable-value accesses the values of variables, and apply-primitive-procedure applies a primitive procedure to a given list of arguments. - -

    4.1.1 The Core of the Evaluator

    +

    4.1.1 The Core of the Evaluator

    -

    Figure 4.1: The eval-apply cycle +

    Figure 4.1: The eval-apply cycle exposes the essence of a computer language. -

    The evaluation process can be described as the interplay between two procedures: eval and apply. +

    The evaluation process can be described as the interplay between two procedures: eval and apply.

    Eval

    -

    Eval takes as arguments an expression and an environment. It classifies the expression and directs its evaluation. Eval is structured as a case analysis of the syntactic type of the expression to be evaluated. In order to keep the procedure general, we express the determination of the type of an expression abstractly, making no commitment to any particular representation for the various types of expressions. Each type of expression has a predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures. +

    Eval takes as arguments an expression and an environment. It classifies the expression and directs its evaluation. Eval is structured as a case analysis of the syntactic type of the expression to be evaluated. In order to keep the procedure general, we express the determination of the type of an expression abstractly, making no commitment to any particular representation for the various types of expressions. Each type of expression has a predicate that tests for it and an abstract means for selecting its parts. This abstract syntax makes it easy to see how we can change the syntax of the language by using the same evaluator, but with a different collection of syntax procedures.

    Primitive expressions
    • -

      For self-evaluating expressions, such as numbers, eval returns the expression itself. +

      For self-evaluating expressions, such as numbers, eval returns the expression itself.

    • -

      Eval must look up variables in the environment to find their values. +

      Eval must look up variables in the environment to find their values.

    @@ -64,27 +63,27 @@
    Special forms
    • -For quoted expressions, eval returns the expression that was quoted. +For quoted expressions, eval returns the expression that was quoted.
    • -An assignment to (or a definition of) a variable must recursively call eval to compute the new value to be associated with the variable. The environment must be modified to change (or create) the binding of the variable. +An assignment to (or a definition of) a variable must recursively call eval to compute the new value to be associated with the variable. The environment must be modified to change (or create) the binding of the variable.
    • -

      An if expression requires special processing of its parts, so as to evaluate the consequent if the predicate is true, and otherwise to evaluate the alternative. +

      An if expression requires special processing of its parts, so as to evaluate the consequent if the predicate is true, and otherwise to evaluate the alternative.

    • -

      A lambda expression must be transformed into an applicable procedure by packaging together the parameters and body specified by the lambda expression with the environment of the evaluation. +

      A lambda expression must be transformed into an applicable procedure by packaging together the parameters and body specified by the lambda expression with the environment of the evaluation.

    • -

      A begin expression requires evaluating its sequence of expressions in the order in which they appear. +

      A begin expression requires evaluating its sequence of expressions in the order in which they appear.

    • -

      A case analysis (cond) is transformed into a nest of if expressions and then evaluated. +

      A case analysis (cond) is transformed into a nest of if expressions and then evaluated.

    @@ -93,11 +92,11 @@
    Combinations
    • -

      For a procedure application, eval must recursively evaluate the operator part and the operands of the combination. The resulting procedure and arguments are passed to apply, which handles the actual procedure application. +

      For a procedure application, eval must recursively evaluate the operator part and the operands of the combination. The resulting procedure and arguments are passed to apply, which handles the actual procedure application.

    -

    Here is the definition of eval: +

    Here is the definition of eval:

    (define (eval exp env) @@ -124,11 +123,11 @@
    Combinations
    C.prompt("scheme-eval", ["scheme-apply"]); -

    For clarity, eval has been implemented as a case analysis using cond. The disadvantage of this is that our procedure handles only a few distinguishable types of expressions, and no new ones can be defined without editing the definition of eval. In most Lisp implementations, dispatching on the type of an expression is done in a data-directed style. This allows a user to add new types of expressions that eval can distinguish, without modifying the definition of eval itself. (See Exercise 4-3.) +

    For clarity, eval has been implemented as a case analysis using cond. The disadvantage of this is that our procedure handles only a few distinguishable types of expressions, and no new ones can be defined without editing the definition of eval. In most Lisp implementations, dispatching on the type of an expression is done in a data-directed style. This allows a user to add new types of expressions that eval can distinguish, without modifying the definition of eval itself. (See Exercise 4-3.)

    Apply

    -

    Apply takes two arguments, a procedure and a list of arguments to which the procedure should be applied. Apply classifies procedures into two kinds: It calls apply-primitive-procedure to apply primitives; it applies compound procedures by sequentially evaluating the expressions that make up the body of the procedure. The environment for the evaluation of the body of a compound procedure is constructed by extending the base environment carried by the procedure to include a frame that binds the parameters of the procedure to the arguments to which the procedure is to be applied. Here is the definition of apply: +

    Apply takes two arguments, a procedure and a list of arguments to which the procedure should be applied. Apply classifies procedures into two kinds: It calls apply-primitive-procedure to apply primitives; it applies compound procedures by sequentially evaluating the expressions that make up the body of the procedure. The environment for the evaluation of the body of a compound procedure is constructed by extending the base environment carried by the procedure to include a frame that binds the parameters of the procedure to the arguments to which the procedure is to be applied. Here is the definition of apply:

    (define (apply procedure arguments) @@ -151,7 +150,7 @@

    Apply

    Procedure arguments

    -

    When eval processes a procedure application, it uses list-of-values to produce the list of arguments to which the procedure is to be applied. List-of-values takes as an argument the operands of the combination. It evaluates each operand and returns a list of the corresponding values:5 +

    When eval processes a procedure application, it uses list-of-values to produce the list of arguments to which the procedure is to be applied. List-of-values takes as an argument the operands of the combination. It evaluates each operand and returns a list of the corresponding values:5

    (define (list-of-values exps env) @@ -167,7 +166,7 @@

    Procedure arguments

    Conditionals

    -

    Eval-if evaluates the predicate part of an if expression in the given environment. If the result is true, eval-if evaluates the consequent, otherwise it evaluates the alternative: +

    Eval-if evaluates the predicate part of an if expression in the given environment. If the result is true, eval-if evaluates the consequent, otherwise it evaluates the alternative:

    (define (eval-if exp env) @@ -179,11 +178,11 @@

    Conditionals

    C.prompt("scheme-eval-if"); -

    The use of true? in eval-if highlights the issue of the connection between an implemented language and an implementation language. The if-predicate is evaluated in the language being implemented and thus yields a value in that language. The interpreter predicate true? translates that value into a value that can be tested by the if in the implementation language: The metacircular representation of truth might not be the same as that of the underlying Scheme.6 +

    The use of true? in eval-if highlights the issue of the connection between an implemented language and an implementation language. The if-predicate is evaluated in the language being implemented and thus yields a value in that language. The interpreter predicate true? translates that value into a value that can be tested by the if in the implementation language: The metacircular representation of truth might not be the same as that of the underlying Scheme.6

    Sequences

    -

    Eval-sequence is used by apply to evaluate the sequence of expressions in a procedure body and by eval to evaluate the sequence of expressions in a begin expression. It takes as arguments a sequence of expressions and an environment, and evaluates the expressions in the order in which they occur. The value returned is the value of the final expression. +

    Eval-sequence is used by apply to evaluate the sequence of expressions in a procedure body and by eval to evaluate the sequence of expressions in a begin expression. It takes as arguments a sequence of expressions and an environment, and evaluates the expressions in the order in which they occur. The value returned is the value of the final expression.

    (define (eval-sequence exps env) @@ -197,7 +196,7 @@

    Sequences

    Assignments and definitions

    -

    The following procedure handles assignments to variables. It calls eval to find the value to be assigned and transmits the variable and the resulting value to set-variable-value! to be installed in the designated environment. +

    The following procedure handles assignments to variables. It calls eval to find the value to be assigned and transmits the variable and the resulting value to set-variable-value! to be installed in the designated environment.

    (define (eval-assignment exp env) @@ -211,7 +210,7 @@

    Assignments and definitions

    -

    Definitions of variables are handled in a similar manner.7 +

    Definitions of variables are handled in a similar manner.7

    (define (eval-definition exp env) @@ -224,17 +223,16 @@

    Assignments and definitions

    C.prompt("scheme-eval-definition"); -

    We have chosen here to return the symbol ok as the value of an -assignment or a definition.8 +

    We have chosen here to return the symbol ok as the value of an +assignment or a definition.8 -

    -

    Exercise 4.1: Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-of-values will evaluate operands from right to left. +

    +

    Exercise 4.1: Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-of-values will evaluate operands from right to left. -Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left. +Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left.

    - -

    4.1.2 Representing Expressions

    +

    4.1.2 Representing Expressions

    The evaluator is reminiscent of the symbolic differentiation program discussed in section 2.3.2. Both programs operate on symbolic expressions. Inboth programs, the result of operating on a compound expression is determinedby operating recursively on the pieces of the expression and combining theresults in a way that depends on the type of the expression. In both programswe used data abstraction to decouple the general rules of operation from thedetails of how expressions are represented. In the differentiation programthis meant that the same differentiation procedure could deal with algebraicexpressions in prefix form, in infix form, or in some other form. For theevaluator, this means that the syntax of the language being evaluated isdetermined solely by the procedures that classify and extract pieces of expressions. @@ -266,7 +264,7 @@

    4.1.2 Representing Expressions

  • -

    Quotations have the form (quote <text-of-quotation>):9 +

    Quotations have the form (quote <text-of-quotation>):9

    (define (quoted? exp) @@ -278,7 +276,7 @@

    4.1.2 Representing Expressions

    C.prompt("scheme-quoted-p"); -

    Quoted? is defined in terms of the procedure tagged-list?, which identifies lists beginning with a designated symbol: +

    Quoted? is defined in terms of the procedure tagged-list?, which identifies lists beginning with a designated symbol:

    (define (tagged-list? exp tag) @@ -291,7 +289,7 @@

    4.1.2 Representing Expressions

  • -

    Assignments have the form (set! ⟨var⟩ ⟨value⟩): +

    Assignments have the form (set! ⟨var⟩ ⟨value⟩):

    (define (assignment? exp) @@ -358,7 +356,7 @@

    4.1.2 Representing Expressions

  • -

    Lambda expressions are lists that begin with the symbol lambda: +

    Lambda expressions are lists that begin with the symbol lambda:

    (define (lambda? exp) (tagged-list? exp 'lambda)) @@ -371,7 +369,7 @@

    4.1.2 Representing Expressions

    C.prompt("scheme-define-lambda-p"); -

    We also provide a constructor for lambda expressions, which is used by definition-value, above: +

    We also provide a constructor for lambda expressions, which is used by definition-value, above:

    (define (make-lambda parameters body) @@ -382,7 +380,7 @@

    4.1.2 Representing Expressions

  • -

    Conditionals begin with if and have a predicate, a consequent, and an (optional) alternative. If the expression has no alternative part, we provide false as the alternative.10 +

    Conditionals begin with if and have a predicate, a consequent, and an (optional) alternative. If the expression has no alternative part, we provide false as the alternative.10

    (define (if? exp) (tagged-list? exp 'if)) @@ -400,7 +398,7 @@

    4.1.2 Representing Expressions

    C.prompt("scheme-define-if-p"); -

    We also provide a constructor for if expressions, to be used by cond->if to transform cond expressions into if expressions: +

    We also provide a constructor for if expressions, to be used by cond->if to transform cond expressions into if expressions:

    (define (make-if predicate consequent alternative) @@ -411,7 +409,7 @@

    4.1.2 Representing Expressions

  • -

    Begin packages a sequence of expressions into a single expression. We include syntax operations on begin expressions to extract the actual sequence from the begin expression, as well as selectors that return the first expression and the rest of the expressions in the sequence.11 +

    Begin packages a sequence of expressions into a single expression. We include syntax operations on begin expressions to extract the actual sequence from the begin expression, as well as selectors that return the first expression and the rest of the expressions in the sequence.11

    (define (begin? exp) (tagged-list? exp 'begin)) @@ -428,7 +426,7 @@

    4.1.2 Representing Expressions

    C.prompt("scheme-define-begin-p"); -

    We also include a constructor sequence->exp (for use by cond->if) that transforms a sequence into a single expression, using begin if necessary: +

    We also include a constructor sequence->exp (for use by cond->if) that transforms a sequence into a single expression, using begin if necessary:

    (define (sequence->exp seq) @@ -443,7 +441,7 @@

    4.1.2 Representing Expressions

  • -

    A procedure application is any compound expression that is not one of the above expression types. The car of the expression is the operator, and the cdr is the list of operands: +

    A procedure application is any compound expression that is not one of the above expression types. The car of the expression is the operator, and the cdr is the list of operands:

    (define (application? exp) (pair? exp)) @@ -466,7 +464,7 @@

    4.1.2 Representing Expressions

    Derived expressions

    -

    Some special forms in our language can be defined in terms of expressions involving other special forms, rather than being implemented directly. One example is cond, which can be implemented as a nest of if expressions. For example, we can reduce the problem of evaluating the expression +

    Some special forms in our language can be defined in terms of expressions involving other special forms, rather than being implemented directly. One example is cond, which can be implemented as a nest of if expressions. For example, we can reduce the problem of evaluating the expression

    (cond ((> x 0) x) @@ -477,7 +475,7 @@

    Derived expressions

    C.no_output_frozen_prompt("scheme-ex-cond-pre-desugar"); -

    to the problem of evaluating the following expression involving if and begin expressions: +

    to the problem of evaluating the following expression involving if and begin expressions:

    (if (> x 0) @@ -491,9 +489,9 @@

    Derived expressions

    C.no_output_frozen_prompt("scheme-ex-cond-post-desugar"); -

    Implementing the evaluation of cond in this way simplifies the evaluator because it reduces the number of special forms for which the evaluation process must be explicitly specified. +

    Implementing the evaluation of cond in this way simplifies the evaluator because it reduces the number of special forms for which the evaluation process must be explicitly specified. -

    We include syntax procedures that extract the parts of a cond expression, and a procedure cond->if that transforms cond expressions into if expressions. A case analysis begins with cond and has a list of predicate-action clauses. A clause is an else clause if its predicate is the symbol else.12 +

    We include syntax procedures that extract the parts of a cond expression, and a procedure cond->if that transforms cond expressions into if expressions. A case analysis begins with cond and has a list of predicate-action clauses. A clause is an else clause if its predicate is the symbol else.12

    (define (cond? exp) (tagged-list? exp 'cond)) @@ -512,7 +510,7 @@

    Derived expressions

    (define (expand-clauses clauses) (if (null? clauses) - 'false ; no else clause + 'false ; no else clause (let ((first (car clauses)) (rest (cdr clauses))) (if (cond-else-clause? first) @@ -528,45 +526,45 @@

    Derived expressions

    C.prompt("scheme-define-cond-p"); -

    Expressions (such as cond) that we choose to implement as syntactic transformations are called derived expressions. Let expressions are also derived expressions (see Exercise 4-6).4 +

    Expressions (such as cond) that we choose to implement as syntactic transformations are called derived expressions. Let expressions are also derived expressions (see Exercise 4-6).4 -

    -

    Exercise 4.2: Louis Reasoner plans to reorder the cond clauses in eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified eval will usually check fewer clauses than the original eval before identifying the type of an expression. +

    +

    Exercise 4.2: Louis Reasoner plans to reorder the cond clauses in eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified eval will usually check fewer clauses than the original eval before identifying the type of an expression.

    • -

      What is wrong with Louis's plan? (Hint: What will Louis's evaluator do with the expression (define x 3)?) +

      What is wrong with Louis's plan? (Hint: What will Louis's evaluator do with the expression (define x 3)?)

    • -

      Louis is upset that his plan didn't work. He is willing to go to any lengths to make his evaluator recognize procedure applications before it checks for most other kinds of expressions. Help him by changing the syntax of the evaluated language so that procedure applications start with call. For example, instead of (factorial 3) we will now have to write (call factorial 3) and instead of (+ 1 2) we will have to write (call + 1 2). +

      Louis is upset that his plan didn't work. He is willing to go to any lengths to make his evaluator recognize procedure applications before it checks for most other kinds of expressions. Help him by changing the syntax of the evaluated language so that procedure applications start with call. For example, instead of (factorial 3) we will now have to write (call factorial 3) and instead of (+ 1 2) we will have to write (call + 1 2).

    -
    -

    Exercise 4.3: Rewrite eval so that the dispatch is done in data-directed style. Compare this with the data-directed differentiation procedure of Exercise 2-73. (You may use the car of a compound expression as the type of the expression, as is appropriate for the syntax implemented in this section.) +

    +

    Exercise 4.3: Rewrite eval so that the dispatch is done in data-directed style. Compare this with the data-directed differentiation procedure of Exercise 2-73. (You may use the car of a compound expression as the type of the expression, as is appropriate for the syntax implemented in this section.)

    -
    -

    Exercise 4.4: Recall the definitions of the special forms and and or from Chapter 1: +

    +

    Exercise 4.4: Recall the definitions of the special forms and and or from Chapter 1:

    • -

      and: The expressions are evaluated from left to right. If any expression evaluates to false, false is returned; any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then true is returned. +

      and: The expressions are evaluated from left to right. If any expression evaluates to false, false is returned; any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then true is returned.

    • -

      or: The expressions are evaluated from left to right. If any expression evaluates to a true value, that value is returned; any remaining expressions are not evaluated. If all expressions evaluate to false, or if there are no expressions, then false is returned. +

      or: The expressions are evaluated from left to right. If any expression evaluates to a true value, that value is returned; any remaining expressions are not evaluated. If all expressions evaluate to false, or if there are no expressions, then false is returned.

    -

    Install and and or as new special forms for the evaluator by defining appropriate syntax procedures and evaluation procedures eval-and and eval-or. Alternatively, show how to implement and and or as derived expressions. +

    Install and and or as new special forms for the evaluator by defining appropriate syntax procedures and evaluation procedures eval-and and eval-or. Alternatively, show how to implement and and or as derived expressions.

    -
    -

    Exercise 4.5: Scheme allows an additional syntax for cond clauses, (test => recipient)}. If evaluates to a true value, then is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the , and the result is returned as the value of -the cond expression. For example +

    +

    Exercise 4.5: Scheme allows an additional syntax for cond clauses, (test => recipient)}. If <test> evaluates to a true value, then <recipient> is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the <test>, and the result is returned as the value of +the cond expression. For example -

    +
    (cond ((assoc 'b '((a 1) (b 2))) => cadr) (else false))
    @@ -574,15 +572,15 @@

    Derived expressions

    C.prompt(); -

    returns 2. Modify the handling of cond so that it supports this extended syntax. +

    returns 2. Modify the handling of cond so that it supports this extended syntax.

    -
    -

    Exercise 4.6: Let expressions are derived expressions, because +

    +

    Exercise 4.6: Let expressions are derived expressions, because -

    -(let (( ) ... ( )) - ) +
    +(let ((<var_1> <exp_1>) ... (<var_n> <exp_n>)) + <body>)
    -

    Implement a syntactic transformation let->combination that reduces evaluating let expressions to evaluating combinations of the type shown above, and add the appropriate clause to eval to handle let expressions. +

    Implement a syntactic transformation let->combination that reduces evaluating let expressions to evaluating combinations of the type shown above, and add the appropriate clause to eval to handle let expressions.

    -
    -

    Exercise 4.7: Let* is similar to let, except that the bindings of the let variables are performed sequentially from left to right, and each binding is made in an environment in which all of the preceding bindings are visible. For example +

    +

    Exercise 4.7: Let* is similar to let, except that the bindings of the let variables are performed sequentially from left to right, and each binding is made in an environment in which all of the preceding bindings are visible. For example -

    +
    (let* ((x 3) (y (+ x 2)) (z (+ x y 5))) @@ -619,9 +617,9 @@

    Derived expressions

    -

    returns 39. Explain how a let* expression can be rewritten as a set of nested let expressions, and write a procedure let*->nested-lets that performs this transformation. If we have already implemented let (Exercise 4-6) and we want to extend the evaluator to handle let*, is it sufficient to add a clause to eval whose action is +

    returns 39. Explain how a let* expression can be rewritten as a set of nested let expressions, and write a procedure let*->nested-lets that performs this transformation. If we have already implemented let (Exercise 4-6) and we want to extend the evaluator to handle let*, is it sufficient to add a clause to eval whose action is -

    +
    (eval (let*->nested-lets exp) env)
    -

    or must we explicitly expand let* in terms of non-derived expressions? +

    or must we explicitly expand let* in terms of non-derived expressions?

    -
    -

    Exercise 4.8: ``Named let'' is a variant -of let that has the form +

    +

    Exercise 4.8: ``Named let'' is a variant +of let that has the form -

    -(let ) +
    +(let <var> <bindings> <body>)
    -

    The and are just as in ordinary let, except that is bound within to a procedure whose body is and whose parameters are the variables in the . Thus, one can repeatedly execute the by invoking the procedure named . For example, the iterative Fibonacci procedure (section 1-2-2) can be rewritten using named let as follows: +

    The <bindings> and <body> are just as in ordinary let, except that <var> is bound within <body> to a procedure whose body is <body> and whose parameters are the variables in the <bindings>. Thus, one can repeatedly execute the <body> by invoking the procedure named <var>. For example, the iterative Fibonacci procedure (section 1-2-2) can be rewritten using named let as follows: -

    +
    (define (fib n) (let fib-iter ((a 1) (b 0) @@ -658,29 +656,28 @@

    Derived expressions

    C.prompt(); -

    Modify let->combination of Exercise 4-6 to also support named -let. +

    Modify let->combination of Exercise 4-6 to also support named +let.

    -
    -

    Exercise 4.9: Many languages support a variety of iteration constructs, such as do, for, while, and until. In Scheme, iterative processes can be expressed in terms of ordinary procedure calls, so special iteration constructs provide no essential gain in computational power. On the other hand, such constructs are often convenient. Design some iteration constructs, give examples of their use, and +

    +

    Exercise 4.9: Many languages support a variety of iteration constructs, such as do, for, while, and until. In Scheme, iterative processes can be expressed in terms of ordinary procedure calls, so special iteration constructs provide no essential gain in computational power. On the other hand, such constructs are often convenient. Design some iteration constructs, give examples of their use, and show how to implement them as derived expressions.

    -Exercise 4.10: By using data abstraction, we were able to write an eval procedure that is independent of the particular syntax of the language to be evaluated. To illustrate this, design and implement a new syntax for Scheme by modifying the procedures in this section, without changing eval or apply. +Exercise 4.10: By using data abstraction, we were able to write an eval procedure that is independent of the particular syntax of the language to be evaluated. To illustrate this, design and implement a new syntax for Scheme by modifying the procedures in this section, without changing eval or apply.
    - -

    4.1.3 Evaluator Data Structures

    +

    4.1.3 Evaluator Data Structures

    In addition to defining the external syntax of expressions, the evaluator implementation must also define the data structures that the evaluator manipulates internally, as part of the execution of a program, such as the representation of procedures and environments and the representation of true and false.

    Testing of predicates

    -

    For conditionals, we accept anything to be true that is not the explicit false object. +

    For conditionals, we accept anything to be true that is not the explicit false object. -

    +
    (define (true? x) (not (eq? x false))) @@ -698,17 +695,15 @@

    Representing procedures

    • -(apply-primitive-procedure proc args)} - +(apply-primitive-procedure proc args) applies the given primitive procedure to the argument values in the list - and returns the result of the application. +<args> and returns the result of the application.
    • -(primitive-procedure? proc) - +(primitive-procedure? proc) -

      tests whether is a primitive procedure. +tests whether <proc> is a primitive procedure.

    @@ -716,9 +711,9 @@

    Representing procedures

    4-1-4.

    Compound procedures are constructed from parameters, procedure bodies, and -environments using the constructor make-procedure: +environments using the constructor make-procedure: -

    +
    (define (make-procedure parameters body env) (list 'procedure parameters body env)) @@ -745,39 +740,39 @@

    Operations on Environments

    • -(lookup-variable-value var env)} - -returns the value that is bound to the symbol in the environment -, or signals an error if the variable is unbound. +(lookup-variable-value var env) +returns the value that is bound to the symbol <var> in the environment +<env>, or signals an error if the variable is unbound. +
    • -(extend-environment variables values base-env)} +(extend-environment variables values base-env)

      returns a new environment, consisting of a new frame in which the symbols in -the list are bound to the corresponding elements in the list -, where the enclosing environment is the environment -. - +the list <variables> are bound to the corresponding elements in the list +<values>, where the enclosing environment is the environment +<base-env>. +

    • -(define-variable! var value env)} - -adds to the first frame in the environment a new binding that -associates the variable with the value . +(define-variable! var value env) +adds to the first frame in the environment <env> a new binding that +associates the variable <var> with the value <value>. +
    • -(set-variable-value! var value env)} +(set-variable-value! var value env) -changes the binding of the variable in the environment -so that the variable is now bound to the value , or signals an +changes the binding of the variable <var> in the environment <env> +so that the variable is now bound to the value <value>, or signals an error if the variable is unbound. - +

    To implement these operations we represent an environment as a list of frames. -The enclosing environment of an environment is the cdr of the list. The +The enclosing environment of an environment is the cdr of the list. The empty environment is simply the empty list. -

    +
    (define (enclosing-environment env) (cdr env)) (define (first-frame env) (car env)) @@ -790,9 +785,9 @@

    Operations on Environments

    Each frame of an environment is represented as a pair of lists: a list of the variables bound in that frame and a list of the associated -values.14 +values.14 -

    +
    (define (make-frame variables values) (cons variables values)) @@ -813,11 +808,11 @@

    Operations on Environments

    we adjoin this to the environment. We signal an error if the number of variables does not match the number of values. -
    +
    (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) - (if (< (length vars) (length vals)) + (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals))))
    @@ -831,7 +826,7 @@

    Operations on Environments

    frame, we search the enclosing environment, and so on. If we reach the empty environment, we signal an ``unbound variable'' error. -
    +
    (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) @@ -852,10 +847,10 @@

    Operations on Environments

    To set a variable to a new value in a specified environment, we scan for the -variable, just as in lookup-variable-value, and change the corresponding +variable, just as in lookup-variable-value, and change the corresponding value when we find it. -

    +
    (define (set-variable-value! var val env) (define (env-loop env) (define (scan vars vals) @@ -876,10 +871,10 @@

    Operations on Environments

    To define a variable, we search the first frame for a binding for the variable, -and change the binding if it exists (just as in set-variable-value!). +and change the binding if it exists (just as in set-variable-value!). If no such binding exists, we adjoin one to the first frame. -

    +
    (define (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) @@ -895,37 +890,36 @@

    Operations on Environments

    C.prompt(); -

    The method described here is only one of many plausible ways to represent environments. Since we used data abstraction to isolate the rest of the evaluator from the detailed choice of representation, we could change the environment representation if we wanted to. (See Exercise 4-11.) In a production-quality Lisp system, the speed of the evaluator's environment operations---especially that of variable lookup---has a major impact on the performance of the system. The representation described here, although conceptually simple, is not efficient and would not ordinarily be used in a production system.15 +

    The method described here is only one of many plausible ways to represent environments. Since we used data abstraction to isolate the rest of the evaluator from the detailed choice of representation, we could change the environment representation if we wanted to. (See Exercise 4-11.) In a production-quality Lisp system, the speed of the evaluator's environment operations---especially that of variable lookup---has a major impact on the performance of the system. The representation described here, although conceptually simple, is not efficient and would not ordinarily be used in a production system.15 -

    +

    Exercise 4.11: Instead of representing a frame as a pair of lists, we can represent a frame as a list of bindings, where each binding is a name-value pair. Rewrite the environment operations to use this alternative representation.

    -
    +

    Exercise 4.12: The procedures -set-variable-value!, define-variable!, and -lookup-variable-value can be expressed in terms of more abstract +set-variable-value!, define-variable!, and +lookup-variable-value can be expressed in terms of more abstract procedures for traversing the environment structure. Define abstractions that capture the common patterns and redefine the three procedures in terms of these abstractions.

    -
    +

    Exercise 4.13: Scheme allows us to create new -bindings for variables by means of define, but provides no way to get +bindings for variables by means of define, but provides no way to get rid of bindings. Implement for the evaluator a special form -make-unbound! that removes the binding of a given symbol from the -environment in which the make-unbound! expression is evaluated. This +make-unbound! that removes the binding of a given symbol from the +environment in which the make-unbound! expression is evaluated. This problem is not completely specified. For example, should we remove only the binding in the first frame of the environment? Complete the specification and justify any choices you make.

    - -

    4.1.4 Running the Evaluator as a Program

    +

    4.1.4 Running the Evaluator as a Program

    Given the evaluator, we have in our hands a description (expressed in Lisp) of the process by which Lisp expressions are evaluated. One advantage of @@ -940,14 +934,14 @@

    4.1.4 Running the Evaluator as a Program

    application of primitive procedures.

    There must be a binding for each primitive procedure name, so that when -eval evaluates the operator of an application of a primitive, it will -find an object to pass to apply. We thus set up a global environment +eval evaluates the operator of an application of a primitive, it will +find an object to pass to apply. We thus set up a global environment that associates unique objects with the names of the primitive procedures that can appear in the expressions we will be evaluating. The global environment -also includes bindings for the symbols true and false, so that +also includes bindings for the symbols true and false, so that they can be used as variables in expressions to be evaluated. -

    +
    (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) @@ -964,13 +958,13 @@

    4.1.4 Running the Evaluator as a Program

    It does not matter how we represent the primitive procedure objects, so long as -apply can identify and apply them by using the procedures -primitive-procedure? and apply-primitive-procedure. We have +apply can identify and apply them by using the procedures +primitive-procedure? and apply-primitive-procedure. We have chosen to represent a primitive procedure as a list beginning with the symbol -primitive and containing a procedure in the underlying Lisp that +primitive and containing a procedure in the underlying Lisp that implements that primitive. -

    +
    (define (primitive-procedure? proc) (tagged-list? proc 'primitive)) @@ -980,16 +974,16 @@

    4.1.4 Running the Evaluator as a Program

    C.prompt(); -

    Setup-environment will get the primitive names and implementation -procedures from a list:16 +

    Setup-environment will get the primitive names and implementation +procedures from a list:16 -

    +
    (define primitive-procedures (list (list 'car car) (list 'cdr cdr) (list 'cons cons) (list 'null? null?) - + <more primitives> )) (define (primitive-procedure-names) @@ -1006,9 +1000,9 @@

    4.1.4 Running the Evaluator as a Program

    To apply a primitive procedure, we simply apply the implementation procedure to the arguments, using the underlying Lisp -system:17 +system:17 -

    +
    (define (apply-primitive-procedure proc args) (apply-in-underlying-scheme (primitive-implementation proc) args)) @@ -1018,16 +1012,16 @@

    4.1.4 Running the Evaluator as a Program

    For convenience in running the metacircular evaluator, we provide a -driver loop +driver loop that models the read-eval-print loop of the underlying -Lisp system. It prints a prompt +Lisp system. It prints a prompt , reads an input expression, evaluates this expression in the global environment, and prints the result. We -precede each printed result by an output prompt +precede each printed result by an output prompt so as to distinguish -the value of the expression from other output that may be printed.18 +the value of the expression from other output that may be printed.18 -

    +
    (define input-prompt ";;; M-Eval input:") (define output-prompt ";;; M-Eval value:") @@ -1049,17 +1043,17 @@

    4.1.4 Running the Evaluator as a Program

    C.prompt(); -

    We use a special printing procedure, user-print, to avoid printing the +

    We use a special printing procedure, user-print, to avoid printing the environment part of a compound procedure, which may be a very long list (or may even contain cycles). -

    +
    (define (user-print object) (if (compound-procedure? object) (display (list 'compound-procedure (procedure-parameters object) (procedure-body object) - ')) + '<procedure-env>)) (display object)))
    -
    +

    Exercise 4.14: Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the -definition of map, and runs some test programs that use it. They work -fine. Louis, in contrast, has installed the system version of map as a +definition of map, and runs some test programs that use it. They work +fine. Louis, in contrast, has installed the system version of map as a primitive for the metacircular evaluator. When he tries it, things go terribly -wrong. Explain why Louis's map fails even though Eva's works. +wrong. Explain why Louis's map fails even though Eva's works.

    - -

    4.1.5 Data as Programs

    +

    4.1.5 Data as Programs

    In thinking about a Lisp program that evaluates Lisp expressions, an analogy might be helpful. One operational view of the meaning of a program is that a program is a description of an abstract (perhaps infinitely large) machine. For example, consider the familiar program to compute factorials: -

    +
    (define (factorial n) (if (= n 1) 1 @@ -1159,7 +1152,7 @@

    4.1.5 Data as Programs

    In a similar way, we can regard the evaluator as a very special machine that takes as input a description of a machine. Given this input, the evaluator configures itself to emulate the machine described. For example, if we feed -our evaluator the definition of factorial, as shown in Figure 4-3, +our evaluator the definition of factorial, as shown in Figure 4-3, the evaluator will be able to compute factorials.

    @@ -1170,8 +1163,8 @@

    4.1.5 Data as Programs

    -

    From this perspective, our evaluator is seen to be a universal -machine +

    From this perspective, our evaluator is seen to be a universal +machine . It mimics other machines when these are described as Lisp programs.@footnote{The fact that the machines are described in Lisp is inessential. If we give our evaluator a Lisp program that behaves as an @@ -1182,14 +1175,14 @@

    4.1.5 Data as Programs

    other. Thus, the notion of ``what can in principle be computed'' (ignoring practicalities of time and memory required) is independent of the language or the computer, and instead reflects an underlying notion of -computability +computability . This was first demonstrated in a clear way by Alan M. Turing (1912-1954), whose 1936 paper laid the foundations for theoretical computer science. In the paper, Turing presented a simple computational -model---now known as a Turing machine +model---now known as a Turing machine ---and argued that any ``effective process'' can be formulated as a program for such a machine. (This -argument is known as the Church-Turing thesis +argument is known as the Church-Turing thesis .) Turing then implemented a universal machine, i.e., a Turing machine that behaves as an evaluator for Turing-machine programs. He used this framework to demonstrate @@ -1207,8 +1200,8 @@

    4.1.5 Data as Programs

    program.@footnote{Some people find it counterintuitive that an evaluator, which is implemented by a relatively simple procedure, can emulate programs that are more complex than the evaluator itself. The existence of a universal evaluator -machine is a deep and wonderful property of computation. Recursion -theory +machine is a deep and wonderful property of computation. Recursion +theory , a branch of mathematical logic, is concerned with logical limits of computation. Douglas Hofstadter's beautiful book @cite{G@"odel, Escher, Bach} (1979) explores some of these ideas.} @@ -1218,25 +1211,25 @@

    4.1.5 Data as Programs

    programming language itself. Imagine that the evaluator program (implemented in Lisp) is running, and that a user is typing expressions to the evaluator and observing the results. From the perspective of the user, an input expression -such as (* x x) is an expression in the programming language, which the +such as (* x x) is an expression in the programming language, which the evaluator should execute. From the perspective of the evaluator, however, the -expression is simply a list (in this case, a list of three symbols: *, -x, and x) that is to be manipulated according to a well-defined +expression is simply a list (in this case, a list of three symbols: *, +x, and x) that is to be manipulated according to a well-defined set of rules. That the user's programs are the evaluator's data need not be a source of confusion. In fact, it is sometimes convenient to ignore this distinction, and to give the user the ability to explicitly evaluate a data object as a Lisp -expression, by making eval available for use in programs. Many Lisp -dialects provide a primitive eval procedure that takes as arguments an +expression, by making eval available for use in programs. Many Lisp +dialects provide a primitive eval procedure that takes as arguments an expression and an environment and evaluates the expression relative to the -environment.@footnote{Warning: This eval primitive is not identical to -the eval procedure we implemented in section
    4.1.1, because it +environment.@footnote{Warning: This eval primitive is not identical to +the eval procedure we implemented in section 4.1.1, because it uses actual Scheme environments rather than the sample environment structures we built in section 4.1.3. These actual environments cannot be manipulated by the user as ordinary lists; they must be accessed via -eval or other special operations. Similarly, the apply primitive -we saw earlier is not identical to the metacircular apply, because it +eval or other special operations. Similarly, the apply primitive +we saw earlier is not identical to the metacircular apply, because it uses actual Scheme procedures rather than the procedure objects we constructed in sections 4-1-3 and 4-1-4.} Thus, @@ -1258,18 +1251,18 @@

    4.1.5 Data as Programs

    will both return 25.@footnote{The @acronym{MIT} implementation of Scheme -includes eval, as well as a symbol user-initial-environment that +includes eval, as well as a symbol user-initial-environment that is bound to the initial environment in which the user's input expressions are evaluated.} -

    +

    Exercise 4.15: Given a one-argument procedure -p and an object a, p is said to ``halt'' on a if -evaluating the expression (p a) returns a value (as opposed to +p and an object a, p is said to ``halt'' on a if +evaluating the expression (p a) returns a value (as opposed to terminating with an error message or running forever). Show that it is -impossible to write a procedure halts? that correctly determines whether -p halts on a for any procedure p and object a. Use -the following reasoning: If you had such a procedure halts?, you could +impossible to write a procedure halts? that correctly determines whether +p halts on a for any procedure p and object a. Use +the following reasoning: If you had such a procedure halts?, you could implement the following program:

    @@ -1284,20 +1277,19 @@

    4.1.5 Data as Programs

    C.no_output_frozen_prompt("scheme-define-run-forever"); -

    Now consider evaluating the expression (try try) and show that any +

    Now consider evaluating the expression (try try) and show that any possible outcome (either halting or running forever) violates the intended -behavior of halts?.@footnote{Although we stipulated that halts? +behavior of halts?.@footnote{Although we stipulated that halts? is given a procedure object, notice that this reasoning still applies even if -halts? can gain access to the procedure's text and its environment. -This is Turing's celebrated Halting Theorem +halts? can gain access to the procedure's text and its environment. +This is Turing's celebrated Halting Theorem , which gave the first -clear example of a non-computable +clear example of a non-computable problem, i.e., a well-posed task that cannot be carried out as a computational procedure.}

    - -

    4.1.6 Internal Definitions

    +

    4.1.6 Internal Definitions

    Our environment model of evaluation and our metacircular evaluator execute definitions in sequence, extending the environment frame one definition at a @@ -1320,29 +1312,30 @@

    4.1.6 Internal Definitions

    (if (= n 0) false (even? (- n 1)))) - ⟨rest of body of f⟩) + ; rest of body of f + )
    -

    Our intention here is that the name odd? in the body of the procedure -even? should refer to the procedure odd? that is defined after -even?. The scope of the name odd? is the entire body of -f, not just the portion of the body of f starting at the point -where the define for odd? occurs. Indeed, when we consider that -odd? is itself defined in terms of even?---so that even? -and odd? are mutually recursive procedures---we see that the only -satisfactory interpretation of the two defines is to regard them as if -the names even? and odd? were being added to the environment +

    Our intention here is that the name odd? in the body of the procedure +even? should refer to the procedure odd? that is defined after +even?. The scope of the name odd? is the entire body of +f, not just the portion of the body of f starting at the point +where the define for odd? occurs. Indeed, when we consider that +odd? is itself defined in terms of even?---so that even? +and odd? are mutually recursive procedures---we see that the only +satisfactory interpretation of the two defines is to regard them as if +the names even? and odd? were being added to the environment simultaneously. More generally, in block structure, the scope of a local name -is the entire procedure body in which the define is evaluated. +is the entire procedure body in which the define is evaluated. -As it happens, our interpreter will evaluate calls to f correctly, but +As it happens, our interpreter will evaluate calls to f correctly, but for an ``accidental'' reason: Since the definitions of the internal procedures come first, no calls to these procedures will be evaluated until all of them -have been defined. Hence, odd? will have been defined by the time -even? is executed. In fact, our sequential evaluation mechanism will +have been defined. Hence, odd? will have been defined by the time +even? is executed. In fact, our sequential evaluation mechanism will give the same result as a mechanism that directly implements simultaneous definition for any procedure in which the internal definitions come first in a body and evaluation of the value expressions for the defined variables doesn't @@ -1363,17 +1356,17 @@

    4.1.6 Internal Definitions

    There is, however, a simple way to treat definitions so that internally defined names have truly simultaneous scope---just create all local variables that will be in the current environment before evaluating any of the value expressions. -One way to do this is by a syntax transformation on lambda expressions. -Before evaluating the body of a lambda expression, we ``scan out'' and +One way to do this is by a syntax transformation on lambda expressions. +Before evaluating the body of a lambda expression, we ``scan out'' and eliminate all the internal definitions in the body. The internally defined -variables will be created with a let and then set to their values by +variables will be created with a let and then set to their values by assignment. For example, the procedure -
    -(lambda - (define u ) - (define v ) - ) +
    +(lambda <vars> + (define u <e1>) + (define v <e2>) + <e3>)
    -where *unassigned* is a special symbol that causes looking up a variable +where *unassigned* is a special symbol that causes looking up a variable to signal an error if an attempt is made to use the value of the not-yet-assigned variable. @@ -1409,24 +1402,24 @@

    4.1.6 Internal Definitions

    the transformation shown above. Thus, some programs that don't obey this restriction will in fact run in such implementations.} -
    +

    Exercise 4.16: In this exercise we implement the method just described for interpreting internal definitions. We assume that -the evaluator supports let (see Exercise 4-6). +the evaluator supports let (see Exercise 4-6).

    • -Change lookup-variable-value (section 4.1.3) to signal an error if -the value it finds is the symbol *unassigned*. +Change lookup-variable-value (section 4.1.3) to signal an error if +the value it finds is the symbol *unassigned*.
    • -Write a procedure scan-out-defines that takes a procedure body and +Write a procedure scan-out-defines that takes a procedure body and returns an equivalent one that has no internal definitions, by making the transformation described above.
    • -Install scan-out-defines in the interpreter, either in -make-procedure or in procedure-body (see section 4.1.3). +Install scan-out-defines in the interpreter, either in +make-procedure or in procedure-body (see section 4.1.3). Which place is better? Why?
    @@ -1434,9 +1427,9 @@

    4.1.6 Internal Definitions

    -

    +

    Exercise 4.17: Draw diagrams of the environment -in effect when evaluating the expression in the procedure in the +in effect when evaluating the expression <e3> in the procedure in the text, comparing how this will be structured when definitions are interpreted sequentially with how it will be structured if definitions are scanned out as described. Why is there an extra frame in the transformed program? Explain @@ -1448,29 +1441,29 @@

    4.1.6 Internal Definitions

    -

    +

    Exercise 4.18: Consider an alternative strategy for scanning out definitions that translates the example in the text to -

    -(lambda +
    +(lambda <vars> (let ((u '*unassigned*) (v '*unassigned*)) - (let ((a ) - (b )) + (let ((a <e1>) + (b <e2>)) (set! u a) (set! v b)) - )) + <e3>))
    -Here a and b are meant to represent new variable names, created +Here a and b are meant to represent new variable names, created by the interpreter, that do not appear in the user's program. Consider the -solve procedure from section 3.5.4: +solve procedure from section 3.5.4: -
    +
    (define (solve f y0 dt) (define y (integral (delay dy) y0 dt)) (define dy (stream-map f y)) @@ -1486,12 +1479,12 @@

    4.1.6 Internal Definitions

    -

    +

    Exercise 4.19: Ben Bitdiddle, Alyssa P. Hacker, and Eva Lu Ator are arguing about the desired result of evaluating the expression -

    +
    (let ((a 1)) (define (f x) (define b (+ a x)) @@ -1504,17 +1497,17 @@

    4.1.6 Internal Definitions

    Ben asserts that the result should be obtained using the sequential rule for -define: b is defined to be 11, then a is defined to be 5, +define: b is defined to be 11, then a is defined to be 5, so the result is 16. Alyssa objects that mutual recursion requires the simultaneous scope rule for internal procedure definitions, and that it is unreasonable to treat procedure names differently from other names. Thus, she argues for the mechanism implemented in Exercise 4-16. This would lead -to a being unassigned at the time that the value for b is to be +to a being unassigned at the time that the value for b is to be computed. Hence, in Alyssa's view the procedure should produce an error. Eva -has a third opinion. She says that if the definitions of a and b -are truly meant to be simultaneous, then the value 5 for a should be -used in evaluating b. Hence, in Eva's view a should be 5, -b should be 15, and the result should be 20. Which (if any) of these +has a third opinion. She says that if the definitions of a and b +are truly meant to be simultaneous, then the value 5 for a should be +used in evaluating b. Hence, in Eva's view a should be 5, +b should be 15, and the result should be 20. Which (if any) of these viewpoints do you support? Can you devise a way to implement internal definitions so that they behave as Eva prefers?@footnote{The @acronym{MIT} implementors of Scheme support Alyssa on the following grounds: Eva is in @@ -1527,16 +1520,16 @@

    4.1.6 Internal Definitions

    -

    +

    Exercise 4.20: Because internal definitions look sequential but are actually simultaneous, some people prefer to avoid them -entirely, and use the special form letrec instead. Letrec looks -like let, so it is not surprising that the variables it binds are bound +entirely, and use the special form letrec instead. Letrec looks +like let, so it is not surprising that the variables it binds are bound simultaneously and have the same scope as each other. The sample procedure -f above can be written without internal definitions, but with exactly +f above can be written without internal definitions, but with exactly the same meaning, as -

    +
    (define (f x) (letrec ((even? (lambda (n) @@ -1554,25 +1547,25 @@

    4.1.6 Internal Definitions

    C.prompt(); -

    Letrec expressions, which have the form +

    Letrec expressions, which have the form -

    -(letrec (( ) ... ( )) - ) +
    +(letrec ((<var_1> <exp_1>) ... (<var_n> <exp_n>)) + <body>)
    -

    are a variation on let in which the expressions - that provide the initial values for the -variables are evaluated in an environment -that includes all the letrec bindings. This permits recursion in the -bindings, such as the mutual recursion of even? and odd? in the +

    are a variation on let in which the expressions +<exp_k> that provide the initial values for the +variables <var_k> are evaluated in an environment +that includes all the letrec bindings. This permits recursion in the +bindings, such as the mutual recursion of even? and odd? in the example above, or the evaluation of 10 factorial with -

    +
    (letrec ((fact (lambda (n) (if (= n 1) @@ -1586,42 +1579,42 @@

    4.1.6 Internal Definitions

    • -

      Implement letrec as a derived expression, by transforming a -letrec expression into a let expression as shown in the text -above or in Exercise 4-18. That is, the letrec variables should -be created with a let and then be assigned their values with -set!. +

      Implement letrec as a derived expression, by transforming a +letrec expression into a let expression as shown in the text +above or in Exercise 4-18. That is, the letrec variables should +be created with a let and then be assigned their values with +set!.

    • Louis Reasoner is confused by all this fuss about internal definitions. The -way he sees it, if you don't like to use define inside a procedure, you -can just use let. Illustrate what is loose about his reasoning by +way he sees it, if you don't like to use define inside a procedure, you +can just use let. Illustrate what is loose about his reasoning by drawing an environment diagram that shows the environment in which the -⟨rest of body of f⟩ is evaluated during evaluation of the -expression (f 5), with f defined as in this exercise. Draw an -environment diagram for the same evaluation, but with let in place of -letrec in the definition of f. +⟨rest of body of f⟩ is evaluated during evaluation of the +expression (f 5), with f defined as in this exercise. Draw an +environment diagram for the same evaluation, but with let in place of +letrec in the definition of f.

    -

    +

    Exercise 4.21: Amazingly, Louis's intuition in Exercise 4-20 is correct. It is indeed possible to specify recursive -procedures without using letrec (or even define), although the +procedures without using letrec (or even define), although the method for accomplishing this is much more subtle than Louis imagined. The following expression computes 10 factorial by applying a recursive factorial procedure:@footnote{This example illustrates a programming trick for -formulating recursive procedures without using define. The most general -trick of this sort is the Y operator +formulating recursive procedures without using define. The most general +trick of this sort is the Y operator , which can be used to give a ``pure [lambda]-calculus'' implementation of recursion. (See Stoy 1977 for details on the [lambda] calculus, and Gabriel 1988 for an exposition of the Y operator in Scheme.)} -

    +
    ((lambda (n) ((lambda (fact) (fact fact n)) @@ -1644,7 +1637,7 @@

    4.1.6 Internal Definitions

    Consider the following procedure, which includes mutually recursive internal definitions: -

    +
    (define (f x) (define (even? n) (if (= n 0) @@ -1661,16 +1654,16 @@

    4.1.6 Internal Definitions

    Fill in the missing expressions to complete an alternative definition of -f, which uses neither internal definitions nor letrec: +f, which uses neither internal definitions nor letrec: -

    +
    (define (f x) ((lambda (even? odd?) (even? even? odd? x)) (lambda (ev? od? n) - (if (= n 0) true (od? ))) + (if (= n 0) true (od? <??> <??> <??>))) (lambda (ev? od? n) - (if (= n 0) false (ev? ))))) + (if (= n 0) false (ev? <??> <??> <??>)))))
    -

    Each time factorial is called, the evaluator must determine that the -body is an if expression and extract the predicate. Only then can it +

    Each time factorial is called, the evaluator must determine that the +body is an if expression and extract the predicate. Only then can it evaluate the predicate and dispatch on its value. Each time it evaluates the -expression (* (factorial (- n 1)) n), or the subexpressions -(factorial (- n 1)) and (- n 1), the evaluator must perform the -case analysis in eval to determine that the expression is an +expression (* (factorial (- n 1)) n), or the subexpressions +(factorial (- n 1)) and (- n 1), the evaluator must perform the +case analysis in eval to determine that the expression is an application, and must extract its operator and operands. This analysis is expensive. Performing it repeatedly is wasteful. @@ -1712,19 +1704,19 @@

    4.1.7 Separating Syntactic Analysis from Execution

    discuss in Chapter 5. Jonathan Rees wrote a Scheme interpreter like this in about 1982 for the T project (Rees and Adams 1982). Marc Feeley (1986) (see also Feeley and Lapalme 1987) independently invented this technique in his -master's thesis.} We split eval, which takes an expression and an -environment, into two parts. The procedure analyze takes only the +master's thesis.} We split eval, which takes an expression and an +environment, into two parts. The procedure analyze takes only the expression. It performs the syntactic analysis and returns a new procedure, -the execution procedure +the execution procedure , that encapsulates the work to be done in executing the analyzed expression. The execution procedure takes an environment as its argument and completes the evaluation. This saves work -because analyze will be called only once on an expression, while the +because analyze will be called only once on an expression, while the execution procedure may be called many times. -

    With the separation into analysis and execution, eval now becomes +

    With the separation into analysis and execution, eval now becomes -

    +
    (define (eval exp env) ((analyze exp) env))
    @@ -1732,12 +1724,12 @@

    4.1.7 Separating Syntactic Analysis from Execution

    C.prompt(); -

    The result of calling analyze is the execution procedure to be applied -to the environment. The analyze procedure is the same case analysis as -performed by the original eval of section 4.1.1, except that the +

    The result of calling analyze is the execution procedure to be applied +to the environment. The analyze procedure is the same case analysis as +performed by the original eval of section 4.1.1, except that the procedures to which we dispatch perform only analysis, not full evaluation: -

    +
    (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) @@ -1761,7 +1753,7 @@

    4.1.7 Separating Syntactic Analysis from Execution

    self-evaluating expressions. It returns an execution procedure that ignores its environment argument and just returns the expression: -
    +
    (define (analyze-self-evaluating exp) (lambda (env) exp))
    @@ -1773,7 +1765,7 @@

    4.1.7 Separating Syntactic Analysis from Execution

    of the quotation only once, in the analysis phase, rather than in the execution phase. -
    +
    (define (analyze-quoted exp) (let ((qval (text-of-quotation exp))) (lambda (env) qval))) @@ -1790,7 +1782,7 @@

    4.1.7 Separating Syntactic Analysis from Execution

    be found, thus obviating the need to scan the environment for the entry that matches the variable.} -
    +
    (define (analyze-variable exp) (lambda (env) (lookup-variable-value exp env)))
    @@ -1798,14 +1790,14 @@

    4.1.7 Separating Syntactic Analysis from Execution

    C.prompt(); -

    Analyze-assignment also must defer actually setting the variable until +

    Analyze-assignment also must defer actually setting the variable until the execution, when the environment has been supplied. However, the fact that -the assignment-value expression can be analyzed (recursively) during -analysis is a major gain in efficiency, because the assignment-value +the assignment-value expression can be analyzed (recursively) during +analysis is a major gain in efficiency, because the assignment-value expression will now be analyzed only once. The same holds true for definitions. -

    +
    (define (analyze-assignment exp) (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) @@ -1824,10 +1816,10 @@

    4.1.7 Separating Syntactic Analysis from Execution

    C.prompt(); -

    For if expressions, we extract and analyze the predicate, consequent, +

    For if expressions, we extract and analyze the predicate, consequent, and alternative at analysis time. -

    +
    (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) @@ -1841,11 +1833,11 @@

    4.1.7 Separating Syntactic Analysis from Execution

    C.prompt(); -

    Analyzing a lambda expression also achieves a major gain in efficiency: -We analyze the lambda body only once, even though procedures resulting -from evaluation of the lambda may be applied many times. +

    Analyzing a lambda expression also achieves a major gain in efficiency: +We analyze the lambda body only once, even though procedures resulting +from evaluation of the lambda may be applied many times. -

    +
    (define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) (bproc (analyze-sequence (lambda-body exp)))) @@ -1855,15 +1847,15 @@

    4.1.7 Separating Syntactic Analysis from Execution

    C.prompt(); -

    Analysis of a sequence of expressions (as in a begin or the body of a -lambda expression) is more involved.@footnote{See Exercise 4-23 +

    Analysis of a sequence of expressions (as in a begin or the body of a +lambda expression) is more involved.@footnote{See Exercise 4-23 for some insight into the processing of sequences.} Each expression in the sequence is analyzed, yielding an execution procedure. These execution procedures are combined to produce an execution procedure that takes an environment as argument and sequentially calls each individual execution procedure with the environment as argument. -

    +
    (define (analyze-sequence exps) (define (sequentially proc1 proc2) (lambda (env) (proc1 env) (proc2 env))) @@ -1885,13 +1877,13 @@

    4.1.7 Separating Syntactic Analysis from Execution

    an execution procedure that calls the operator execution procedure (to obtain the actual procedure to be applied) and the operand execution procedures (to obtain the actual arguments). We then pass these to -execute-application, which is the analog of apply in section -4-1-1. Execute-application differs from apply in that the +execute-application, which is the analog of apply in section +4-1-1. Execute-application differs from apply in that the procedure body for a compound procedure has already been analyzed, so there is no need to do further analysis. Instead, we just call the execution procedure for the body on the extended environment. -
    +
    (define (analyze-application exp) (let ((fproc (analyze (operator exp))) (aprocs (map analyze (operands exp)))) @@ -1921,21 +1913,21 @@

    4.1.7 Separating Syntactic Analysis from Execution

    run-time support procedures as in sections 4-1-2, 4-1-3, and 4-1-4. -
    +

    Exercise 4.22: Extend the evaluator in this -section to support the special form let. (See Exercise 4-6.) +section to support the special form let. (See Exercise 4-6.)

    -

    +

    Exercise 4.23: Alyssa P. Hacker doesn't -understand why analyze-sequence needs to be so complicated. All the +understand why analyze-sequence needs to be so complicated. All the other analysis procedures are straightforward transformations of the -corresponding evaluation procedures (or eval clauses) in section -4-1-1. She expected analyze-sequence to look like this: +corresponding evaluation procedures (or eval clauses) in section +4-1-1. She expected analyze-sequence to look like this: -

    +
    (define (analyze-sequence exps) (define (execute-sequence procs env) (cond ((null? (cdr procs)) ((car procs) env)) @@ -1957,7 +1949,7 @@

    4.1.7 Separating Syntactic Analysis from Execution

    although the individual expressions in the sequence have been analyzed, the sequence itself has not been. -

    Compare the two versions of analyze-sequence. For example, consider the +

    Compare the two versions of analyze-sequence. For example, consider the common case (typical of procedure bodies) where the sequence has just one expression. What work will the execution procedure produced by Alyssa's program do? What about the execution procedure produced by the program in the @@ -1967,7 +1959,7 @@

    4.1.7 Separating Syntactic Analysis from Execution

    -

    +

    Exercise 4.24: Design and carry out some experiments to compare the speed of the original metacircular evaluator with the version in this section. Use your results to estimate the fraction of time @@ -1985,11 +1977,11 @@

    4.1.7 Separating Syntactic Analysis from Execution