blob: 2a411dd99b364883408e90f31033d0021177b5b0 (
plain) (
tree)
|
|
<HTML>
<HEAD>
<TITLE>Simply Scheme part II introduction</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE> 2/e Copyright (C) 1999 MIT
<H1>Composition of Functions</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>The big idea in this part of the book is deceptively simple. It's that we
can take the value returned by one function and use it as an argument to
another function. By "hooking up" two functions in this way, we invent a
new, third function. For example, let's say we have a function that adds
the letter <CODE>s</CODE> to the end of a word:
<P><EM>add-s</EM>("<CODE>run</CODE>") = "<CODE>runs</CODE>"
<P>and another function that puts two words together into a sentence:
<P><EM>sentence</EM>("<CODE>day</CODE>", "<CODE>tripper</CODE>") =
"<CODE>day tripper</CODE>"
<P>We can combine these to create a new function that represents the
third person singular form of a verb:
<P><EM>third-person</EM>(verb) = <EM>sentence</EM>("<CODE>she</CODE>",
<EM>add-s</EM>(verb))
<P>That general formula looks like this when applied to a particular
verb:
<P><EM>third-person</EM>("<CODE>sing</CODE>") = "<CODE>she sings</CODE>"
<P>The way we say it in Scheme is
<PRE>
(define (third-person verb)
(sentence 'she (add-s verb)))
</PRE>
<P>(When we give an example like this at the beginning of a part,
don't worry about the fact that you don't recognize the notation. The
example is meant as a preview of what you'll learn in the coming chapters.)
<P>We know that this idea probably doesn't look like much of a big deal to
you. It seems obvious. Nevertheless, it will turn out that we can express
a wide variety of computational algorithms by linking functions together in
this way. This linking is what we mean by
"functional programming."
<P><A HREF="simply-toc.html">(back to Table of Contents)</A>
</BODY>
</HTML>
|