COMP203: Lecture 5 Part (b)


Syllabus | Homework and Assignments | Grading Rubric | Midterm Exam | Final Project

When circumstances allow, I'll be typing up bits of my lecture notes and posting them online. These may or may not bear any resemblance to the actual lectures.

Readlist is an Operation, not a Variable

You've been trying to write a procedure that behaves like the one below:
? converse
Please type your full name.
Brian Harvey
Your first name is: Brian
Your last name is: Harvey
>

The first attempt at this program usually results in the following behavior:

? converse
Please type your full name.
Brian Harvey
Your first name is:  Brian
Logo stops halfway through the program! Why?

What happens is that, on the first try, you usually write the following procedure:

to converse
 print [Please type your full name.]
 print sentence [Your first name is:] first readlist
 print sentence [Your last name is:] last readlist
end
Logo faithfully asks you to type in your name, and readlist faithfully outputs your full name to first, which outputs your first name to sentence, which outputs a sentence to print. (Draw a plumbing diagram if you need help understanding this sentence.) The sentence
Your first name is: Brian
appears on the screen. Then Logo tries to run the next instruction. Command print waits for input from operation sentence. Operation sentence has [Your last name is:] as its first input, and the output of last as its second input. Operation last waits for output from readlist. And readlist waits for you to type something at the keyboard. The reason why the program stops is that it's waiting for more input!

Logically, this program makes sense. "Take the first thing in the output of readlist, then take the last thing from that output." However, when you run procedure readlist a second time, it doesn't know what the input was the first time. All it knows is that it's supposed to output something you type at the keyboard.

By now, you may have figured out what we need to do to fix the program. Instead of expecting readlist to remember your name (which it won't), we get Logo to remember your name by storing it in a variable. Then we can use first :variablename and last :variablename to copy the first and last parts of the name stored in the container named variablename.

So far, the only way we know how to store a value in a variable is to make it the input to a procedure. So that's what we'll do:

to converse
 print [Please type your full name:]
 halves readlist
end
Now the output of readlist is the input to some procedure named halves. We don't know what halves looks like yet, but we can already see that it is a command that expects one input, and that that input can be a list.

Procedure halves will store the output of readlist in some variable, and then we can use first and last to look at the value stored in that variable. Problem solved! Try writing procedure halves yourself. If you get stuck, scroll down to see the solution.

 

to halves :fullname
 print sentence [Your first name is:] first :fullname
 print sentence [Your last name is:] last :fullname
end

More Practice

We've written a few sample procedures, but it takes practice to really understand how procedures work and be able to write new ones quickly. Here are a few things you can do to practice:
  1. Change the halves procedure so that it uses correct punctuation. This helps you practice working with words and lists -- a review of chapter 2.
  2. Change the converse program so that it displays a first, middle and last name. This is practice working with words and lists and also gives you a little bit of practice with variables. What happens if a person's full name includes only a first and last name? What would you need to be able to do to avoid this problem?
  3. Write a soap.opera procedure like the one on page 44, or like the one below:
    ? soap.opera "Bob "Krystal "Susan
    Krystal had a fight with Bob
    and Bob and Susan are good friends
    so now Susan is mad at Krystal.
    >
    
    This is a nice practice problem for working with variables.
  4. Write the conj procedure described on page 46.
    ? conj "jouer
    je joue
    tu joues
    il joue
    nous jouons
    vous jouez
    elles jouent
    
    This is a very challenging practice problem for working with variables and words. Good luck!