The big idea for this lesson 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:
We know that this idea probably doesn't look like much of a big deal to you.
It might seem 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.”
Reading
finish this before section
This week, most of the reading is meant to improve your
understanding of concepts that you saw in the first lab. So,
you may want to skim parts that you already know. You can use these readings as
a reference when you're finishing the the rest of the lab.
The expression (+ 8 2) has the value 10. It is a
compound expression made up of three atoms.
For this problem, write five other Scheme expressions whose values
are also the number ten:
An atom
Another compound expression made up of three atoms
A compound expression made up of four atoms
A compound expression made up of an atom and two compound subexpressions
Any other kind of expression
Exercise 2.
Let's build a repertoire of functions to deal with words and sentences.
We'll give you the second procedure from the previous lab.
a. Write a procedure first-two that takes a word as its
argument, returning a two-letter word containing the first two letters of the
argument.
b. Write a procedure two-first that takes two words as
arguments, returning a two-letter word containing the first letters of the two
arguments.
c. Now write a procedure two-first-sent
that takes a two-word sentence as argument, returning a two-letter word
containing the first letters of the two words.
Exercise 3.
Write a predicate teen? that returns true if its argument is
between 13 and 19.
Exercise 4.
Write a procedure indef-article.
See examples below:
Homework
do this in section if possible; finish the rest at home
Exercise 5.
Write a procedure insert-and that takes a sentence of
items and returns a new sentence with an and
in the right place.
Exercise 6.
Write a procedure query that turns a statement into a
question by swapping the first two words and adding a question mark to the last
word:
Exercise 7.
Write a procedure european-time to convert a time
from American AM/PM notation into European 24-hour notation. Also write
american-time, which does the opposite:
Exercise 8.
Write a procedure describe-time that takes a number
of seconds as its argument and returns a more useful description of that
amount of time:
Exercise 9.
The following program doesn't work. Why not? Fix it.