1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
<HTML>
<HEAD>
<TITLE>Simply Scheme part III introduction</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE> 2/e Copyright (C) 1999 MIT
<H1>Functions as Data</H1>
<TABLE><TR><TD>
<P><IMG SRC="simply.jpg" ALT="cover photo">
<TD valign="center">
<CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR><A HREF="http://www.cnmat.berkeley.edu/~matt">Matthew
Wright</A><BR>University of California, Berkeley</CITE>
<BR><BR><A HREF="http://www-mitpress.mit.edu/book-home.tcl?isbn=0262082810">MIT
Press web page for Simply Scheme</A>
</TABLE>
<P><A HREF="simply-toc.html">(back to Table of Contents)</A>
<HR>
<P>By now you're accustomed to the idea of expressing a computational process
in terms of the function whose value you want to compute, rather than in
terms of a sequence of actions. But you probably think of a function (or
the procedure that embodies it) as something very different from the words,
sentences, numbers, or other data that serve as arguments to the functions.
It's like the distinction between verbs and nouns in English: A verb
represents something <EM>to do,</EM> while a noun represents something
<EM>that is.</EM>
<P>In this part of the book our goal is to overturn that distinction.
<P>Like many big ideas, this one seems simple at first. All we're saying is
that a function can have <EM>functions</EM> as its domain or range. One
artificially simple example that you've seen earlier was the
<CODE>number-of-arguments</CODE> function in Chapter 2. That function
takes a function as argument and returns a number. It's not so different
from <CODE>count</CODE>, which takes a word or sentence as argument and returns a
number.
<P>But you'll see that this idea leads to an enormous rise in the length and
complexity of the processes you can express in a short procedure, because
now a process can give rise to several other processes. A typical example
is the <CODE>acronym</CODE> procedure that we introduced in Chapter 1 and
will examine now in more detail. Instead of applying the <CODE>first</CODE>
procedure to a single word, we use <CODE>first</CODE> as an argument to a procedure,
<CODE>every</CODE>, that automatically applies it to every word of a sentence. A
single <CODE>every</CODE> process gives rise to several <CODE>first</CODE> processes.
<P>The same idea of function as data allows us to write procedures that create
and return new procedures. At the beginning of Part II we showed a Scheme
representation of a function that computes the third person singular of a
verb. Now, to illustrate the idea of function as data, we'll show how to
represent in Scheme a function <CODE>make-conjugator</CODE> whose range is <EM>the
whole family</EM> of verb-conjugation functions:
<PRE>
(define (make-conjugator prefix ending)
(lambda (verb) (sentence prefix (word verb ending))))
</PRE>
<P>Never mind the notation for now; the idea to think about is that
we can use <CODE>make-conjugator</CODE> to create many functions similar to the
<CODE>third-person</CODE> example of the Part II introduction:
<PRE>
> (define third-person (make-conjugator 'she 's))
> (third-person 'program)
(SHE PROGRAMS)
> (define third-person-plural-past (make-conjugator 'they 'ed))
> (third-person-plural-past 'play)
(THEY PLAYED)
> (define second-person-future-perfect
(make-conjugator '(you will have) 'ed))
> (second-person-future-perfect 'laugh)
(YOU WILL HAVE LAUGHED)
</PRE>
<P>We'll explore only a tiny fraction of the area opened up by the idea of
allowing a program as data. Further down the same road is the study of
<EM>compilers</EM> and <EM>interpreters,</EM> the programs that translate your
programs into instructions that computers can carry out. A Scheme
compiler is essentially a function whose domain is Scheme programs.
<P><A HREF="simply-toc.html">(back to Table of Contents)</A>
</BODY>
</HTML>
|