COMP203: Lecture 2


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.

Logo Procedures and Notepad

Last class you learned to store a series of Logo commands as a procedure to be run later. Start this class by writing another Logo procedure. (If you don't have an idea for your own procedure you can use procedure hi from lecture 1.)

If you copy and paste, you'll find that our version of Logo has some issues displaying the text you pasted in. Hit enter one or more times and maybe type cleartext to get it back to normal. Then type the name of the procedure you just pasted in and see if it works! (If it doesn't work, make sure that you have a question mark prompt and not > or ~. If you don't paste in the line that says end, Logo will think anything new you type is part of the procedure you just pasted in.)

Being able to copy and paste procedures into Logo is a big help -- it makes your homework easy to grade and lets you use Notepad to edit your procedures rather than Logo's editor. This is particularly helpful if your homework is a Notepad document full of Logo procedures.

However, once you have defined a procedure in Logo you cannot define a new procedure with the same name. Try typing in the following instructions and see what happens:

to design2
 repeat 8 [forward 100 right 135]
end
to design2
After you type to design2 the second time, Logo gives an error message. Instead of politely waiting for you to give it a list of instructions to store in the procedure design2, Logo tells you that design2 already exists and refuses to let you redefine it. This keeps you from accidentally erasing something important, but it also makes it harder for you to work on your homework in Notepad and paste corrections into Logo.

If you want to edit procedures in Notepad instead of Logo, you have to erase the old procedure before pasting in the new one. To get rid of the old design2 procedure, type erase "design2 and hit enter. Logo forgets the list of instructions stored in procedure design2 and you're free to define a new version of the procedure:

to design2
 repeat 5 [forward 100 right 144]
end
Open Notepad now, type (or paste) a procedure into it, and then copy and paste that procedure into Logo. If you're in a hurry or don't know what you want to write, you can use the following procedure:
to welcome
 print [Welcome to COMP203!  What is your name?]
 print (sentence [I hope you have fun here] word readword "!)
end
Try it -- does it do what you expect it to? What would you like to change about it?

Edit the procedure in Notepad, erase it in Logo, and then copy and paste it from Notepad into Logo.

Reviewing your work

You should now have two procedures defined in Logo. Do you remember their names and what they do? If not, you can get Logo to print out their names using the command pots. Try it now.

If you want to look at what's in all those procedures, use the command edall.

You can erase all those procedures using the command erall, but don't do that yet. (This is very useful to me when grading papers, but probably less useful to you in general.)

Saving your work

To create a file named mywork.txt containing all your procedure definitions, type save "mywork.txt and hit enter. The file created this way is on the computer you're working on and can be loaded using the command load "mywork.txt. Classroom and lab computers are not safe places for long term file storage. However, they should be connected to a network drive which is safe. To save your work on your personal "W drive", type:
save "W:/Private/mywork.txt
You can then load your file by replacing "save" by "load" in the command above. You can also open the file mywork.txt in Notepad by navigating through Computer to W: to Private. Try it now!

If you open the file mywork.txt in Notepad, you might notice that the procedures weren't saved in the order you defined them. This is typical; Logo's save command is good for making backups in case of power outages or when considering major changes to a program but is not very good for working on multiple homework problems at the same time. For that you should use an editor like Notepad.

It's possible to set up your home computer to access your W: drive the same way the classroom computers do. To do so, follow the instructions for your computer's operating system at http://it.bridgew.edu/Support/Win7WebDAV.cfm. (Note that with Vista, I had to re-map the drive every time I turned on my computer.)

Lists, load and Language

The Logo programming language is designed using lists -- Logo procedures are just list of instructions.

We can think about our language as designed using lists as well. Words are lists of letters, sentences are lists of words, paragraphs are lists of sentences, and essays are lists of paragraphs.

From this perspective, Logo is the perfect language for working with language. In the CSLS folder that comes with your Logo program you'll find a file named DOCTOR. Save that file to your W: drive.

Now, start Logo and load the file using a command like:

load "W:/Private/DOCTOR.txt
(Logo's load command accepts one input, which is the name of a file on your computer. The effect of the command is to load all the Logo procedures from that file into Logo.)

To run the program once it's loaded, use the command doctor.

To see what procedures Logo has loaded, type poall -- this will display the titles of all the procedures you've definded for Logo.

Chapter 2

Now that we've finished our initial "test drive" of Logo, let's go back and look at the language in more detail.

For example, why does typing:

repeat 50 [setcursor list random 75 random 20 type "Hi]
cause Logo to write the word "Hi" all over the screen?

Logo's help command can be of use to us here. Type help "repeat at the ? prompt.

You're told that repeat is a command (more on that soon) which runs a list of instructions some number of times. In the example above, it looks like the number of times is 50 and the instructions are:

setcursor list random 75 random 20 type "Hi
The instruction type "Hi seems pretty self-explanatory. What's going on in the rest of the instruction list?

We could continue to dissect this example by typing help "random, help "list and help "setcursor. In theory, it's possible to completely predict a computer's actions by looking at its code in this way. Take a minute to practice with the help command -- being able to predict what code will do will be very useful to you in figuring out why it's not doing what you think it ought to.

Types of Procedures

The help command told us that repeat was a command and that random was an instruction. What do these words mean?

It's helpful to think of procedures as coming in two types: procedures that do things for you, and procedures that do things for other procedures. If you get the two types mixed up, Logo will give you an error message. For example, the instruction:

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 randomized 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.

A complete instruction is a command followed by as many expressions as it needs.

What does "as many expressions as it needs" mean, and how did we get from page 11 to page 17? Let's back up a bit and explain.

Inputs

Try typing forward into Logo and hitting enter. You get an error message like not enough inputs to forward. The command forward doesn't just move the turtle forward; it moves the turtle forward some specified distance. The command forward wouldn't be very useful if you couldn't specify how far to move the turtle!

In the complete instruction forward 150 the number 150 after the command forward is the expression forward needs in order to know how far to move the turtle. In the instruction type "hi, "hi is the expression type needs in order to know what to display on the screen. Similarly, print requires an input like [Hi. What's your name?] to display on the screen.

An input is a piece of information given to a Logo procedure. Examples of inputs include "hi, 150 and the result of running the (incomplete) instruction random 75.

Practice

Students often have difficulty learning to use inputs and variables. There are a variety of tools and teaching techniques that can help with this (if the ones presented in this class don't help you, ask for more!) but first, let's warm up with a few exercies. Open the worksheet on procedures and work as far through it as time permits. If you get stuck, feel free to ask your neighbor for help.