Summary: In this laboratory, you will enhance your understanding of strings in Scheme and learn some new procedures as you focus on a useful task: turning regular phrases into those a pirate might say.
a. Start DrScheme.
b. At the top of the definitions pane, enter
Document and write a procedure,
that returns an appropriate pirate greeting (e.g.,
"Hello" and returns
word is anything else.
pirate-greeting so that it returns the greeting
for more than just
"Hello". It shouldn't return the greeting
you have selected all the time, but it should probably return it for
Write a procedure,
(pirate-guy word) that returns a "piratey" word for a man if
Most pirates are not so boring as to use the same word for
man at every step. They might use
bastard, and many other things, depending on their mood and the situation. Hence, we should have
pirate-guy make different choices depending on the computer's
Fortunately, Scheme provides a
random procedure that
can help. The expression
(random number) returns
an integer between 0 and number-1. You can use this procedure
to select between two strings with
(if (= (random 2) 0) "bilge rat" "landlubber")
a. Execute the preceding expression a few times to see what happens.
b. What happens if you change the
2 to a
4 in the example?
pirate-guy so that it alternates between three or more terms.
Combine the ideas from the previous exercises to write a single procedure,
(pirate-word word) that converts
a word in pirate-speak, at least for some words. For example, it should
to some appropriate appellation.
pirate-word changes only one word. What if we'd
like to change multiple words? We can use one of the really-cool Scheme
map procedure takes two
arguments, a procedure and a list, and it applies the procedure to every
element of the list.
That description may not make much sense to you until you've tried it.
a. See what happens when you write
(map pirate-word (list "one" "man" "and" "another" "man" "played" "dice"))
b. Play with enough examples that you're confident that you understand what
map procedure makes life much easier. However, it is still fairly painful to have to write phrases as a list of words. Unfortunately, Scheme does not come with a procedure for extracting all the words in a phrase. Fortunately, I've spent the time writing such a procedure. I call it
split. (No, it has nothing to do with bowling.) Its inverse is
a. Try applying
split to a variety of strings and see if you can figure out what it does. Here are a few you might try.
"Hello. What is your name?"
b. Try applying
join to some lists of words. What does it seem to do?
c. How could you combine
pirate-word to piratize the phrase "Hello. I just saw a man laugh at another man."
If you figured out the previous problem, you probably came up with an answer like
(join (map pirate-word (split "...")))
a. It is painful to have to type all of that each time we want to piratize a phrase. Turn that expression into a procedure,
b. Try it on a number of examples.
Some pirates like to add an extra phrase to the end of a sentence, such as
Saavy?. Figure out how to extend
piratize to add some piratey phrases to the ends of some sentences.
Extend your procedures as far as you are willing. You will turn in these procedures as homework 2.
Wednesday, 17 September 2003 [Samuel A. Rebelsky]
Friday, 19 September 2003 [Samuel A. Rebelsky]
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 9 13:59:15 2003.
The source to the document was last modified on Fri Sep 19 14:28:59 2003.
This document may be found at