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}}
-
+
-
+
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}} -
+
-
-
+
+
![]()
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
2with the namesize.8 Once the namesizehas been associated with the number2, we can refer to the value2by 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. +
defineis 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 -
+- -Exercise 1.1.2. 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. @@ -339,8 +337,7 @@
1.1.2 Naming and the Environment
1.1.3 Evaluating Combinations
+1.1.3 Evaluating Combinations
One of our goals in this chapter is to isolate issues about thinking procedurally. As a case in point, let us consider that, in evaluating combinations, the interpreter is itself following a procedure. @@ -352,7 +349,7 @@
1.1.3 Evaluating Combinations
Apply the procedure that is the value of the leftmost subexpression (the operator) to the arguments that are the values of the other subexpressions (the operands). - 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. -
+![]()
![]()
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 symbolx(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 symbolxand the other of which is3, 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
fis the procedure defined in section 1.1.4. We begin by retrieving the body off:(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
aby the argument5:(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)produces6and(* 5 2)produces10, 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
xreplaced 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$."
elseis 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 @@
- -(and <e1> ... <en>) +
(and <e1> ... <en>)-The interpreter evaluates the expressions <e> one at a time, in left-to-right order. If any <e> evaluates to false, the value of the and expression is false, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to true values, the value of the and expression is the value of the last one. +
The interpreter evaluates the expressions
<e>one at a time, in left-to-right order. If any<e>evaluates to false, the value of the and expression is false, and the rest of the<e>'s are not evaluated. If all<e>'s evaluate to true values, the value of the and expression is the value of the last one.- -(or <e1> ... <en>) +
(or <e1> ... <en>)-The interpreter evaluates the expressions <e> one at a time, in left-to-right order. If any <e> evaluates to a true value, that value is returned as the value of the or expression, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to false, the value of the or expression is false. +
The interpreter evaluates the expressions
<e>one at a time, in left-to-right order. If any<e>evaluates to a true value, that value is returned as the value of the or expression, and the rest of the<e>'s are not evaluated. If all<e>'s evaluate to false, the value of the or expression is false.- -(not <e>) +
(not <e>)-The value of a not expression is true when the expression <e> evaluates to false, and false otherwise. +
The value of a not expression is true when the expression
<e>evaluates to false, and false otherwise.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
ifneeds to be provided as a special form. “Why can’t I just define it as an ordinary procedure in terms ofcond?” she asks. Alyssa’s friend Eva Lu Ator claims this can indeed be done, and she defines a new version ofif:(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: onlyifcan 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. +
+
Figure 1.2: Procedural decomposition of the
sqrtprogram.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
#tand#f. When the interpreter checks a predicate's value, it interprets#fas false. Any other value is treated as true. (Thus, providing#tis logically unnecessary, but it is convenient.) In this book we will use namestrueandfalse, which are associated with the values#tand#frespectively.-18 abs also uses the “minus” operator -, which, when used with a single operand, as in (- x), indicates negation. +
18
absalso 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
ifandcondis that the<e>part of eachcondclause 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 thecond. In anifexpression, 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
Ais 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
@@ -487,7 +483,7 @@pascalas the combination function ). Start counting rows and columns from 0.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-primesthat 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
nextthat 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). Withtimed-prime-testincorporating 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.
-
+@@ @@ -1227,7 +1217,7 @@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.
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
termandnext. We can usesumjust as we would any procedure. For example, we can use it (along with a procedureincthat increments its argument by 1) to definesum-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-integersin terms ofsum:(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-sumin 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
- -
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)- @@ -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 argumentxthat addsxand 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
lambdais 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
lambdaexpression to specify an anonymous procedure for binding our local variables. The body offthen 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
letto make its use more convenient. Usinglet, thefprocedure 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
letexpression 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
letexpression is a list of name-expression pairs. When theletis evaluated, each name is associated with the value of the corresponding expression. The body of theletis evaluated with these names bound as local variables. The way this happens is that theletexpression 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
letexpression is simply syntactic sugar for the underlyinglambdaapplication. -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
letexpression is the body of thelet. 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 +
letallows one to bind variables as locally as possible to where they are to be used. For example, if the value ofxis 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
xin the body of theletis 3, so the value of theletexpression is 33. On the other hand, thexthat 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 ofxis 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,xwill be 3 andywill be 4 (which is the outerxplus 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 procedurefabove 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
letin situations like this and to use internaldefineonly 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
integralprocedure 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 +
searchis 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 usesearchvia the following procedure, which checks to see which of the endpoints has a negative function value and which has a positive value, and calls thesearchprocedure 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-pointthat 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
newlineanddisplayprimitives 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 primitivelogprocedure, 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
nanddare 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 procedurecont-fracsuch 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 makekin 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-fracprocedure 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.kspecifies 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-dampis 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 numberx, produces the average ofxand(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 thenewtons-methodprocedure 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
doublethat takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, ifincis 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
composefrom 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
smooththat 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-improvethat 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-nextandpi-termwithinpi-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 asmake-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
cosbutton 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
@@ 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}} -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.
+
-
-
+
+
![]()
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 integernand whose denominator is the integerd.- -(numer x) returns the numerator of the rational number x. +
(numer x)returns the numerator of the rational numberx.- -(denom x) returns the denominator of the rational number x. +
(denom x)returns the denominator of the rational numberx.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, andmake-ratshould 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,andmake-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 procedurescarandcdr. 2 Thus, we can usecons,car, andcdras 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,
conscan 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, andcdr, 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,anddenomare 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 agcdprocedure like the one in section 1.2.5 that produces the greatest common divisor of two integers, we can usegcdto 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-ratwithout changing any of the procedures (such asadd-ratandmul-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-ratthat handles both positive and negative arguments.Make-ratshould 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-ratand selectorsnumeranddenom. 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, andequal-rat?. These, in turn, are implemented solely in terms of the constructor and selectorsmake-rat,numer, anddenom, 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 ofcons,car, andcdr. 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 thegcdwhen the rational numbers are constructed. If not, we may be better off waiting until access time to compute thegcd. In any case, when we change from one representation to the other, the proceduresadd-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
gcdat 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-segmentand selectorsstart-segmentandend-segmentthat 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 constructormake-pointand selectorsx-pointandy-pointthat define this representation. Finally, using your selectors and constructors, define a proceduremidpoint-segmentthat 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, anddenom. 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
xfrom a pair of integersnandd, then extracting thenumerand thedenomofxand dividing them should yield the same result as dividingnbyd. In other words,make-rat,numer, anddenommust satisfy the condition that, for any integernand any non-zero integerd, ifxis (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, anddenommust 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, andcdrfor operating on pairs. But the only thing we need to know about these three operations is that if we glue two objects together usingconswe can retrieve the objects usingcarandcdr. That is, the operations satisfy the condition that, for any objectsxandy, ifzis(cons x y)then(car z)isxand(cdr z)isy. 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 implementcons,car, andcdrwithout 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 proceduredispatch, which takes one argument and returns eitherxorydepending on whether the argument is 0 or 1. Correspondingly,(car z)is defined to applyzto 0. Hence, ifzis the procedure formed by(cons x y), thenzapplied to 0 will yieldx. Thus, we have shown that(car (cons x y))yieldsx, as desired. Similarly,(cdr (cons x y))applies the procedure returned by(cons x y)to 1, which returnsy. Therefore, this procedural implementation of pairs is a valid implementation, and if we access pairs using onlycons,car, andcdrwe 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))yieldsxfor any objectsxandy.(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, andcdr.(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
oneandtwodirectly (not in terms ofzeroandadd-1). (Hint: Use substitution to evaluate(add-1 zero)). Give a direct definition of the addition procedure+(not in terms of repeated application ofadd-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. (
Minandmaxare 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-boundandlower-boundto 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
widthof 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-intervalinto 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-percentthat takes a center and a percentage tolerance and produces the desired interval. You must also define a selectorpercentthat produces the percentage tolerance for a given interval. Thecenterselector 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,
par2is a "better" program for parallel resistances thanpar1. Is she right? Why?-
+@@ -729,7 +725,7 @@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.)
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
consstands for "construct." The namescarandcdrderive 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.Carstands for "Contents of Address part of Register" andcdr(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-ratwith the value of the expressioncons, which is the primitive procedure that constructs pairs. Thusmake-ratandconsare 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-ratcallingcons,make-ratiscons, so there is only one procedure called, not two, whenmake-ratis called. On the other hand, doing this defeats debugging aids that trace procedure calls or put breakpoints on procedure calls: You may want to watchmake-ratbeing called, but you certainly don't want to watch every call tocons. 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
displayis the Scheme primitive for printing data. The Scheme primitivenewlinestarts a new line for printing. Neither of these procedures returns a useful value, so in the uses ofprint-ratbelow, we show only whatprint-ratprints, not what the interpreter prints as the value returned byprint-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 calledbox-and-pointer notation, each object is shown as apointerto 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) thecarof the pair and the right part containing thecdr.
-
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
conscan 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 propertyofcons. 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 createhierarchicalstructures---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. Thecarof each pair is the corresponding item in the chain, and thecdrof the pair is the next pair in the chain. Thecdrof 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 variablenil. The entire sequence is constructed by nestedconsoperations:(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 calledlistto 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 procedure1to arguments2,3, and4. -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
caras selecting the first item in the list, and ofcdras selecting the sublist consisting of all but the first item. Nested applications ofcarandcdrcan be used to extract the second, third, and subsequent items in the list.9 The constructorconsmakes 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, theempty list. The wordnilis a contraction of the Latin word nihil, which means "nothing".10List 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 procedurelist-reftakes 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 computinglist-refis the following:@@ -187,7 +186,7 @@
- -
For n = 0, list-ref should return the car of the list. +
For n = 0,
list-refshould return thecarof the list.- -
Otherwise, list-ref should return the (n - 1)st item of the cdr of the list. +
Otherwise,
list-refshould return the (n - 1)st item of thecdrof the list.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
cdrdown the whole list. To aid in this, Scheme includes a primitive predicatenull?, which tests whether its argument is the empty list. The procedurelength, 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
lengthprocedure implements a simple recursive plan. The reduction step is:@@ -231,12 +230,12 @@
- -
The length of any list is 1 plus the length of the cdr of the list. +
The
lengthof any list is 1 plus thelengthof thecdrof the list.List operations
-
- -
The length of the empty list is 0. +
The
lengthof the empty list is 0.We could also compute length in an iterative style: +
We could also compute
lengthin 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 "
consup" an answer list whilecdring down a list, as in the procedureappend, 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: +
appendis also implemented using a recursive plan. Toappendlistslist1andlist2, do the following:@@ -294,8 +293,8 @@
- -
If list1 is the empty list, then the result is just list2. +
If
list1is the empty list, then the result is justlist2.- -
Otherwise, append the cdr of list1 and list2, and cons the car of list1 onto the result: +
Otherwise,
appendthecdroflist1andlist2, andconsthecaroflist1onto the result: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-pairthat 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
reversethat 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-denominationand partly into the procedurecount-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
ccso 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
ccas 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
ccsomewhat. 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, andno-more?in terms of primitive operations on list structures. Does the order of the listcoin-valuesaffect the answer produced bycc? 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
+,*, andlisttake arbitrary numbers of arguments. One way to define such procedures is to usedefinewith 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 alistof 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
fcan 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,xwill be 1,ywill be 2, andzwill 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
gcan 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,wwill 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-paritythat 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.maptakes 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-listin terms ofmap:(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. +
mapis 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 ofscale-list, the recursive structure of the program draws attention to the element-by-element processing of the list. Definingscale-listin terms ofmapsuppresses 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,maphelps 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-listtakes a list of numbers as argument and returns a list of the squares of those numbers.