Fundamentals of Computer Science I (CS151 2003F)

Piratizing Phrases

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.


Useful Procedures: join, map, random, split, string=?,



Exercise 0: Preparation

a. Start DrScheme.

b. At the top of the definitions pane, enter

(load "/home/rebelsky/Web/Scheme/")

Exercise 1: A Pirate Greeting

Document and write a procedure, (pirate-greeting word) that returns an appropriate pirate greeting (e.g., Ahoy) if word is "Hello" and returns word if word is anything else.

Exercise 2: More Greetings

Extend 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 "Hi" and "Greetings".

Exercise 3: Refering to Men

Write a procedure, (pirate-guy word) that returns a "piratey" word for a man if word is "man".

Exercise 4: Alternating Names

Most pirates are not so boring as to use the same word for man at every step. They might use bilge rat, landlubber, 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 mood.

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?

c. Rewrite pirate-guy so that it alternates between three or more terms.

Exercise 5: Combining Ideas

Combine the ideas from the previous exercises to write a single procedure, (pirate-word word) that converts word to a word in pirate-speak, at least for some words. For example, it should convert "Hello" to "Ahoy" and "man" to some appropriate appellation.

Exercise 6: Changing Multiple Words

Right now, pirate-word changes only one word. What if we'd like to change multiple words? We can use one of the really-cool Scheme procedures, map. The 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 does.

Exercise 7: Splitting Phrases

Scheme's 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 join.

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.

b. Try applying join to some lists of words. What does it seem to do?

c. How could you combine split, join, map, and pirate-word to piratize the phrase "Hello. I just saw a man laugh at another man."

Exercise 8: Putting it Together

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, (piratize phrase).

b. Try it on a number of examples.

Exercise 9: Ends of sentences

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.

Exercise 10: Extend, Extend, Extend

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]


Disclaimer: 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

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,