Simply Scheme: Introducing Computer Science, Part 3: Composition of Functions Simply Scheme: Introducing Computer Science 2/e Copyright (C) 1999 MIT

Part II

Composition of Functions

cover photo
Brian Harvey
University of California, Berkeley
Matthew Wright
University of California, Santa Barbara

Download PDF version
Back to Table of Contents
BACK chapter thread NEXT
MIT Press web page for Simply Scheme

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

add-s(“run”) = “runs

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

sentence(“day”, “tripper”) = “day tripper

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

third-person(verb) = sentence(“she”, add-s(verb))

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

third-person(“sing”) = “she sings

The way we say it in Scheme is

(define (third-person verb)
  (sentence 'she (add-s verb)))

(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.) 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.”


(back to Table of Contents)

BACK chapter thread NEXT

Brian Harvey, bh@cs.berkeley.edu