4 Working with words and sentences
(9 activities)
4.1 Quiz: "Communicating with the Scheme Interpreter"(1 step)
4.1.1 (Student Assessment) Review procedure definition, evaluation, and terminology.
1.Define a procedure named geometric-mean that, given two numbers as arguments, returns the square root of their product. You will probably want to use the sqrt procedure, which takes the square root of a number.
2.Consider the following procedure:
    (define (f a b)
      (* b (+ 3 a)) )
Determine the value of the expression
    (f (f 3 4) 5)
3.Assume that a definition of square is written:
(define (square x)
     (* x x))
Write a call to the procedure square with the argument 3.
4.Write a call to the procedure square that will result in the error:
"too few arguments to..."

5.Write a call to the procedure square that will result in the error:
"too many arguments to..."
6.Write a call to the procedure square that will result in the error:
"not a number..."
4.2 Experiment with word arguments and quoting.(7 steps)
4.2.1 (Display page) Review some builtin procedures that take words as arguments.

Review some builtin procedures that take words as inputs.

Up to now, we've worked only with procedures that take numbers as inputs (arguments) and return numbers as values. As you learned from yesterday's reading assignment, the Scheme interpreter also has builtin procedures that work with words and sentences. We'll initially start experimenting with words and then move on to sentences. Two builtin procedures of interest are
  • first, which, given a word as input, returns the first character of that word;
  • butfirst, which, given a word as input, returns the result of removing the first character of the input.
  • last, which, given a word as input, returns the last character of that word;
  • butlast, which, given a word as input, returns the result of removing the last character of the input.
It's an error to give a word with no characters to first or last or butfirst or butlast.
4.2.2 (Brainstorm) Why do we need to quote a word argument to first?
Experiment in the Scheme interpreter with the first procedure. You will find that
(first mike)
produces an error, while
(first (quote mike))
returns an "m". Explain why evaluating (first mike) produces an error message.
4.2.3 (Display page) Why didn't we need to use quote with numbers?

Why didn't we need to use quote with numbers?

Numbers in Scheme are self-evaluating; that is, they evaluate to themselves. This is why you can say
(+ 1 2 3 4)
instead of
(+ (quote 1) (quote 2) (quote 3) (quote 4))
It's OK to quote numbers if you want, but not necessary.
4.2.4 (Brainstorm) Examine quoting in a slightly different context.
Now type the following procedures into the Scheme interpreter.
(define (initial1 name)
  (first name) )

(define (initial2 name)
  (first (quote name)) )
Try to use each procedure to find Mike's first initial, by supplying the word

