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 22 shows a standard way to visualize a pairin this case, the pair formed by (cons 1 2). In this representation, which is called boxandpointer notation , each object is shown as a pointer to a box. The box for a primitive object contains a representation of the object. For example, the box for a number contains a numeral. The box for a pair is actually a double box, the left part containing (a pointer to) the car of the pair and the right part containing the cdr.
Figure 2.2: Boxandpointer 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 22 and Exercise 23.) As a consequence, pairs provide a universal building block from which we can construct all sorts of data structures. Figure 23 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 structuresstructures 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
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 24, 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 boxandpointer diagrams as a diagonal line and in programs as the value of the variable nil. The entire sequence is constructed by nested cons operations:
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,
is equivalent to
Lisp systems conventionally print lists by printing the sequence of elements, enclosed in parentheses. Thus, the data object in Figure 24 is printed as (1 2 3 4):
Be careful not to confuse the expression (list 1 2 3 4) with the list (1 2 3 4), which is the result obtained when the expression is evaluated. Attempting to evaluate the expression (1 2 3 4) will signal an error when the interpreter tries to apply the procedure 1 to arguments 2, 3, and 4.
We can think of car as selecting the first item in the list, and of cdr as selecting the sublist consisting of all but the first item. Nested applications of car and cdr can be used to extract the second, third, and subsequent items in the list.9 The constructor cons makes a list like the original one, but with an additional item at the beginning.
The value of nil, used to terminate the chain of pairs, can be thought of as a sequence of no elements, the empty list . The word nil is a contraction of the Latin word nihil, which means "nothing".10
List operations
The use of pairs to represent sequences of elements as lists is accompanied by conventional programming techniques for manipulating lists by successively "cdring down" the lists. For example, the procedure listref 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 listref is the following:

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

Otherwise, listref should return the (n  1)st item of the cdr of the list.
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:
The length procedure implements a simple recursive plan. The reduction step is:

The length of any list is 1 plus the length of the cdr of the list.
This is applied successively until we reach the base case:

The length of the empty list is 0.
We could also compute length in an iterative style:
Another conventional programming technique is to "cons up" an answer list while cdring down a list, as in the procedure append, which takes two lists as arguments and combines their elements to make a new list:
append is also implemented using a recursive plan. To append lists list1 and list2, do the following:

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

Otherwise, append the cdr of list1 and list2, and cons the car of list1 onto the result:
Exercise 2.17: Define a procedure lastpair that returns the list that contains only the last element of a given (nonempty) list:
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.19: Consider the changecounting 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 firstdenomination and partly into the procedure countchange (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 could then call cc as follows:
To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:
Define the procedures firstdenomination, exceptfirstdenomination, and nomore? in terms of primitive operations on list structures. Does the order of the list coinvalues affect the answer produced by cc? Why or why not?
Exercise 2.20: The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dottedtail 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
the procedure f can be called with two or more arguments. If we evaluate
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
the procedure g can be called with zero or more arguments. If we evaluate
then in the body of g, w will be the list (1 2 3 4 5 6).11
Use this notation to write a procedure sameparity that takes one or more integers and returns a list of all the arguments that have the same evenodd parity as the first argument. For example,
Mapping over lists
One extremely useful operation is to apply some transformation to each element in a list and generate the list of results. For instance, the following procedure scales each number in a list by a given factor:
We can abstract this general idea and capture it as a common pattern expressed as a higherorder procedure, just as in section 1.3. The higherorder 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
Now we can give a new definition of scalelist in terms of 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 scalelist, the recursive structure of the program draws attention to the elementbyelement processing of the list. Defining scalelist 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 21, this abstraction gives us the flexibility to change the lowlevel 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 squarelist takes a list of numbers as argument and returns a list of the squares of those numbers.
Here are two different definitions of squarelist. Complete both of them by filling in the missing expressions:
Exercise 2.22: Louis Reasoner tries to rewrite the first squarelist procedure of Exercise 221 so that it evolves an iterative process:
Unfortunately, defining squarelist this way produces the answer list in the reverse order of the one desired. Why?
Louis then tries to fix his bug by interchanging the arguments to cons:
This doesn't work either. Explain.
Exercise 2.23: The procedure foreach is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, foreach just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all – foreach is used with procedures that perform an action, such as printing. For example,
should print 57, 321 and 88. The value returned by the call to foreach should be true. Give an implementation of foreach.
2.2.2 Hierarchical Structures
The representation of sequences in terms of lists generalizes naturally to represent sequences whose elements may themselves be sequences. For example, we can regard the object ((1 2) 3 4) constructed by
as a list of three items, the first of which is itself a list, (1 2). Indeed, this is suggested by the form in which the result is printed by the interpreter. Figure 25 shows the representation of this structure in terms of pairs.
Figure 2.5: Structure formed by (cons (list 1 2) (list 3 4)).
Another way to think of sequences whose elements are sequences is as trees . The elements of the sequence are the branches of the tree, and elements that are themselves sequences are subtrees. Figure 26 shows the structure in Figure 25 viewed as a tree.
Figure 2.6: The list structure in Figure 25 viewed as a tree.
Recursion is a natural tool for dealing with tree structures, since we can often reduce operations on trees to operations on their branches, which reduce in turn to operations on the branches of the branches, and so on, until we reach the leaves of the tree. As an example, compare the length procedure of section 2.2.1 with the countleaves procedure, which returns the total number of leaves of a tree:
To implement countleaves, recall the recursive plan for computing length:
 length of a list x is 1 plus length of the cdr of x.
 length of the empty list is 0.
countleaves is similar. The value for the empty list is the same:
 countleaves of the empty list is 0.
But in the reduction step, where we strip off the car of the list, we must take into account that the car may itself be a tree whose leaves we need to count. Thus, the appropriate reduction step is
 countleaves of a tree x is countleaves of the car of x plus countleaves of the cdr of x.
Finally, by taking cars we reach actual leaves, so we need another base case:
 countleaves of a leaf is 1.
To aid in writing recursive procedures on trees, Scheme provides the primitive predicate pair?, which tests whether its argument is a pair. Here is the complete procedure:13
Exercise 2.24: Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding boxandpointer structure, and the interpretation of this as a tree (as in Figure 26).
Exercise 2.25: Give combinations of cars and cdrs that will pick 7 from each of the following lists. Represent the list as l.
Exercise 2.26: Suppose we define x and y to be two lists:
What result is printed by the interpreter in response to evaluating each of the following expressions?
Exercise 2.27: Modify your reverse procedure of Exercise 218 to produce a deepreverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deepreversed as well. For example,
Exercise 2.28: Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in lefttoright order. For example,
Exercise 2.29: A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):
A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

