COMP203: Lecture 1


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.

About The Class

Q: Why bother teaching programming?

A: So that your students learn and practice linear, rational thought. These skills may also be learned in a geometry or logical reasoning course. Computer programs provide more feedback than geometric proofs -- if you don't get them right they don't work! Also, skill with computers helps land higher paying jobs.

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 people realized computers were going to be important.

Logo is still the best language I know for teaching programming. It provides instant, sometimes visual feedback, and it 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 program Maple to approximate.

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 also use Maple or another computer algebra system in a job after graduation.

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.

About the Textbook

The Logo textbook is available free online, and is in print. Most other Logo texts are out of print. That's why we're using this textbook.

It's a really great textbook! Unfortunately, it's written for people who already know something about programming and want to have fun learning more. It's rough going for people who don't know much about programming to begin with. The purpose of these notes is to supplement the explanations in the book. If you are having trouble understanding an explanation in the book, let me know and I'll try to find time to write something to supplement the book's explanation.

Chapter 1: Exploration

This chapter is a warmup -- the point is to turn Logo on and see what it can do. If you like, type in the instructions that appear at the bottoms of pages 2 and 3. Change some of the numbers and see how the computer's behavior changes. Change the word "Hi. What would happen if you changed some of the other words?

Understanding Commands

You can find out what a word means to Logo by looking it 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.

This information and the experience of running the command might be enough for you to conclude that:

repeat 50 [setcursor list random 75 random 20 type "Hi]
tells Logo to type the word "Hi" at random locations in the text window 50 times.

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.

Commands and Drawing

The instructions on pages 2 and 3 are called "commands". You type them into Logo and something happens. 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 for a few minutes -- it's like having a little geometry Etch A Sketch on your computer screen! To erase the screen, type clearscreen. Unfortunately, Logo doesn't keep track of the commands you've given; if you come up with a design you like, take notes on it!

Procedures

So far we've learned a lot about Logo but we haven't done any actual programming. 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
Parts of this procedure are somewhat complicated. For now, let's focus on procedures in general and try not to think too hard about this particular example.

When you want to define a procedure, you type the word to to let 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 that is the name of the procedure. (If you want to use more than one word to name your procedure, just don't put spaces between the words!)

After you type in the name of the procedure, 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 you've finished the procedure. If everything was typed correctly, Logo will then return you 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 took notes on a design you liked, write a Logo procedure to create that design! If you didn't take notes, type in the procedure below:

to design
 left 90
 forward 100
 right 90
 forward 200
end
Once it's typed in, you can run this procedure using the command design.

Repeat

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 [design]. 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?

Editing a Procedure

You couldn't see what pattern your instructions were going to make as you were typing them in, and if you made any mistakes you had no way to correct them. The newest version of Logo has a fairly nice editing tool that lets you make changes to your procedure after you've typed it in. To make changes to a procedure named design you would use the command edit "design. (If you forget the quotation marks, Logo will first run the procedure design and then give you an error message.)

When you edit your procedure, the line that starts with to must always be first and the line end must always be last. You can add, delete or change anything you like between those two lines. When you are satisfied with your changes, choose "Close and accept changes" from the File menu. (If you decide you don't like the changes you've made, choose "Close and revert changes" instead.) Run the revised version of your procedure by typing in the procedure name, the same way you did before.

Another nice feature of the editor is that it displays your procedure in its entirety -- this is especially useful if you are drawing pictures and therefore have a small text window. Once you're editing the procedure you can select the text of the procedure using your mouse, copy it, and paste it into a Notepad document, email message, or any other computer program. This gives you a quick and easy way to save your work. Choose one of these methods to save your design procedure now; we'll return to it next class.

The save command

Page 8 of the text describes a command save that will save your work. Try telling Logo save "mystuff. (Here mystuff is a file name and has quotation marks in front of it, just as the procedure name design had quotation marks in edit "design.)

This either causes Logo to save your work in a file on your computer or generates an error message when Logo tries to write to a protected location on your computer.

For BSU students, a better command is:
save "W:/Private/mystuff.txt Try this now.

This command should cause Logo to write a file to your "W drive". This folder should be available to you every time you log into a BSU workstation. Find the file mystuff.txt and open it. It has all the procedures you've defined in this Logo session and they're not in order. The save command is good for making backups of your work, but it's not as useful for turning in homework as you might like.