mike
to each as an argument. That is, what happens when you call
(initial1 'mike)
and
(initial2 'mike)
Explain what each procedure is doing, and why.
4.2.5 (Display page) Now try it with the word procedure.

Now try it with the word procedure.

The word builtin procedure takes any number of words as arguments (it's like the + and * procedures, which take any number of numbers as arguments), and returns the result of gluing all the words together into a single word. Here's an example:
>(word (quote i) (quote am) (quote bic))

iambic
Write and test a procedure named plural that returns the result of gluing an "s" onto the end of its argument. Put your procedure into a file named plural.scm. Here are examples of how plural would be used.
>(plural (quote house))
houses
>(plural (quote guess))

guesss
4.2.6 (Display page) Here's an abbreviation for quote.

Here's an abbreviation for quote.

The quoting operation is used so often that Scheme provides an abbreviation for quote: the expression
'x
is translated by the Scheme interpreter into the expression
(quote x)

for any x.
4.2.7 (WebScheme) Try taking words apart

Predict the result of some expressions

Fill in the blanks below to get the right answer. To see if you are right, press the arrow. If you see a green check, you got it. You will need to wait until you see the word "SchemeHandler" in the upper left corner of the page before you start. If you think one of the calls will give you an error, put "error" in the blank.
Scheme Expression Value Correct?
(first 'oneword) Status icon
(butfirst 'oneword) Status icon
(first 10) Status icon
(butfirst 10) Status icon
(first 'a) Status icon
(butfirst 'a) Status icon
(first "") Status icon
(butfirst "") Status icon
4.3 Experiment with sentences.(6 steps)
4.3.1 (Display page) Here is how sentences work.

Here is how sentences work.

A sentence is a list of words surrounded by parentheses. Here is a two-word sentence:
(mike clancy)
The sentence procedure takes any number of arguments; each must be a word or a sentence. It returns a sentence that contains all the words from the arguments, in order. An example:
> (sentence (quote mary) (quote (had a little)) (quote lamb))

(mary had a little lamb)
Another example, using the quote shortcut:
> (sentence 'the 'quick '(brown fox jumped) 'over '(the lazy dog))
(the quick brown fox jumped over the lazy dog)
The empty sentence is the sentence without any words, namely
( )
4.3.2 (WebScheme) Predict the values of some expressions.

Predict the result of some expressions

Fill in the blanks below to get the right answer. To see if you are right, press the arrow. If you see a green check, you got it. You will need to wait until you see the word "SchemeHandler" in the upper left corner of the page before you start.
Scheme Expression Value Correct?
(sentence 'I '(me mine)) Status icon
(sentence '( ) '(is empty)) Status icon
(word 'ab 'cd) Status icon
(sentence 'ab 'cd) Status icon
(sentence 'a (word 'k 'c)) Status icon
4.3.3 (WebScheme) Experiment with words and sentences.

Experiment with words and sentences

Fill in the blanks below to get the right answer. To see if you are right, press the arrow. If you see a green check, you got it. You will need to wait until you see the word "SchemeHandler" in the upper left corner of the page before you start.
Scheme Expression Right Answer Your Answer Correct?
(   'a 'b)
ab
????
Status icon
(   'a 'b)
(a b) ???? Status icon
(  'a (   'b 'c))
(a bc) ???? Status icon
(sentence (word 'a 'b) (sentence    ))
(ab c d) ???? Status icon
(   'a (   'b 'c))
(a b c) ???? Status icon
(sentence    (sentence 'c 'd))
(a b c d) ???? Status icon
4.3.4 (Display page) First and butfirst work both with words and with sentences.

first and butfirst work with both words and sentences.

The first procedure, applied to a sentence, returns the first word of that sentence. Similarly, the butfirst procedure, given a sentence as argument, returns the result of removing the first word from the sentence. Given an empty sentence as argument, both first and butfirst produce an error. Because first and butfirst can take both words and sentences as arguments, sometimes it's difficult to keep track in a complicated expression about which is which.
4.3.5 (WebScheme) Practice working with words and sentences in the same expression.

Predict the result of some expressions

Fill in the blanks below to get the right answer. To see if you are right, press the arrow. If you see a green check, you got it. You will need to wait until you see the word "SchemeHandler" in the upper left corner of the page before you start.
Scheme Expression Value Correct?
(butfirst 'a) Status icon
(butfirst '(a)) Status icon
(butfirst (first 'abc)) Status icon
(butfirst (first '(abc))) Status icon
(butfirst (first '(ab cd ef))) Status icon
4.3.6 (WebScheme) Practice with more challenging expressions.

Practice with more challenging expressions

Fill in the blanks below to get the right answer. To see if you are right, press the arrow. If you see a green check, you got it. You will need to wait until you see the word "SchemeHandler" in the upper left corner of the page before you start.
Scheme Expression Right Answer Your Answer Correct?
(first (butfirst    ))
abc ???? Status icon
(butfirst (first    ))
abc ???? Status icon
(    (    '(abc def ghi)))
a ???? Status icon
(    (    '(abc def ghi)))
def ???? Status icon
(    (    '(abc def ghi)))
(ghi) ???? Status icon
4.4 Review words, sentences, and quotes.(4 steps)
4.4.1 (Display page) An interesting analogy
A sentence is a collection of words. A word is a collection of letters. Words and sentences are similar to Pez candy dispensers. Here's a basic summary of the argument:
  • The sentence or word itself is the dispenser
  • Individual words in the sentence or letters in the word are like the candies.
  • Individual candies are in a specific order within the dispenser, just like individual words or letters are in a specific order within the sentence or word.
  • With a flip of your finger, you can separate the first candy (first) from the dispenser and all of the rest of the candies (butfirst). You can use those two procedures to separate the first word or letter from the rest of the sentence or word.
  • As long as it is your Pez dispenser, it's OK to take the last candy out. It's also OK to take the first or butfirst of a one-word sentence or a one-letter word.
  • People collect empty Pez dispensers. (I promise I'm not making this up. If you don't believe me, check out the Burlingame Pez Museum.) It's equally OK in Scheme to have an empty sentence (it looks like ( )) or an empty word (it looks like "").
  • A Pez dispenser is only empty when it doesn't have any candy at all in it. You can't just say it's empty if the last thing in there is a candy you don't like. Likewise, you can't say a sentence is empty just because you don't like what it contains. If "" is still a word, ("") is not an empty sentence.
Don't go too crazy with the analogy, though. You can pop the top on a Pez dispenser even after it's empty, although you won't get any candy. If you try to take apart an empty word or sentence, Scheme throws a fit.
4.4.2 (Brainstorm) Review evaluation when quotes are involved.
Explain the difference in meaning between
(first 'mezzanine)
and
(first '(mezzanine))
Don't just say, "The first one returns this and the second returns that." Explain why they are different. Do the same for

(first (square 7))
and
(first '(square 7))
4.4.3 (Brainstorm) Review special cases for butfirst.
Explain the difference between
(butfirst 'x)
and
(butfirst '(x))
Again, tell why they act the way they do. Don't just say they give different answers.
4.4.4 (Display page) (Optional) Things get trickier here.

Things get trickier here.

Supply parentheses and quotes in the line below so that when you evaluate the result, you get (def ghi).

butfirst sentence abc word def ghi
4.5 Experiment with "appearances".(1 step)
4.5.1 (Brainstorm) Find out about "appearances".
Appearances is a builtin procedure that returns some information about how one of its arguments relates to another. Your task is to find out how many and what types of arguments it takes, and what information it returns about those arguments. Knowing how to figure out what a procedure does just by playing with it is a really handy skill, but it can be frustrating. Consider the error messages carefully; your TA can help you make sense of them. If you aren't having any luck, talk to your neighbors and TA!
4.6 Think about evaluating expressions with quotes.(2 steps)
4.6.1 (Display page) Here is how expressions with quotes are evaluated.

Here is how expressions, including those with quotes, are evaluated.

Earlier, we discussed rules for evaluating Scheme expressions involving user-defined procedures. Here is how evaluation of possibly quoted expressions works.
  1. Does the expression contain parentheses? (I.e. is it a "simple" expression without parentheses or a "complicated" expression with parentheses?) Note that a quoted expression such as '(x y) is "complicated", since it really is (quote (x y)). If it's a number, it's self-evaluating; its value is the number itself. If it's a word, it should have been associated with a value, so that value is returned.
     
  2. Otherwise, the expression starts with a left parenthesis. Is "quote" the first word after the left parenthesis? If so, return the quoted word or sentence. Quote is called a special form since it is evaluated in this special-case way.
     
  3. Otherwise, the first word after the left parenthesis should name a procedure; it is looked up among the name of procedures that are either built-in or that have been defined by the user.
     
  4. The arguments are counted to make sure they match the number of placeholder names.
     
  5. The arguments are evaluated; that is, scheme will work through these 7 steps separately for each of the arguments. (This has the effect of the "inside-out" evaluation we did with expressions involving + and *.)
     
  6. The argument values are substituted for the corresponding placeholder names throughout the body of the procedure.
     
  7. The body expression is evaluated, and the result is the value of the procedure call.
4.6.2 (Brainstorm) Here's something to watch out for.
A problem arises when a procedure's placeholder name is the same as the name of a builtin procedure that the procedure is trying to use. Here's an example:
(define (plural word)
  (word word 's) )
Explain what will happen as a result of evaluating
(plural 'house)
Also, explain why it happens.
4.7 Use a sentence to "package" information.(3 steps)
4.7.1 (Display page) Why package information?

Why package information?

A Scheme procedure can only return a single value. When a procedure needs to return two related pieces of information, we typically combine the two into a sentence and then return that. Similarly, instead of designing a procedure to take several related arguments, we often code it to take a single sentence as an argument and then access the parts of the sentence within the procedure. Here's an example. Suppose we want to determine how many inches a given measurement--in feet and inche--represents. We might code this as
(define (inch-count feet inches)
  (+ (* 12 feet) inches) )
But if we wanted to define the inverse procedure, which would take a measurement in inches and return the corresponding number of feet and inches, we would have to return a two-word sentence that contains the feet and inches (or else write two separate procedures, one for feet and one for inches).
4.7.2 (Display page) Write procedures to work with measurements.

Write procedures to work with measurements.

Thus we will represent a height measurement as a two-word sentence, with the first word being a nonnegative integer and the second being a number between 0 and 11, inclusive. Write and test a procedure inch-count that, given a height measurement as argument, returns the number of inches represented by that height. For example,

(inch-count '(2 3))
should return 27 (the number of inches represented by 2 feet, 3 inches). Then write and test a procedure height that, given a measurement in inches, returns the corresponding height measurement in feet and inches. The height and inch-count procedures will be inverses; the value of

(height (inch-count ___ ))
will, for any legal argument to inch-count, be equal to that argument, and the value of

(inch-count (height ____ ))
will similarly be equal to the argument. Put both these procedures into a file named measurements.scm. You might find the procedures quotient and remainder to be useful here:
(quotient 10 4) ==> 2
(quotient 10 6) ==> 1
(remainder 10 4) ==> 2
(remainder 10 6) ==> 4
(remainder 10 5) ==> 0
Your reader contains a short document titled "Integer Division and its uses" that should be helpful with problems requiring quotient, remainder, and the like.
4.7.3 (Display page) (optional) Translate a day in the French Revolutionary year...

Write a procedure to translate a date in the French Revolutionary year to a sentence representing that date.

The following is a good, albeit involved, problem. We've made it optional for you: certainly try it, and don't give up it you have time to work on it. If you do need to leave it before finishing it today, consider coming back in a few weeks to finish it! Note that you can work on the two parts of this problem (getting the month, and getting the day of the month) independently - perhaps by working on procedures named FR-month and FR-day-of-month. Once you have both working, it's easy to put them together to make FR-date work correctly. You'll probably need to test your procedures-in-progress quite a bit. That is, code a little bit, test, code some more, test, and so forth. Remember that in the STk in emacs, you can "bring back" the last thing you typed by pressing the control key and the up-arrow key together. This will make it easy to repeat tests.
Write a procedure named FR-date that, given a day in the French Revolutionary calendar year--a positive integer less than 361--returns a sentence containing the month number and the date in that month. The month number will be an integer between 1 and 12, inclusive; the date in the month will be an integer between 1 and 30. Put your procedure into a file named FR-date.scm, and test it on the examples below.
expression     value
(FR-date 1)    (1 1)
(FR-date 2)    (1 2)
(FR-date 30)   (1 30)
(FR-date 31)   (2 1)
(FR-date 360)  (12 30)
You will find the document "Integer Division and its Uses", available in the CS3 reader as well as here, to be very useful.
4.8 Consider some typical student misconceptions.(5 steps)
4.8.1 (Evidence) Here are five ways students get confused.

Here are five ways students get confused.

Students sometimes invent incorrect rules for using parentheses and quotes. Here are some examples of mistakes that many students have made over the years. These are taken from real life, or at least real students in CS 3. Odds are that several people in this very class will make some of these mistakes this semester. Each example includes the description of the rule, an example—an incorrect call to a procedure—of the rule, the correct call, and a framework for the definition of the procedure to be called.

Bad Rule 1

All arguments provided for a procedure are enclosed in parentheses and quotes.
incorrect call resulting from following the rule correct call procedure definition

(example1 
  '(a thing))

(example1 
  'a 
  'thing)

(define (example1 wd1 wd2)
  ... )

Bad Rule 2

Sentences in general don't need to be quoted.
incorrect call resulting from following the rule correct call procedure definition

(example2 
  (a b c) 
  (d 2 f))

(example2 
  '(a b d) 
  '(d 2 f))

(define (example2 sent1 sent2)
  ... )

Bad Rule 3

Sentences of numbers don't need to be quoted.
incorrect call resulting from following the rule correct call procedure definition

(example3 
  (5 18 299))

(example3 
  '(5 18 299))

(define (example3 numericSent)
  ... )

Bad Rule 4

Words don't need to be quoted.
incorrect call resulting from following the rule correct call procedure definition

(example4 
  try 
  this 
  out)


(example4 
  'try 
  'this 
  'out)

(define (example4 wd1 wd2 wd3)
  ... )
Remember, if you don't quote a word, scheme will try to evaluate it, rather than use it literally.

Bad Rule 5

Quotes may go either inside or outside a sentence.
incorrect call resulting from following the rule correct call procedure definition

(example5 
  ('this 'is 'a 'sentence))

(example5 
  '(this is a sentence))

(define (example5 sent)
  ... )
4.8.2 (Brainstorm) Determine a student's misconceptions.
A CS 3 student is given the following procedure, which returns the result of replacing the first word in its second argument by its first argument.
(define (first-replaced item sent)
  (sentence item (butfirst sent)) )
The student, hoping to produce the value
(red dog)
calls first-replaced as follows:
(first-replaced red (the dog))
Which incorrect rules for evaluation is the student using, and what is the proper call to first-replaced? Briefly explain your conclusion about the student. Hint: this is a good time to add another error or two to your list.
4.8.3 (Brainstorm) Determine another student's misconceptions.
A CS 3 student is given the following procedure, which returns the result of gluing together each word in the first argument with the corresponding word in the second argument. (The student has previously defined the second and third procedures.)
(define (paired-sent sent1 sent2)
  (sentence
    (word (first sent1)  (first sent2))
    (word (second sent1) (second sent2))
    (word (third sent1)  (third sent2)) ) )
The student, hoping to produce the value
(a3 b7 c9)

calls paired-sent as follows:
(paired-sent ('a 'b 'c) (3 7 9))
Which incorrect rules for evaluation is the student using, and what is the proper call to paired-sent? Briefly explain your conclusion about the student and supply the correct call to paired-sent. Remember, the student has already written second and third.
4.8.4 (Brainstorm) Determine a third student's misconceptions.
A CS 3 student is given the following procedure, which returns the result of attaching the square of the first argument to the second argument as its first word.
(define (square-attached num sent)
  (sentence (* num num) sent) )
The student, hoping to produce the value
(16 2 c f 3)
calls square-attached as follows:
(square-attached '(4) '(2 c f 3))
Which incorrect rules for evaluation is the student using, and what is the proper call to square-attached? Briefly explain your conclusion about the student.
4.8.5 (Display page) Reminder: sometimes, in lab, you should look back at earlier materials.
There are certain steps in your lab (Brainstorms and Discussion Forums) in which you can see other students comments and thoughts. You should occasionally revisit these steps, in order to get the most out of them. For instance, the last three steps were Brainstorm exercises. If you were the first student in your section to answer them, you didn't get to see any other responses! We recommend that every student go back, at least once, to look at new responses and consider the problem again.
4.9 Homework(3 steps)
4.9.1 (Display page) Homework activities

Homework exercises to submit by the start of your next lab.

Make sure you read Chapter 6 of Simply Scheme. This isn't really an exercise, but be sure to do it anyway. Do exercises 5.13 and 5.19 in Simply Scheme (available here if you haven't yet gotten the book). Put solutions to these exercises, along with tests of your solution to 5.19, into a file named hwk2.scm in a directory (folder) called hwk2. This will be part of how you submit homework for this class. This is what you need to do:
  1. From an xterm window, type mkdir hwk2 and press return.
  2. To go into that directory, type cd hwk2 and press return.
  3. Now use emacs to make the hwk2.scm file.
Print this file out and hand it in to your TA in the next lab session. In the following weeks, you will use an electronic submission program when you need to turn in homework. We will detail this procedure when it is ready. For this week, however, you will hand in printouts.
Also, be sure to finish all of today's activities by the start of the next lab.

Additional activities

Contribute at least one post and at least one reply to each of the following discussions. Go back to yesterday's homework discussion and make sure you leave thoughtful comments.
4.9.2 (Discussion Forum) Why are quotes and parentheses so hard to understand?
Most people have at least a little trouble with quotes and parens. Do you find any of this confusing? How do you deal with it? If you don't have any problems with quotes or parens, give us a tip on how you make sense of it all. Be sure to make at least one thoughtful comment on somebody else's post. For grading purposes, "Right on!" and "I agree." don't count as thoughtful.
4.9.3 (Discussion Forum) English vs. Scheme
Both English and Scheme have things called words and sentences. These are similar, but not identical. List at least two ways in which English words or sentences are like Scheme words or sentences and at least two ways in which they are not like Scheme words or sentences. Also, make at least one intelligent comment on a classmate's list. Just so you know, things like "Yeah!" do not count as intelligent responses, no matter how much thought you put into them.