|
3 | 3 | Foreword |
4 | 4 | </NAME> |
5 | 5 |
|
6 | | - <TEXT> |
7 | | - in preparation |
8 | | - </TEXT> |
| 6 | + <TEXT> |
| 7 | + I had the pleasure of meeting the amazing Alan Perlis and talking with |
| 8 | + him a few times, when I was still a student. Following in his |
| 9 | + footsteps is a daunting task, even though he blazed an excellent |
| 10 | + trail. Still, I would like to reexamine one comment he made in the |
| 11 | + original foreword to this book (and, please, you may wish to read his |
| 12 | + foreword before you finish this one). Is it really true that it is |
| 13 | + better to have 100 functions operate on one data structure than to |
| 14 | + have 10 functions operate on 10 data structures? |
| 15 | + </TEXT> |
| 16 | + <TEXT> |
| 17 | + To answer that question carefully, we first need to ask whether |
| 18 | + that one data structure is <QUOTE>universal</QUOTE>: can it conveniently |
| 19 | + fulfill the roles of those 10 more specialized data structures? |
| 20 | + </TEXT> |
| 21 | + <TEXT> |
| 22 | + For that matter, we can also ask: do we really need 100 |
| 23 | + functions? Is there a single universal function that can fulfill |
| 24 | + the roles of all those other functions? |
| 25 | + </TEXT> |
| 26 | + <TEXT> |
| 27 | + The surprising answer to that last question is <QUOTE>yes</QUOTE>; it is |
| 28 | + only slightly tricky to construct a function that accepts (1) a |
| 29 | + data structure that serves as a description of some other |
| 30 | + function, and (2) a list of arguments, and behaves exactly as |
| 31 | + that other function would when applied to the given |
| 32 | + arguments. And it is only slightly tricky to design a data |
| 33 | + structure capable of describing any computation whatsoever. One |
| 34 | + such data structure (the tagged-list representation of |
| 35 | + expressions and statements, paired with environments that |
| 36 | + associate names with values) and one such universal function |
| 37 | + (<JAVASCRIPTINLINE>apply</JAVASCRIPTINLINE>) are described in |
| 38 | + Chapter 4 of this book. So maybe we need only one function and |
| 39 | + one data structure. |
| 40 | + </TEXT> |
| 41 | + <TEXT> |
| 42 | + That is true in theory. In practice, we find it convenient to |
| 43 | + draw distinctions that help us, as human beings constructing |
| 44 | + descriptions of computations, to organize the structure of our |
| 45 | + code so that we can better understand them. I believe that |
| 46 | + Perlis was making a remark not about computational capability, |
| 47 | + but about human abilities and human limitations. |
| 48 | + </TEXT> |
| 49 | + <TEXT> |
| 50 | + One thing the human mind seems to do well is to name things; we have |
| 51 | + powerful associative memories. Given a name, we can quickly recall |
| 52 | + some associated thing to mind. This is why we typically find it easier |
| 53 | + to work with the lambda calculus than the combinatory calculus; it is |
| 54 | + much easier for most people to interpret the Lisp expression |
| 55 | + <SCHEMEINLINE>(lambda (x) (lambda (y) (+ x y)))</SCHEMEINLINE> |
| 56 | + or the JavaScript expression |
| 57 | + <JAVASCRIPTINLINE>x => y => x + y</JAVASCRIPTINLINE> |
| 58 | + than the combinatory expression |
| 59 | + <BLOCKQUOTE> |
| 60 | +((S ((S (K S)) ((S ((S (K S)) ((S (K K)) (K +)))) ((S (K K)) I)))) (K I)) |
| 61 | + </BLOCKQUOTE> |
| 62 | + even though there is a |
| 63 | + direct structural correspondence, easily expressed in five lines of |
| 64 | + Lisp code. |
| 65 | + </TEXT> |
| 66 | + <TEXT> |
| 67 | + So while in principle we could get by with just one universal |
| 68 | + function, we prefer to modularize our code, to give names to the |
| 69 | + various pieces, and to mention the names of function |
| 70 | + descriptions rather than constantly feeding the descriptions |
| 71 | + themselves to the universal function. |
| 72 | + </TEXT> |
| 73 | + <TEXT> |
| 74 | + In my 1998 talk <QUOTE>Growing a Language,</QUOTE> I commented |
| 75 | + that a good programmer <QUOTE>does not just write programs. A |
| 76 | + good programmer builds a working vocabulary.</QUOTE> As we |
| 77 | + design and define more and more parts of our programs, we give |
| 78 | + names to those parts, and the result is that we have a richer |
| 79 | + language in which to write the rest. |
| 80 | + </TEXT> |
| 81 | + <TEXT> |
| 82 | + But we also find it natural to draw distinctions among data |
| 83 | + structures, and to give them names. |
| 84 | + </TEXT> |
| 85 | + <TEXT> |
| 86 | + It may be that nested lists are a universal data structure (and |
| 87 | + it is worth noting that many modern and widely used data |
| 88 | + structures, such as HTML and XML and JSON, are also |
| 89 | + parenthetically nested representations, only slightly more |
| 90 | + elaborate than Lisp<APOS/>s bare parentheses). There are also many |
| 91 | + functions, such as finding the length of a list or applying a |
| 92 | + function to every element of a list and getting back a list of |
| 93 | + the results, that are useful in a wide variety of situations. |
| 94 | + And yet, when I am thinking about a specific computation, I |
| 95 | + often say to myself, <QUOTE>This list of two things I expect to be a |
| 96 | + personal name and a surname, but that list of two things I |
| 97 | + expect to be the real and imaginary parts of a complex number, |
| 98 | + and that other list of two things I will regard as the numerator |
| 99 | + and denominator of a fraction.</QUOTE> In other words, I draw |
| 100 | + distinctions<EMDASH/>and it may be useful to represent those |
| 101 | + distinctions explicitly in the data structure, in part to |
| 102 | + prevent mistakes such as accidentally treating a complex number |
| 103 | + as a fraction. (Again, this is a comment about human abilities |
| 104 | + and human limitations.) |
| 105 | + </TEXT> |
| 106 | + <TEXT> |
| 107 | + So maybe the truth is somewhere in between the extremes that |
| 108 | + Perlis so eloquently posited. Maybe the sweet spot is something |
| 109 | + more like 40 functions general enough to operate usefully on a |
| 110 | + universal data structure such as lists, but also 10 sets of 6 |
| 111 | + functions each that are relevant when we take one of 10 |
| 112 | + specialized views of that universal data structure. This is |
| 113 | + manageable if we give good names to these functions and |
| 114 | + specialized views. |
| 115 | + </TEXT> |
| 116 | + <TEXT> |
| 117 | + As you read this book, please pay attention not only to the |
| 118 | + programming language constructs and how they are used, but also |
| 119 | + to the <EM>names</EM> given to functions and variables and data |
| 120 | + structures. They have been chosen in a deliberate and systematic |
| 121 | + way to enhance your understanding of the overall program |
| 122 | + structure. |
| 123 | + </TEXT> |
| 124 | + <TEXT> |
| 125 | + Primitives, means of combination, abstraction, naming, and |
| 126 | + conventions for using a universal data structure in specialized |
| 127 | + ways by drawing distinctions: these are the fundamental building |
| 128 | + blocks of a good programming language. From there, imagination |
| 129 | + and good engineering judgment based on experience can do the |
| 130 | + rest. |
| 131 | + </TEXT> |
9 | 132 |
|
10 | 133 | <SIGNATURE> |
11 | 134 | <ATTRIBUTION> |
|
0 commit comments