Write the corresponding selectors leftbranch and rightbranch, which return the branches of a mobile, and branchlength and branchstructure, which return the components of a branch.
(define (leftbranch mobile) 'yourcodehere) (define (rightbranch mobile) 'yourcodehere) (define (branchlength branch) 'yourcodehere) (define (branchstructure branch) 'yourcodehere) 
Using your selectors, define a procedure totalweight that returns the total weight of a mobile.
(define (totalweight mobile) 'yourcodehere) 
A mobile is said to be balanced if the torque applied by its topleft branch is equal to that applied by its topright branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.
(define (balanced? mobile) 'yourcodehere) 
Suppose we change the representation of mobiles so that the constructors are
How much do you need to change your programs to convert to the new representation?
Mapping over trees
Just as map is a powerful abstraction for dealing with sequences, map together with recursion is a powerful abstraction for dealing with trees. For instance, the scaletree procedure, analogous to scalelist of section 2.2.1, takes as arguments a numeric factor and a tree whose leaves are numbers. It returns a tree of the same shape, where each number is multiplied by the factor. The recursive plan for scaletree is similar to the one for countleaves:
Another way to implement scaletree is to regard the tree as a sequence of subtrees and use map. We map over the sequence, scaling each subtree in turn, and return the list of results. In the base case, where the tree is a leaf, we simply multiply by the factor:
Many tree operations can be implemented by similar combinations of sequence operations and recursion.
Exercise 2.30: Define a procedure squaretree analogous to the squarelist procedure of Exercise 221. That is, squarelist should behave as follows:
Define squaretree both directly (i.e., without using any higherorder procedures) and also by using map and recursion.
Exercise 2.31: Abstract your answer to Exercise 230 to produce a procedure treemap with the property that squaretree could be defined as
Exercise 2.32: We can represent a set as a list of distinct elements, and we can represent the set of all subsets of the set as a list of lists. For example, if the set is (1 2 3), then the set of all subsets is (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)). Complete the following definition of a procedure that generates the set of subsets of a set and give a clear explanation of why it works:
2.2.3 Sequences as Conventional Interfaces
In working with compound data, we've stressed how data abstraction permits us to design programs without becoming enmeshed in the details of data representations, and how abstraction preserves for us the flexibility to experiment with alternative representations. In this section, we introduce another powerful design principle for working with data structures – the use of conventional interfaces .
In section 1.3 we saw how program abstractions, implemented as higherorder procedures, can capture common patterns in programs that deal with numerical data. Our ability to formulate analogous operations for working with compound data depends crucially on the style in which we manipulate our data structures. Consider, for example, the following procedure, analogous to the countleaves procedure of section 2.2.2, which takes a tree as argument and computes the sum of the squares of the leaves that are odd:
On the surface, this procedure is very different from the following one, which constructs a list of all the even Fibonacci numbers $\text{Fib}(k)$, where $k$ is less than or equal to a given integer $n$:
Despite the fact that these two procedures are structurally very different, a more abstract description of the two computations reveals a great deal of similarity. The first program

enumerates the leaves of a tree;

filters them, selecting the odd ones;

squares each of the selected ones; and

accumulates the results using +, starting with 0.
The second program

enumerates the integers from 0 to n;

computes the Fibonacci number for each integer;

filters them, selecting the even ones; and

accumulates the results using cons, starting with the empty list.
A signalprocessing engineer would find it natural to conceptualize these processes in terms of signals flowing through a cascade of stages, each of which implements part of the program plan, as shown in Figure 27. In sumoddsquares, we begin with an enumerator , which generates a "signal" consisting of the leaves of a given tree. This signal is passed through a filter , which eliminates all but the odd elements. The resulting signal is in turn passed through a map , which is a "transducer" that applies the square procedure to each element. The output of the map is then fed to an accumulator , which combines the elements using +, starting from an initial 0. The plan for evenfibs is analogous.
Figure 2.7: The signalflow plans for the procedures sumoddsquares (top) and evenfibs (bottom) reveal the commonality between the two programs.
Unfortunately, the two procedure definitions above fail to exhibit this signalflow structure. For instance, if we examine the sumoddsquares procedure, we find that the enumeration is implemented partly by thenull? and pair? tests and partly by the treerecursive structureof the procedure. Similarly, the accumulation is found partly in the tests and partly in the addition used in the recursion. In general, there are no distinct parts of either procedure that correspond to the elements in the signalflow description. Our two procedures decompose the computations in a different way, spreading the enumeration over the program and mingling it with the map, the filter, and the accumulation. If we could organize our programs to make the signalflow structure manifest in the procedures we write, this would increase the conceptual clarity of the resulting code.
Sequence Operations
The key to organizing programs so as to more clearly reflect the signalflow structure is to concentrate on the "signals" that flow from one stage in the process to the next. If we represent these signals as lists, then we can use list operations to implement the processing at each of the stages. For instance, we can implement the mapping stages of the signalflow diagrams using the map procedure from section 2.2.1:
Filtering a sequence to select only those elements that satisfy a given predicate is accomplished by
Accumulations can be implemented by
All that remains to implement signalflow diagrams is to enumerate the sequence of elements to be processed. For evenfibs, we need to generate the sequence of integers in a given range, which we can do as follows:
To enumerate the leaves of a tree, we can use14
Now we can reformulate sumoddsquares and evenfibs as in the signalflow diagrams. For sumoddsquares, we enumerate the sequence of leaves of the tree, filter this to keep only the odd numbers in the sequence, square each element, and sum the results:
For evenfibs, we enumerate the integers from 0 to n, generate the Fibonacci number for each of these integers, filter the resulting sequence to keep only the even elements, and accumulate the results into a list:
The value of expressing programs as sequence operations is that this helps us make program designs that are modular, that is, designs that are constructed by combining relatively independent pieces. We can encourage modular design by providing a library of standard components together with a conventional interface for connecting the components in flexible ways.
Modular construction is a powerful strategy for controlling complexity in engineering design. In real signalprocessing applications, for example, designers regularly build systems by cascading elements selected from standardized families of filters and transducers. Similarly, sequence operations provide a library of standard program elements that we can mix and match. For instance, we can reuse pieces from the sumoddsquares and evenfibs procedures in a program that constructs a list of the squares of the first $n + 1$ Fibonacci numbers:
We can rearrange the pieces and use them in computing the product of the odd integers in a sequence:
We can also formulate conventional dataprocessing applications in terms of sequence operations. Suppose we have a sequence of personnel records and we want to find the salary of the highestpaid programmer. Assume that we have a selector salary that returns the salary of a record, and a predicate programmer? that tests if a record is for a programmer. Then we can write
These examples give just a hint of the vast range of operations that can be expressed as sequence operations.15
Sequences, implemented here as lists, serve as a conventional interface that permits us to combine processing modules. Additionally, when we uniformly represent structures as sequences, we have localized the datastructure dependencies in our programs to a small number of sequence operations. By changing these, we can experiment with alternative representations of sequences, while leaving the overall design of our programs intact. We will exploit this capability in section 3.5, when we generalize the sequenceprocessing paradigm to admit infinite sequences.
Exercise 2.33: Fill in the missing expressions to complete the following definitions of some basic listmanipulation operations as accumulations:
Exercise 2.34: Evaluating a polynomial in x at a given value of x can be formulated as an accumulation. We evaluate the polynomial $$ a_n r^n + a_{n1} r^{n1} + \cdots + a_1 r + a_0 $$
using a wellknown algorithm called Horner's rule , which structures the computation as $$ (\cdots (a_n r + a_{n1}) r + \cdots + a_1) r + a_0 $$
In other words, we start with $a_n$, multiply by $x$, add $a_{n1}$, multiply by $x$, and so on, until we reach $a_0$.16
Fill in the following template to produce a procedure that evaluates a polynomial using Horner's rule. Assume that the coefficients of the polynomial are arranged in a sequence, from $a_0$ through $a_n$.
For example, to compute $1 + 3x + 5x^3 + x^5$ at $x = 2$ you would evaluate
Exercise 2.35: Redefine countleaves from section 2.2.2 as an accumulation:
Exercise 2.36: The procedure accumulaten is similar to accumulate except that it takes as its third argument a sequence of sequences, which are all assumed to have the same number of elements. It applies the designated accumulation procedure to combine all the first elements of the sequences, all the second elements of the sequences, and so on, and returns a sequence of the results. For instance, if s is a sequence containing four sequences, ((1 2 3) (4 5 6) (7 8 9) (10 11 12)), then the value of (accumulaten + 0 s) should be the sequence (22 26 30). Fill in the missing expressions in the following definition of accumulaten:
Suppose we represent vectors $v = (v_i)$ as sequences of numbers, and matrices $m = (m_{ij})$ as sequences of vectors (the rows of the matrix). For example, the matrix $$ \left[ \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 4 & 5 & 6 & 6 \\ 6 & 7 & 8 & 9 \end{array} \right] $$
is represented as the sequence ((1 2 3 4) (4 5 6 6) (6 7 8 9)). With this representation, we can use sequence operations to concisely express the basic matrix and vector operations. These operations (which are described in any book on matrix algebra) are the following:
 (dotproduct v w) returns the sum $\sum_i v_i w_i$
 (matrix*vector m w) returns the vector $t$, where $t_i = \sum_j m_{ij} v_j$
 (matrix*matrix m n) returns the matrix $p$, where $p_{ij} = \sum_k m_{ik} n_{kj}$
 (transpose m) returns the matrix $n$, where $n_{ij} = m_{ji}$
We can define the dot product as17
Fill in the missing expressions in the following procedures for computing the other matrix operations. (The procedure accumulaten is defined in Exercise 236.)
Exercise 2.38: The accumulate procedure is also known as foldright, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a foldleft, which is similar to foldright, except that it combines elements working in the opposite direction:
What are the values of
Give a property that op should satisfy to guarantee that foldright and foldleft will produce the same values for any sequence.
Exercise 2.39: Complete the following definitions of reverse (Exercise 218) in terms of foldright and foldleft from Exercise 238:
Nested Mappings
We can extend the sequence paradigm to include many computations that are commonly expressed using nested loops.18 Consider this problem: Given a positive integer n, find all ordered pairs of distinct positive integers i and j, where 1 <= j< i<= n, such that i + j is prime. For example, if n is 6, then the pairs are the following: \begin{array}{cccc} i & 2 & 3 & 4 & 4 & 5 & 6 & 6 \\ j & 1 & 2 & 1 & 3 & 2 & 1 & 5 \\ \hline \\ i+j & 3 & 5 & 5 & 7 & 7 & 7 & 11 \end{array}
A natural way to organize this computation is to generate the sequence of all ordered pairs of positive integers less than or equal to $n$, filter to select those pairs whose sum is prime, and then, for each pair $(i, j)$ that passes through the filter, produce the triple $(i,j,i+j)$.
Here is a way to generate the sequence of pairs: For each integer $i <= n$, enumerate the integers $j<i$, and for each such $i$ and $j$ generate the pair $(i,j)$. In terms of sequence operations, we map along the sequence (enumerateinterval 1 n). For each $i$ in this sequence, we map along the sequence (enumerateinterval 1 ( i 1)). For each $j$ in this latter sequence, we generate the pair (list i j). This gives us a sequence of pairs for each $i$. Combining all the sequences for all the $i$ (by accumulating with append) produces the required sequence of pairs:19
The combination of mapping and accumulating with append is so common in this sort of program that we will isolate it as a separate procedure:
Now filter this sequence of pairs to find those whose sum is prime. The filter predicate is called for each element of the sequence; its argument is a pair and it must extract the integers from the pair. Thus, the predicate to apply to each element in the sequence is
Finally, generate the sequence of results by mapping over the filtered pairs using the following procedure, which constructs a triple consisting of the two elements of the pair along with their sum:
Combining all these steps yields the complete procedure:
Nested mappings are also useful for sequences other than those that enumerate intervals. Suppose we wish to generate all the permutations of a set $S$; that is, all the ways of ordering the items in the set. For instance, the permutations of $\{1,2,3\}$ are $\{1,2,3\}$, $\{1,3,2\}$, $\{2,1,3\}$, $\{2,3,1\}$, $\{3,1,2\}$, and $\{3,2,1\}$. Here is a plan for generating the permutations of $S$: For each item $x$ in $S$, recursively generate the sequence of permutations of $S  x$,20 and adjoin $x$ to the front of each one. This yields, for each x in S, the sequence of permutations of S that begin with $x$. Combining these sequences for all $x$ gives all the permutations of $S$:21
Notice how this strategy reduces the problem of generating permutations of S to the problem of generating the permutations of sets with fewer elements than S. In the terminal case, we work our way down to the empty list, which represents a set of no elements. For this, we generate (list nil), which is a sequence with one item, namely the set with no elements. The remove procedure used in permutations returns all the items in a given sequence except for a given item. This can be expressed as a simple filter:
Exercise 2.40: Define a procedure uniquepairs that, given an integer $n$, generates the sequence of pairs $(i,j)$ with $1 ≤ j< i ≤ n$. Use uniquepairs to simplify the definition of primesumpairs given above.
Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers $i$, $j$, and $k$ less than or equal to a given integer $n$ that sum to a given integer $s$.
Exercise 2.42: The "eightqueens puzzle" asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal).
Figure 2.8: A solution to the eightqueens puzzle.
One possible solution is shown in Figure 28. One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed $k  1$ queens, we must place the $k^{th}$ queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to place $k  1$ queens in the first $k  1$ columns of the board. For each of these ways, generate an extended set of positions by placing a queen in each row of the kth column. Now filter these, keeping only the positions for which the queen in the $k^{th}$ column is safe with respect to the other queens. This produces the sequence of all ways to place $k$ queens in the first $k$ columns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle.
We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on an $n \times n$ chessboard. queens has an internal procedure queencols that returns the sequence of all ways to place queens in the first k columns of the board.
In this procedure restofqueens is a way to place $k  1$ queens in the first $k  1$ columns, and newrow is a proposed row in which to place the queen for the $k^{th}$ column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoinposition, which adjoins a new rowcolumn position to a set of positions, and emptyboard, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in the kth column is safe with respect to the others. (Note that we need only check whether the new queen is safethe other queens are already guaranteed safe with respect to each other.)
Exercise 2.43: Louis Reasoner is having a terrible time doing Exercise 242. His queens procedure seems to work, but it runs extremely slowly. (Louis never does manage to wait long enough for it to solve even the $6 \times 6$ case.) When Louis asks Eva Lu Ator for help, she points out that he has interchanged the order of the nested mappings in the flatmap, writing it as
Explain why this interchange makes the program run slowly. Estimate how long it will take Louis's program to solve the eightqueens puzzle, assuming that the program in Exercise 242 solves the puzzle in time T.
2.2.4 Example: A Picture Language
TODO: eventually I hope to implement this picture language in javascript. However it is not done yet.
This section presents a simple language for drawing pictures that illustrates the power of data abstraction and closure, and also exploits higherorder procedures in an essential way. The language is designed to make it easy to experiment with patterns such as the ones in Figure 29, which are composed of repeated elements that are shifted and scaled.22 In this language, the data objects being combined are represented as procedures rather than as list structure. Just as cons, which satisfies the closure property, allowed us to easily build arbitrarily complicated list structure, the operations in this language, which also satisfy the closure property, allow us to easily build arbitrarily complicated patterns.
Figure 2.9: Designs generated with the picture language.
The picture language
When we began our study of programming in section 1.1, we emphasized the importance of describing a language by focusing on the language's primitives, its means of combination, and its means of abstraction. We'll follow that framework here.
Part of the elegance of this picture language is that there is only one kind of element, called a painter . A painter draws an image that is shifted and scaled to fit within a designated parallelogramshaped frame. For example, there's a primitive painter we'll call wave that makes a crude line drawing, as shown in Figure 210. The actual shape of the drawing depends on the frameall four images in Figure 210 are produced by the same wave painter, but with respect to four different frames. Painters can be more elaborate than this: The primitive painter called rogers paints a picture of MIT's founder, William Barton Rogers, as shown in Figure 211.23 The four images in Figure 211 are drawn with respect to the same four frames as the wave images in Figure 210.
To combine images, we use various operations that construct new painters from given painters. For example, the beside operation takes two painters and produces a new, compound painter that draws the first painter's image in the left half of the frame and the second painter's image in the right half of the frame. Similarly, below takes two painters and produces a compound painter that draws the first painter's image below the second painter's image. Some operations transform a single painter to produce a new painter. For example, flipvert takes a painter and produces a painter that draws its image upsidedown, and fliphoriz produces a painter that draws the original painter's image lefttoright reversed.
Figure 2.10: Images produced by the wave painter, with respect to four different frames. The frames, shown with dotted lines, are not part of the images.
Figure 2.11: Images of William Barton Rogers, founder and first president of MIT, painted with respect to the same four frames as in Figure 210 (original image reprinted with the permission of the MIT Museum).
Figure 212 shows the drawing of a painter called wave4 that is built up in two stages starting from wave:
Figure 2.12: Creating a complex figure, starting from the wave painter of Figure 210.
In building up a complex image in this manner we are exploiting the fact that painters are closed under the language's means of combination. The beside or below of two painters is itself a painter; therefore, we can use it as an element in making more complex painters. As with building up list structure using cons, the closure of our data under the means of combination is crucial to the ability to create complex structures while using only a few operations.
Once we can combine painters, we would like to be able to abstract typical patterns of combining painters. We will implement the painter operations as Scheme procedures. This means that we don't need a special abstraction mechanism in the picture language: Since the means of combination are ordinary Scheme procedures, we automatically have the capability to do anything with painter operations that we can do with procedures. For example, we can abstract the pattern in wave4 as
and define wave4 as an instance of this pattern:
We can also define recursive operations. Here's one that makes painters split and branch towards the right as shown in figures Figure 213 and Figure 214:
Figure 2.13: Recursive plans for rightsplit and cornersplit.
We can produce balanced patterns by branching upwards as well as towards the right (see Exercise 244 and figures Figure 213 and Figure 214):
Figure 2.14: The recursive operations rightsplit and cornersplit applied to the painters wave and rogers. Combining four cornersplit figures produces symmetric squarelimit designs as shown in Figure 29.
By placing four copies of a cornersplit appropriately, we obtain a pattern called squarelimit, whose application to wave and rogers is shown in Figure 29:
Exercise 2.44: Define the procedure upsplit used by cornersplit. It is similar to rightsplit, except that it switches the roles of below and beside.
Higherorder operations
In addition to abstracting patterns of combining painters, we can work at a higher level, abstracting patterns of combining painter operations. That is, we can view the painter operations as elements to manipulate and can write means of combination for these elementsprocedures that take painter operations as arguments and create new painter operations.
For example, flippedpairs and squarelimit each arrange four copies of a painter's image in a square pattern; they differ only in how they orient the copies. One way to abstract this pattern of painter combination is with the following procedure, which takes four oneargument painter operations and produces a painter operation that transforms a given painter with those four operations and arranges the results in a square. Tl, tr, bl, and br are the transformations to apply to the top left copy, the top right copy, the bottom left copy, and the bottom right copy, respectively.
Then flippedpairs can be defined in terms of squareoffour as follows:24
and squarelimit can be expressed as25
Exercise 2.45: rightsplit and upsplit can be expressed as instances of a general splitting operation. Define a procedure split with the property that evaluating
produces procedures rightsplit and upsplit with the same behaviors as the ones already defined.
Frames
Before we can show how to implement painters and their means of combination, we must first consider frames. A frame can be described by three vectorsan origin vector and two edge vectors. The origin vector specifies the offset of the frame's origin from some absolute origin in the plane, and the edge vectors specify the offsets of the frame's corners from its origin. If the edges are perpendicular, the frame will be rectangular. Otherwise the frame will be a more general parallelogram.
Figure 215 shows a frame and its associated vectors. In accordance with data abstraction, we need not be specific yet about how frames are represented, other than to say that there is a constructor makeframe, which takes three vectors and produces a frame, and three corresponding selectors originframe, edge1frame, and edge2frame (see Exercise 247).
Figure 2.15: A frame is described by three vectors  an origin and two edges.
We will use coordinates in the unit square $(0 ≤ x,y ≤ 1)$ to specify images. With each frame, we associate a frame coordinate map , which will be used to shift and scale images to fit the frame. The map transforms the unit square into the frame by mapping the vector v = (x,y) to the vector sum $$ \text{Origin}(\text{Frame}) + x \cdot \text{Edge}_1(\text{Frame}) + y \cdot \text{Edge}_2(\text{Frame}) $$
For example, (0,0) is mapped to the origin of the frame, (1,1) to the vertex diagonally opposite the origin, and (0.5,0.5) to the center of the frame. We can create a frame's coordinate map with the following procedure:26
Observe that applying framecoordmap to a frame returns a procedure that, given a vector, returns a vector. If the argument vector is in the unit square, the result vector will be in the frame. For example,
returns the same vector as
Exercise 2.46: A twodimensional vector v running from the origin to a point can be represented as a pair consisting of an xcoordinate and a ycoordinate. Implement a data abstraction for vectors by giving a constructor makevect and corresponding selectors xcorvect and ycorvect. In terms of your selectors and constructor, implement procedures addvect, subvect, and scalevect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar: \begin{align} (x_1, y_1) + (x_2, y_2) &= (x_1 + x_2, y_1 + y_2) \\ (x_1, y_1)  (x_2, y_2) &= (x_1  x_2, y_1  y_2) \\ s \cdot (x, y) &= (sx, sy) \end{align}
Exercise 2.47: Here are two possible constructors for frames:
For each constructor supply the appropriate selectors to produce an implementation for frames.
Painters
A painter is represented as a procedure that, given a frame as argument, draws a particular image shifted and scaled to fit the frame. That is to say, if p is a painter and f is a frame, then we produce p's image in f by calling p with f as argument.
The details of how primitive painters are implemented depend on the particular characteristics of the graphics system and the type of image to be drawn. For instance, suppose we have a procedure drawline that draws a line on the screen between two specified points. Then we can create painters for line drawings, such as the wave painter in Figure 210, from lists of line segments as follows:27
The segments are given using coordinates with respect to the unit square. For each segment in the list, the painter transforms the segment endpoints with the frame coordinate map and draws a line between the transformed points.
Representing painters as procedures erects a powerful abstraction barrier in the picture language. We can create and intermix all sorts of primitive painters, based on a variety of graphics capabilities. The details of their implementation do not matter. Any procedure can serve as a painter, provided that it takes a frame as argument and draws something scaled to fit the frame.28
Exercise 2.48: A directed line segment in the plane can be represented as a pair of vectorsthe vector running from the origin to the startpoint of the segment, and the vector running from the origin to the endpoint of the segment. Use your vector representation from Exercise 246 to define a representation for segments with a constructor makesegment and selectors startsegment and endsegment.
Exercise 2.49: Use segments>painter to define the following primitive painters:
 The painter that draws the outline of the designated frame.
 The painter that draws an "X" by connecting opposite corners of the frame.
 The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.
 The wave painter.
