COMP203: Lecture 2

Procedures

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.

Office Hours: Conant 453. Tuesday 11:30-12:30, Wednesday 11-12, Thursday 12:30-1:30 and by appointment.

Chapter 2

Now that we've seen some of what Logo can do, let's go back and look at the language in more detail.

Procedures

Last class we made Logo do things using commands like print [Hello world!] and forward 50. The commands print and forward are words in Logo's vocabulary -- Logo knows what they mean. In general, the words Logo knows are called procedures; print, help and forward are procedures. Last class we created a procedure named sayhello, and in chapter 1 the author defines the procedures hi and music.quiz.

Other words, like say, draw and arccos are not in Logo's vocabulary. We could add them to Logo's vocabulary by writing procedures with those names.

In order to give instructions to Logo, you need to learn the words it knows. Here are some procedure names you should memorize. Use Logo's help procedure to look up details on using these procdures.

clearscreen
Erases everything on the screen and moves the turtle to the center.
back
Moves the turtle backward on the screen the specified distance.
forward
Moves the turtle forward on the screen the specified distance.
penup
Stops the turtle from drawing a line as it travels.
pendown
Tells the turtle to draw a line as it travels.
list
Combines two inputs into a list. This may result in a list of lists.
sentence
Merges two inputs into a single list. This can be used to combine the contents of two lists.
first
Outputs the first item in a list.
butfirst
Outputs all of a list except its first item.
last
Outputs the last item in a list.
butlast
Outputs all of a list except its last item.
item
Outputs an item from a specified point in a list.
readword
Outputs a word typed at the keyboard.
readlist
Outputs a list of words typed at the keyboard.

Technical Terms

We're going to spend half a semester learning Logo, so we take some time early in the semester to define technical terms (p. 12, 17) to use in describing this language.

We know that print is a built-in Logo procedure. What happens if we just tell Logo to print? Try it!

Logo tells us not enough inputs to print. It's complaining because we didn't tell it what to print.

Some procedures, like cleartext and readlist, don't need extra information to work. Some procedures, like print and sum, do. An instruction is a statement that contains all the information Logo needs to do something.

Here are some examples of instructions. Try them out!

print [Hello world!]
help "readlist
print random 7
print sentence "Hello word first readlist
forward 30
right 90
Here are some examples of things that aren't instructions. Can you guess what's wrong with them?
print
print sentence "Hello
sum 2
sum 2 3
sentence "Hello "world

Commands and Operations

We know that sum is a built-in Logo procedure. What happened when we told Logo to sum 2 3?

Logo told us You don't say what to do with 5. Obviously Logo successfully added 2 to 3. Why is it complaining about the result?

Logo has two types of procedures: commands and operations. Commands such as help, forward, print and save do something for you, like displaying words on the screen. Operations such as sum, readlist, first and list output information for other Logo procedures to use.

Complete instructions

In the instruction

print 17
the word print is the name of a procedure. The number 17 is input to that procedure. Grammatically, the command print must always be followed by what you want Logo to print. (What happens if you just type print at the ? prompt and hit enter?)

The textbook tells us more about the grammatical rules for communicating with Logo:
A complete instruction is a command followed by as many expressions as it needs.

A "complete instruction" is a sort of Logo sentence telling Logo to perform one task. Examples of complete instructions include:

cleartext
print 17
repeat 50 [setcursor list random 75 random 20 type "Hi]

Practice Break!

Using the commands introduced in this class, see if you can use Logo to:

Expressions

The procedures we wrote last class didn't need any additional information to do their job; neither does procedure cleartext. Procedure print needs to know what text to display on the screen. Procedure repeat needs to know how many times to repeat and what to repeat.

The number of expressions a procedure needs in order to operate depends on the procedure -- expressions (or "inputs") like 17 and [forward 100 right 90] are sometimes required to describe what the procedure is supposed to do.

To find out what type and how many inputs a procedure requires, type help followed by quotes and the procedure name at the question mark prompt. For example, help "repeat, help "print or help "textscreen. (Procedure help is a command which requires one input -- the name of a procedure.)

Another example of a complete instruction is:

print sum 12 5
Here sum 12 5 is the expression that tells print what to display. The input to print is the "output" given to it by procedure sum.

Operations

when we asked Logo to evaluate sum 2 3 we got an error message. sum 2 3 is not an instruction that makes sense to Logo, sort of like "while walking quietly" is not a sentence in English. When you typed in sum 2 3 you told Logo "5". Logo displayed an error message saying "5 what?"

Procedure sum is an operation -- a procedures that outputs information for other procedures. To complete the instruction sum 2 3 we might choose print sum 2 3. As another example,

forward random 150
draws a random length line on the screen. The instruction:
random 150
generates an error message like: You don't say what to do with 33.

In the first example, random 150 is doing something for procedure forward and forward is doing something for you -- showing you a line segment with a random length. In the second example random 150 tries to do something for a procedure, but there is no procedure for it to help.

The book explains the difference between these two types of procedure on page 17:

An operation is a procedure that computes a value and outputs it; random, list and readword are operations.

A command is a procedure that does not output a value but instead has some effect, such as displaying something on the screen; print, save, left and forward are commands.

We will see many examples of commands and operations in this class, and will often see error messages when we accidentally treat operations as if they were commands. A common mistake on homework is to write procedures that are commands when you're asked to write operations. As long as you test everything carefully to be sure it does what you think it does, you should be able to stay out of trouble.

Plumbing Diagrams

The author of the textbook has a nice way of keeping track of what information a procedure needs, where it comes from, and where any output from that procedure is going to go.

What do you think the instruction print sum 4 product 10 2 will do when entered in Logo? Try it!

On page 14 in Chapter 2 the author describes every detail of how Logo parses this instruction. He then recommends a much simpler graphical method of recording the same information called plumbing diagrams. Much of the remainder of chapter 2 involves analyzing commands using plumbing diagrams.

In a plumbing diagram, each procedure is represented by a box. Procedures that require inputs have "hoppers" on top to receive them. Procedures (operations) that produce output have "drain pipes" on the bottom that deposit their output in the hopper of a procedure below. Commands (procedures with no output) do not have drain pipes.

The plumbing diagram for print sum 2 3 would look like:

You can tell that sum 2 3 is not a complete instruction from its plumbing diagram -- the output of the pipe from sum is not collected in a hopper.

You can tell that print sentence "hello is not a complete instruction because one of the hoppers in sentence is unfilled.

The text has an example of the plumbing diaram for print sum 4 product 10 2.

Draw a plumbing diagram for the instruction print sentence "Hello readlist. Use the help command to find out what inputs each procedure requires.

Draw a plumbing diagram for the instruction:

setcursor list random 75 random 20

Draw a plumbing diagram for the instruction:

repeat 50 [setcursor list random 75 random 20]

readword

We had fun making Logo draw pictures last class -- often that's as far as a grade school student will go with Logo. However, as we saw in Chapter 1, it's possible to write more interesting and interactive programs in Logo. One way to do this is with the operation readword. For example:
to welcome
 print [Welcome to COMP203!  What is your name?]
 print (sentence [I hope you have fun here] word readword "!)
end
Here the operation readword helps the operation word by outputting whatever is typed at the keyboard when readword is run. Try out the procedure! Here's a simpler procedure using readword:
to testreadword
 print [Type a word at the keyboard and hit enter.]
 print last readword
 print [is the last letter of the word you typed.]
end

Type edit "welcome to change procedure welcome, experimenting with word, last, sentence and readword until you know something about what each of these operations does. Use help or ask a neighbor if you get stuck.