blob: 859a67b9bbcbcc5298c63504bfb9ae08a45f18ec (
plain) (
tree)
|
|
<P>
<P>
<HTML>
<HEAD>
<TITLE>Simply Scheme: Introducing Computer Science, Part 8: Functions as Data</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme:</CITE>
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H2>Part III</H2>
<H1>Functions as Data</H1>
<TABLE width="100%"><TR><TD>
<IMG SRC="../simply.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><CITE><A HREF="http://ccrma.stanford.edu/~matt">Matthew
Wright</A><BR>University of California, Santa Barbara</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/ssch08.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../ss-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="../ssch7/variables.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="higher.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="http://mitpress.mit.edu/0262082810">MIT
Press web page for <CITE>Simply Scheme</CITE></A>
</TABLE></TABLE>
<HR><BIG>
<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:
<P><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:
<P><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>
</BIG>
<HR>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="../ssch7/variables.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="higher.html"><STRONG>NEXT</STRONG></A>
<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>,
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>
|