about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/ssch3/part2
blob: 6a78f0eb6c33c657169e15d9f26f61dbc6ffbf40 (plain) (blame)
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
\input bkmacs

\part{Composition of Functions}
\justidx{composition of functions}
\justidx{function composition}

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 {\tt s} to the end of a word:

\smallskip
\centerline{{\it add-s\/}(``{\tt run}'') = ``{\tt runs}''}
\smallskip

\noindent and another function that puts two words together into a sentence:

\smallskip
\centerline{{\it sentence\/}(``{\tt day}'', ``{\tt tripper}'') = ``{\tt
day tripper}''}
\smallskip

\noindent We can combine these to create a new function that represents the
third person singular form of a verb:

\smallskip
\centerline{{\it third-person\/}(verb) = {\it sentence\/}(``{\tt
she}'', {\it add-s\/}(verb))}
\smallskip

\noindent That general formula looks like this when applied to a particular
verb:

\smallskip
\centerline{{\it third-person\/}(``{\tt sing}'') = ``{\tt she sings}''}
\smallskip

\noindent The way we say it in Scheme is

{\prgex%
(define (third-person verb)
  (sentence 'she (add-s verb)))
}

\noindent (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.)
\vfill\eject

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
``\bkidx{functional}{programming}.''

\bye