Transforming and combining painters
An operation on painters (such as flipvert or beside) works by creating a painter that invokes the original painters with respect to frames derived from the argument frame. Thus, for example, flipvert doesn't have to know how a painter works in order to flip itit just has to know how to turn a frame upside down: The flipped painter just uses the original painter, but in the inverted frame.
Painter operations are based on the procedure transformpainter, which takes as arguments a painter and information on how to transform a frame and produces a new painter. The transformed painter, when called on a frame, transforms the frame and calls the original painter on the transformed frame. The arguments to transformpainter are points (represented as vectors) that specify the corners of the new frame: When mapped into the frame, the first point specifies the new frame's origin and the other two specify the ends of its edge vectors. Thus, arguments within the unit square specify a frame contained within the original frame.
Here's how to flip painter images vertically:
Using transformpainter, we can easily define new transformations. For example, we can define a painter that shrinks its image to the upperright quarter of the frame it is given:
Other transformations rotate images counterclockwise by 90 degrees29
or squash images towards the center of the frame:30
Frame transformation is also the key to defining means of combining two or more painters. The beside procedure, for example, takes two painters, transforms them to paint in the left and right halves of an argument frame respectively, and produces a new, compound painter. When the compound painter is given a frame, it calls the first transformed painter to paint in the left half of the frame and calls the second transformed painter to paint in the right half of the frame:
Observe how the painter data abstraction, and in particular the representation of painters as procedures, makes beside easy to implement. The beside procedure need not know anything about the details of the component painters other than that each painter will draw something in its designated frame.
Exercise 2.50: Define the transformation fliphoriz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees.
Exercise 2.51: Define the below operation for painters. Below takes two painters as arguments. The resulting painter, given a frame, draws with the first painter in the bottom of the frame and with the second painter in the top. Define below in two different waysfirst by writing a procedure that is analogous to the beside procedure given above, and again in terms of beside and suitable rotation operations (from Exercise 250).
Levels of language for robust design
The picture language exercises some of the critical ideas we've introduced about abstraction with procedures and data. The fundamental data abstractions, painters, are implemented using procedural representations, which enables the language to handle different basic drawing capabilities in a uniform way. The means of combination satisfy the closure property, which permits us to easily build up complex designs. Finally, all the tools for abstracting procedures are available to us for abstracting means of combination for painters.
We have also obtained a glimpse of another crucial idea about languages and program design. This is the approach of stratified design , the notion that a complex system should be structured as a sequence of levels that are described using a sequence of languages. Each level is constructed by combining parts that are regarded as primitive at that level, and the parts constructed at each level are used as primitives at the next level. The language used at each level of a stratified design has primitives, means of combination, and means of abstraction appropriate to that level of detail.
Stratified design pervades the engineering of complex systems. For example, in computer engineering, resistors and transistors are combined (and described using a language of analog circuits) to produce parts such as andgates and orgates, which form the primitives of a language for digitalcircuit design.31 These parts are combined to build processors, bus structures, and memory systems, which are in turn combined to form computers, using languages appropriate to computer architecture. Computers are combined to form distributed systems, using languages appropriate for describing network interconnections, and so on.
As a tiny example of stratification, our picture language uses primitive elements (primitive painters) that are created using a language that specifies points and lines to provide the lists of line segments for segments>painter, or the shading details for a painter like rogers. The bulk of our description of the picture language focused on combining these primitives, using geometric combiners such as beside and below. We also worked at a higher level, regarding beside and below as primitives to be manipulated in a language whose operations, such as squareoffour, capture common patterns of combining geometric combiners.
Stratified design helps make programs robust , that is, it makes it likely that small changes in a specification will require correspondingly small changes in the program. For instance, suppose we wanted to change the image based on wave shown in Figure 29. We could work at the lowest level to change the detailed appearance of the wave element; we could work at the middle level to change the way cornersplit replicates the wave; we could work at the highest level to change how squarelimit arranges the four copies of the corner. In general, each level of a stratified design provides a different vocabulary for expressing the characteristics of the system, and a different kind of ability to change it.
Exercise 2.52: Make changes to the square limit of wave shown in Figure 29 by working at each of the levels described above. In particular:
 Add some segments to the primitive wave painter of Exercise 249 (to add a smile, for example).
 Change the pattern constructed by cornersplit (for example, by using only one copy of the upsplit and rightsplit images instead of two).
 Modify the version of squarelimit that uses squareoffour so as to assemble the corners in a different pattern. (For example, you might make the big Mr. Rogers look outward from each corner of the square.)
