COMP203: Lecture 5

Variables, Comments, Modularity and Functional Programming

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.

Math Career Panel, 2/18

Check your grade on Assignment 1

Assignment 2 due midnight, 2/17

How do you feel about changing the date of the midterm?

Review of Variables and Input

At the end of last class, we were trying to write a procedure letternum which requires two inputs, a number and a word, and prints a message about letters in the word as in the example below:
? letternum 5 "competent
Letter number 5 in the word competent is e.
? letternum 3 "cat
Letter number 3 in the word cat is t.
? letternum 4 "Heidi
Letter number 4 in the word Heidi is d.
We started with the line:
to letternum :number :letters
What questions do you have about this task?

Helpful Questions

When planning to write this procedure, it's helpful to ask yourself the following questions: Starting with a list of steps or a plumbing diagram on paper can be extremely helpful in writing the instructions that form the procedure.

Knowing procedures which might be useful is a matter of gaining experience with the language. When you see a Logo instruction, try to remember what the procedures in it do, adding to your knowldege of what is possible in Logo.


The more complicated a program, the more different ways there are to write it. One thing to think about when you're writing a program is how efficient it is -- how many steps it takes to complete its task or how much of the computer's memory it uses. Another thing to think about is organization and clarity -- if you can't remember what a helper procedure does or which procedures rely on a particular operation you wrote, you may have a hard time debugging or making changes to your program.

Luckily, you can write notes to yourself (and others) inside your procedures. The procedures to draw my initials have comments like this, as did the boat procedure in the procedures worksheet. Logo will ignore anything after a semicolon (;), such as notes on which part of the procedure draws the sail and which part draws the hull. Using comments in your more complicated homework assignments can be very helpful to the person grading them and is highly recommended.


This worksheet includes examples of comments and walks you through writing some procedures. If the first few tasks are too easy, feel free to skip ahead! If a task feels too hard, try answering the following questions:

Programs, Superprocedures and Subprocedures

When you wrote a procedure like boat or completed the bonus problem on the worksheet, you were writing a Logo program. A program is a collection of procedures working together to perform a specific task like drawing a boat or playing tic-tac-toe.

Two other useful vocabulary words are superprocedure and subprocedure. A subprocedure is one that's used by another procedure, like triangle is used by boat. A superprocedure is a procedure that uses another procedure; boat is a superprocedure of triangle. Notice that this is a relationship between procedures similar to < or &#subset;. Procedure boat may be a superprocedure for one procedure and a subprocedure for another. In fact, we'll eventually see examples of procedures that are their own subprocedures (and superprocedures).

Variables and readlist

We've seen the Logo operation readlist which outputs whatever is typed at the keyboard, in list format.
? show readlist
Hello, how are you?
[Hello, how are you?]

We've seen an example of this in procedure hi on page 7:

to hi
print [Hi.  What is your name?]
print sentence [How are you,] word first readlist "?
ignore readlist
print [That's nice.]

The operation readlist is very handy, but we can only use its output once. The next thing we'll do is look at a common problem with readlist and see how using a variable can solve it.

What happens when you try to write this converse procedure, shown on page 45 of the text?

? converse
Please type your full name.
Brian Harvey
Your first name is: Brian
Your last name is: Harvey
What goes wrong? Why?

Subprocedures and Functional Programming

The most common problem with writing the converse procedure is that Logo forgets the last name as it displays the first name on the screen. How can we store the last name for later use?

Of course we want to store it in a variable, but how? One handy trick is to use the name as the input to a procedure. The procedure then stores the name in a variable and can pick it apart at leisure:

to converse
  print [Please type your full name.]
  conversehelper readlist
This version of converse asks the user for their name, then gives the name as input to procedure conversehelper. Procedure conversehelper is then very simple to write.
to conversehelper :name
  print sentence [Your first name is:] first :name
  print sentence [Your last name is:] last :name
This is an example of storing a value in Logo by using that value as the input to a procedure.

Changing the value of a variable

You've stopped thinking like a mathematician and started thinking like a programmer when the command below makes sense:
to increase :number
  make "number (:number + 1)
  print :number
? increase 5
? increase 1
In algebra, we never say "x = x + 1". In computer programming, we replace a value by a larger value so often that some languages have special abbreviations to do exactly this. What procedure increment says to Logo is "look up the value stored in the variable named number, add one to it, and store the result in the variable named number." The old value in number is erased and replaced by a value one higher.

Procedure make requires two inputs: a variable name and a value. Using the analogy from last class, make goes to the locker whose name matches its first input, removes anything that's in the locker, then stores its second input in the locker.

So what?

We can use make to solve the problem with procedure converse in a way you might like. It's not elegant, but we can simply create a variable to store the output of readlist:

to converse
  print [Please type your full name.]
  make "fullname readlist
  print sentence [Your first name is:] first :fullname
  print sentence [Your last name is:] last :fullname
We'll learn next class what problems there are with this approach and how to solve some of them.