COMP203: Lecture 1

Messing around with Logo

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.

Welcome to COMP203!

Let's...

About The Class

Q: Why bother teaching programming?

A: For future teachers: so that your students learn and practice linear, rational thought (as in Geometry or Logic). For future researchers: we can solve more and larger problems with technology than we can without, but at times we need to custom-design tools for doing so. Programming empowers us to create our own technological problem-solving tools.

Q: What's "computer algebra"?

A: Computer algebra is algebra that's done by computers. A cheap calculator can add, subtract, multiply and divide, but can't solve equations. Computer algebra systems can solve equations, multiply matrices, integrate functions, and apply the rules of group theory.

Q: What's Logo?

A: Logo is a programming language invented in Cambridge, Massachusetts in 1967. It was originally used to teach computer programming to young children -- that was before it was possible to use a computer without knowing programming, but after computers became more accessible. Logo is a good language to use when teaching programming; it provides instant, sometimes visual feedback, and supports and encourages "modular" programming.

Q: What's Maple?

A: Maple is a computer algebra system; mathematicians and other scientists use it to solve difficult math problems. You may already have used it in a number theory or physics class. Whenever possible, Maple will give exact answers to the questions you program into it (e.g. 1/3 instead of 0.333333). However, if an exact answer is not possible or if you prefer an approximation you can ask Maple to approximate.

Mathematica, Macsyma, Sage, Octave, and MATLAB are other computer algebra systems.

Q: When am I ever going to use this?

A: You can use Maple to help you in your physics, algebra, calculus or number theory classes; you might use Maple or another computer algebra system in industry or graduate school as well.

You might use Logo in a programming or geometry course, or in grade school enrichment programs or projects. You are unlikely to get a high paying job as a Logo programmer -- if you want to be a professional programmer you should take COMP151, not COMP203.

What's Different about this course?

About the Textbook

The Logo textbook is available free online, but is out of print as are most other Logo texts.

It's a really great book, written more to be fun and interesting than to be a textbook. This class tries to supplement and complement the material in the book. If you are having trouble understanding an explanation in the book, ask about it at the beginning of class.

Getting started

How does Logo work? You can run it on the classroom computers by typing "Logo" into the box above the Windows start button. You should see a black screen with a white question mark.

This question mark is what is often called a command prompt. When you see this, Logo is waiting for a instruction. Let's type in an instruction!

