COMP203: Lecture 3


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.

Inputs

Last class we learned about commands and operations, and that "A complete instruction is a command followed by as many expressions as it needs." The number of expressions a command needs is (at least) the number of inputs that command requires to produce the effect you want. The command forward needs one input -- the distance you want the turtle to move forward.
? help "forward
FORWARD dist
FD dist

        moves the turtle forward, in the direction that it's facing, by
        the specified distance (measured in turtle steps).

Each Logo procedure requires a certain number of inputs. For the procedures Logo comes with, the help procedure tells you how many inputs that procedure needs and what type of inputs (words, lists, numbers) it expects. The command print needs one input -- the word or list of words that you want to appear on the screen.

? help "print
PRINT thing
PR thing
(PRINT thing1 thing2 ...)
(PR thing1 thing2 ...)

        command.  Prints the input or inputs to the current write stream
        (initially the screen).  All the inputs are printed on a single
        line, separated by spaces, ending with a newline.  If an input is a
        list, square brackets are not printed around it, but brackets are
        printed around sublists.  Braces are always printed around arrays.
We see from the help information on print that this command cna be abbreviated and that it can sometimes accept more than two inputs.

The operation sum also defaults to two inputs but can accept more

? print sum 3 5
8
? print (sum 3 5 7 9)
24
?

Remember that sum 2 3 is not a complete instruction because sum is not a command. sum is an operation; in order to use it without getting an error message we have to make it part of a complete instruction, as in:

print sum 3 5
You might think of operations as being like mathematical functions -- they require one or more variables as input and then (usually) use those values to compute some output.

Type help "sum to see the help information on sum. How many and what type of inputs does the procedure help require?

Complicated Inputs

So far this seems simple -- some procedures require inputs, and some provide them -- but it can get complicated quickly. For example, can you tell if:

print sentence [How are you,] word first readlist "?
is a complete instruction?

Let's analyze this instruction starting with help "print:

PRINT thing
PR thing
(PRINT thing1 thing2 ...)
(PR thing1 thing2 ...)

        command.  Prints the input or inputs to the current write stream
        (initially the screen).  All the inputs are printed on a single
        line, separated by spaces, ending with a newline.  If an input is a
        list, square brackets are not printed around it, but brackets are
        printed around sublists.  Braces are always printed around arrays.
Command print requires one input. In order for our instruction to be complete, procedure sentence must output one value to procedure print.
?help "sentence
SENTENCE thing1 thing2
SE thing1 thing2
(SENTENCE thing1 thing2 thing3 ...)
(SE thing1 thing2 thing3 ...)

        outputs a list whose members are its inputs, if those inputs are
        not lists, or the members of its inputs, if those inputs are lists.
Operation sentence requires two inputs. Those two inputs must be [How are you,] and the output of word. Similarly, operation word requires two inputs: the output of first and "?.

How do we tell that "? is the second input to word rather than the output of readlist being the second input to word? In fact, the output of readlist is the input to first. When evaluating the word command Logo evaluates first to get the first input to word before looking for the second input to word. One way to remind ourselves of this is to rewrite the instruction as:

print sentence [How are you,] word (first readlist) "?
This illustrates that (first readlist) is a command together with its input, and that the output of this command is a single input to word. (It is always ok, and sometimes necessary, to put parentheses around a command and its inputs. It is not ok to put parentheses around only a command's inputs. So (sum 2 3) is ok and sum (2 3) is not ok.)

Plumbing Diagrams

Most of what's left of chapter 2 deals with analyzing commands in this way. The author has invented a nice tool for keeping track of which inputs go to which procedures called "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.

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

Draw a plumbing diagram for the instruction forward random 50.

Draw a plumbing diagram for the instruction:

print sentence [How are you,] word first readlist "?

Manipulating Words and Lists

The last thing we have to do before learning to write procedures that use inputs is familiarize ourselves with commands that do things to words and lists. Logo procedures and instructions are made up of lists; Logo is a very good programming language for working with lists.

First, think about the fact that Logo needs to know whether an input is a word or an instruction. The instruction print design2 is not the same as print "design2 (try them both!)

If you want Logo to print a word rather than running a procedure, put quotation marks (") in front of the word. Try now to get Logo to print your name.

Lists

If we want Logo to print more than one word, the best way to proceed is to combine those words in a "list". To make a list, enclose the elements of the list, in order, in square brackets. For example:
[1 2 3 4 5]
[The quick brown fox jumped over the lazy dog.]
[How are you,]
[setcursor list random 75 random 20 type "Hi]
[forward 50 right 120]
We can use lists as input in just the same way we use quoted words:
type [1 2 3 4 5]
print [The quick brown fox jumped over the lazy dog.]
Try now to get Logo to greet you by name:
Hi Heidi!

Parts of Lists

You might want Logo to say hello to all your friends now, but you probably don't want to type in all your friends' names individually. We saw in Chapter 1 that Logo can combine parts of lists to make customized sentences. How does that work?

The commands print and type accept a word or list as input and display that word or the contents of that list on the screen. The command show displays a list on the screen along with the brackets quoting the list; this is useful when working with mathematical lists:

show [1 2 3 4 5]
The operation first accepts a word or list as input and outputs the first letter or word of the word or list, respectively. The operation butfirst accepts a word or list as input and outputs everything but the first letter or word of the list.

By combining first and butfirst we can access any item in a word or list. For example, print first butfirst butfirst "octopus is a complete instruction that displays the third letter of the word octopus on the screen. We can use a plumbing diagram to see how this works.

Logo also has operations last and butlast that do what you'd expect. Can you guess what the instruction

print last butlast [1 2 3 4 5]
will do? Use Logo to check your answer.

Next, use Logo to print the third letter of the word "exaggerate".

If we want to see the sixth letter of the word octopus, we could type:

print first butfirst butfirst butfirst butfirst butfirst "octopus
This is no fun, and computers are supposed to be good at repeating simple tasks like "remove the first letter from the word"; can Logo make this easier for us? In fact, item is a Logo operation that takes two inputs -- a number n and a word or list -- and outputs the nth item in the list: print item 6 "octopus.

Use item to get Logo to display the third word in a sentence such as [The quick brown fox jumped over the lazy dog.]

Combining Parts

In addition to selecting items from lists or words we can also combine letters into words or join two lists. The operation word accepts two words or letters as input and outputs a word combining them:

print word "Heidi "?
The operation sentence accepts two lists or words as input and outputs a list combining them:
show sentence [How are you,] "Heidi?
Combine everything we've learned about lists to draw the plumbing diagram for the complete instruction:
print sentence [How are you,] word first [Heidi Burgiel] "?

Your answer should look something like this image. We eventually see an example in which word is used to conjugate French verbs:

print word "jou "ez
Draw a plumbing diagram for the following complete instruction:
print sentence [The sum of 2 and 3 is] word sum 2 3 ".