Notes
6 The use of the word "closure" here comes from abstract algebra, where a set of elements is said to be closed under an operation if applying the operation to elements in the set produces an element that is again an element of the set. The Lisp community also (unfortunately) uses the word "closure" to describe a totally unrelated concept: A closure is an implementation technique for representing procedures with free variables. We do not use the word "closure" in this second sense in this book.
7 The notion that a means of combination should satisfy closure is a straightforward idea. Unfortunately, the data combiners provided in many popular programming languages do not satisfy closure, or make closure cumbersome to exploit. In Fortran or Basic, one typically combines data elements by assembling them into arraysbut one cannot form arrays whose elements are themselves arrays. Pascal and C admit structures whose elements are structures. However, this requires that the programmer manipulate pointers explicitly, and adhere to the restriction that each field of a structure can contain only elements of a prespecified form. Unlike Lisp with its pairs, these languages have no builtin generalpurpose glue that makes it easy to manipulate compound data in a uniform way. This limitation lies behind Alan Perlis's comment in his foreword to this book: "In Pascal the plethora of declarable data structures induces a specialization within functions that inhibits and penalizes casual cooperation. It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures."
8 In this book, we use list to mean a chain of pairs terminated by the endoflist marker. In contrast, the term list structure refers to any data structure made out of pairs, not just to lists.
9 Since nested applications of car and cdr are cumbersome to write, Lisp dialects provide abbreviations for them  for instance,
(cadr <arg>) = (car (cdr <arg>))
The names of all such procedures start with c and end with r. Each a between them stands for a car operation and each d for a cdr operation, to be applied in the same order in which they appear in the name. The names car and cdr persist because simple combinations like cadr are pronounceable.
10 It's remarkable how much energy in the standardization of Lisp dialects has been dissipated in arguments that are literally over nothing: Should nil be an ordinary name? Should the value of nil be a symbol? Should it be a list? Should it be a pair? In Scheme, nil is an ordinary name, which we use in this section as a variable whose value is the endoflist marker (just as true is an ordinary variable that has a true value). Other dialects of Lisp, including Common Lisp, treat nil as a special symbol. The authors of this book, who have endured too many language standardization brawls, would like to avoid the entire issue. Once we have introduced quotation in section 2.3, we will denote the empty list as '() and dispense with the variable nil entirely.
11 To define f and g using lambda we would write
12 Scheme standardly provides a map procedure that is more general than the one described here. This more general map takes a procedure of n arguments, together with n lists, and applies the procedure to all the first elements of the lists, all the second elements of the lists, and so on, returning a list of the results. For example:
13 The order of the first two clauses in the cond matters, since the empty list satisfies null? and also is not a pair.
14 This is, in fact, precisely the fringe procedure from exercise 2.28. Here we've renamed it to emphasize that it is part of a family of general sequencemanipulation procedures.
15 Richard Waters (1979) developed a program that automatically analyzes traditional Fortran programs, viewing them in terms of maps, filters, and accumulations. He found that fully 90 percent of the code in the Fortran Scientific Subroutine Package fits neatly into this paradigm. One of the reasons for the success of Lisp as a programming language is that lists provide a standard medium for expressing ordered collections so that they can be manipulated using higherorder operations. The programming language APL owes much of its power and appeal to a similar choice. In APL all data are represented as arrays, and there is a universal and convenient set of generic operators for all sorts of array operations.
16 According to Knuth (1981), this rule was formulated by W. G. Horner early in the nineteenth century, but the method was actually used by Newton over a hundred years earlier. Horner's rule evaluates the polynomial using fewer additions and multiplications than does the straightforward method of first computing an xn, then adding an1xn1, and so on. In fact, it is possible to prove that any algorithm for evaluating arbitrary polynomials must use at least as many additions and multiplications as does Horner's rule, and thus Horner's rule is an optimal algorithm for polynomial evaluation. This was proved (for the number of additions) by A. M. Ostrowski in a 1954 paper that essentially founded the modern study of optimal algorithms. The analogous statement for multiplications was proved by V. Y. Pan in 1966. The book by Borodin and Munro (1975) provides an overview of these and other results about optimal algorithms.
17 This definition uses the extended version of map described in footnote 12.
18 This approach to nested mappings was shown to us by David Turner, whose languages KRC and Miranda provide elegant formalisms for dealing with these constructs. The examples in this section (see also exercise 2.42) are adapted from Turner 1981. In section 3.5.3, we'll see how this approach generalizes to infinite sequences.
19 We're representing a pair here as a list of two elements rather than as a Lisp pair. Thus, the "pair" (i,j) is represented as (list i j), not (cons i j).
20 The set S  x is the set of all elements of S, excluding x.
21 Semicolons in Scheme code are used to introduce comments. Everything from the semicolon to the end of the line is ignored by the interpreter. In this book we don't use many comments; we try to make our programs selfdocumenting by using descriptive names.
22 The picture language is based on the language Peter Henderson created to construct images like M.C. Escher's "Square Limit" woodcut (see Henderson 1982). The woodcut incorporates a repeated scaled pattern, similar to the arrangements drawn using the squarelimit procedure in this section.
23 William Barton Rogers (18041882) was the founder and first president of MIT. A geologist and talented teacher, he taught at William and Mary College and at the University of Virginia. In 1859 he moved to Boston, where he had more time for research, worked on a plan for establishing a "polytechnic institute", and served as Massachusetts's first State Inspector of Gas Meters.
When MIT was established in 1861, Rogers was elected its first president. Rogers espoused an ideal of "useful learning" that was different from the university education of the time, with its overemphasis on the classics, which, as he wrote, "stand in the way of the broader, higher and more practical instruction and discipline of the natural and social sciences." This education was likewise to be different from narrow tradeschool education. In Rogers's words:
The worldenforced distinction between the practical and the scientific worker is utterly futile, and the whole experience of modern times has demonstrated its utter worthlessness.
Rogers served as president of MIT until 1870, when he resigned due to ill health. In 1878 the second president of MIT, John Runkle, resigned under the pressure of a financial crisis brought on by the Panic of 1873 and strain of fighting off attempts by Harvard to take over MIT. Rogers returned to hold the office of president until 1881.
Rogers collapsed and died while addressing MIT's graduating class at the commencement exercises of 1882. Runkle quoted Rogers's last words in a memorial address delivered that same year:
As I stand here today and see what the Institute is, ... I call to mind the beginnings of science. I remember one hundred and fifty years ago Stephen Hales published a pamphlet on the subject of illuminating gas, in which he stated that his researches had demonstrated that 128 grains of bituminous coal  "Bituminous coal," these were his last words on earth. Here he bent forward, as if consulting some notes on the table before him, then slowly regaining an erect position, threw up his hands, and was translated from the scene of his earthly labors and triumphs to "the tomorrow of death", where the mysteries of life are solved, and the disembodied spirit finds unending satisfaction in contemplating the new and still unfathomable mysteries of the infinite future.
In the words of Francis A. Walker (MIT's third president):
All his life he had borne himself most faithfully and heroically, and he died as so good a knight would surely have wished, in harness, at his post, and in the very part and act of public duty.
24 Equivalently, we could write
(define flippedpairs (squareoffour identity flipvert identity flipvert))
25 Rotate180 rotates a painter by 180 degrees (see exercise 2.50). Instead of rotate180 we could say (compose flipvert fliphoriz), using the compose procedure from exercise 1.42.
26 Framecoordmap uses the vector operations described in exercise 2.46 below, which we assume have been implemented using some representation for vectors. Because of data abstraction, it doesn't matter what this vector representation is, so long as the vector operations behave correctly.
27 Segments>painter uses the representation for line segments described in exercise 2.48 below. It also uses the foreach procedure described in exercise 2.23.
28 For example, the rogers painter of figure 2.11 was constructed from a graylevel image. For each point in a given frame, the rogers painter determines the point in the image that is mapped to it under the frame coordinate map, and shades it accordingly. By allowing different types of painters, we are capitalizing on the abstract data idea discussed in section 2.1.3, where we argued that a rationalnumber representation could be anything at all that satisfies an appropriate condition. Here we're using the fact that a painter can be implemented in any way at all, so long as it draws something in the designated frame. Section 2.1.3 also showed how pairs could be implemented as procedures. Painters are our second example of a procedural representation for data.
29 Rotate90 is a pure rotation only for square frames, because it also stretches and shrinks the image to fit into the rotated frame.
30 The diamondshaped images in figures 2.10 and 2.11 were created with squashinwards applied to wave and rogers.
Based on Structure and Interpretation of Computer Programs, a work at https://mitpress.mit.edu/sicp/.