? print [Hello owrld!]
Hello owrld!
?
When we give it an instruction, Logo does exactly what we tell it to (which isn't always what we want it to). Here are a few instructions you might try:
repeat 5 [forward 100 right 144]
print sum 1234 6789
print first "Heidi
print first readword
For the final command, Logo won't do anything until you hit enter a second time. If you just hit enter without typing anything first, you'll get your first error message!

It's ok to get error messages. Logo isn't mad at you, it just doesn't understand you. Now let's go to the textbook to see a wider range of instructions (and perhaps more error messages!)

Chapter 1: Exploration

This chapter is a warmup -- the point is to introduce you to Logo and let you see what it can do. The first instruction in the book is deliberately complex, to give you something to think about.

repeat 50 [setcursor list random 75 random 20 type "hi]

Type it in carefully, hit enter, and see what happens. If it makes a big mess of your Logo window, you got it right. Type cleartext or ct to clean up the mess.

Look closely at the words used in this instruction. What do you think each of them does? Talk to your neighbor and try using Logo to confirm your guesses; be prepared to share your conclusions with the class. Don't be afraid to experiment -- you can always quit and restart Logo.

Commanding a Computer

The point of this exercise was not to learn about repeat and type, but to demonstrate how the Logo language uses keywords to give instructions to the computer.

Commands like type and repeat make the computer do something, in the same way that selecting "paste" from the edit menu makes a computer do something.

Operations like random and list feed information to the computer, in much the same way as selecting a block of text gives the computer information on what you want copied to the clipboard.

Our computers aren't "intelligent"; they can't guess what you mean when you use words they don't know. However, we can program them to remember a large number of different words and commands and respond to them appropriately. When "talking to" Logo you will need to use vocabulary and grammar the computer has been programmed to understand, and to be very careful of your spelling and punctuation.

The experience of having a device do exactly what you tell it to do (and not what you wanted it to) is very educational. The experience of having Logo fail to do what you want it to because you forgot to type quotation marks can also be very frustrating; if you get stuck please feel free to ask a neighbor for help, email me the code you're working on, or see me during office hours.

You can find out what a word means to Logo and what grammar is required to use it by looking the word up in the manual. For example, the manual entry for the word setcursor is:

SETCURSOR vector

	command.  The input is a list of two numbers, the x and y
	coordinates of a text window position (origin in the upper left
	corner, positive direction is southeast).  The text cursor
	is moved to the requested position.  This command also forces
	the immediate printing of any buffered characters.
You can get this information more quickly by typing help "setcursor at the Logo question mark prompt.

What the computer is telling you is that Logo knows the command SETCURSOR (the word setcursor is in Logo's vocabulary). To use setcursor you must follow a rule of grammar that says the word setcursor always comes before a vector, similar to the way a noun precedes a verb.

In general, type help, then quotes ("), then the command name to get help on any Logo command. There should be no space between the quotes and the command name. Take a second to get help on one of the vocabulary words (procedure names) used in the first example.

Commands and Drawing

Logo classes for younger children frequently rely on Logo's drawing abilities and instant visual feedback. Let's take a few minutes to try out the commands forward, right and left. An example of an interesting sequence of commands is given below.
left 90
forward 100
right 90
forward 200
Play with these commands -- it's like having an Etch A Sketch on your computer screen! To erase the screen, type clearscreen.

When used this way, Logo doesn't keep track of the commands you've given. Once you create a design you like, take notes on it (or type textscreen and copy and paste from the Logo window) so that you can reproduce it next class!

Procedures

You now have a list of commands that makes a nice design. But typing all those commands in is tedious and error prone. Is there a way to store them in the computer rather than retyping them from your notes?

A Logo procedure is a way of storing a series of Logo commands to be executed (run) later. A procedure is a very simple example of a computer program. In this class we'll use the word "program" to mean a procedure or collection of procedures that accomplish some task, like drawing a picture or playing a game.

The book gives an example of a procedure on page 5:

to hi
 print [Hi.  What's your name?]
 print sentence [How are you,] word first readlist "?
 ignore readlist
 print [That's nice.]
end
In a few weeks you'll know what all the parts of this procedure do. For now, we'll focus on specific parts of it while taking the remainder for granted.

The vocabulary word used to define a procedure is "to". This lets Logo know that you're about to define a procedure rather than giving it a command. After to there's a space, then one word hi that is the name of the procedure. To run this procedure we first copy and paste it into Logo to "define" the word "hi" for Logo. Then we can type the word hi at the ? prompt and hit enter to see what it does.

If you want to use more than one word to name your procedure, put dots or _'s but not spaces between the words.

You're going to write a procedure to draw your special design. Type "to" and the name of your procedure, then hit Enter. The Logo question mark prompt is replaced by a carat prompt (>). This indicates that Logo is waiting for you to type in the commands that make up the procedure. Notice that the picture on the screen does not change as you type in your commands -- Logo is storing your instructions be followed later, not following them right now.

After you've finished typing in commands, type end and hit Enter to tell Logo that it should stop storing instructions and go back to being ready to run them. If everything was typed correctly, Logo will tell you your procedure is defined, and return to the question mark prompt.

Now you can see what the list of commands you stored in your procedure actually does. Type your procedure name (e.g. design) at the question mark prompt, hit enter, and see what happens!

If you have extra time, see if you can write Logo procedures to draw squares, rectangles, letters, or other interesting shapes. A sample procedure appears below:

to drawH
 forward 100
 right 180
 forward 40
 left 90
 forward 50
 left 90
 forward 50
 back 100
 right 90
end

Logo is Fun!

Copy and paste the
Haunted House game code into Logo.  Hit enter; the screen gets very messy.  Type ct or cleartext to clean up the mess, then run the program by typing "HAUNTEDHOUSE" at the ? prompt. By the time of the midterm all students in this class should be able to write a program like this one.

To make changes to the haunted house game, type edall at the Logo prompt. Then "close and accept changes" and run the program again to see what effect your changes have had.

Repeat (optional)

One handy Logo instruction is repeat. This simply tells the computer to do the same thing over several times. For example, repeat 20 [forward 1] does the same thing as forward 20. What happens if you draw several copies of the procedure drawn by your design? Try typing repeat 5 [drawH].

(The grammar for the repeat function tells Logo to expect a number and a "list of instructions" after the word repeat. Whatever appears inside the []'s will be repeated the given number of times.)

When you repeat your design five times, does the turtle end in the same position it started in? If not, how many times would you have to repeat your design in order to bring the turtle back to its starting position? Make a guess, then experiment to see if you were right.

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 (Mac users scroll to the bottom of this page).