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.
? letternum 5 "competent Letter number 5 in the word competent is e. ? letternum 3 "cat Letter number 3 in the word cat is t. ? letternum 4 "Heidi Letter number 4 in the word Heidi is d.We started with the line:
to letternum :number :lettersWhat questions do you have about this task?
Knowing procedures which might be useful is a matter of gaining experience with the language. When you see a Logo instruction, try to remember what the procedures in it do, adding to your knowldege of what is possible in Logo.
Luckily, you can write notes to yourself (and others) inside your procedures. The procedures to draw my initials have comments like this, as did the boat procedure in the procedures worksheet. Logo will ignore anything after a semicolon (;), such as notes on which part of the procedure draws the sail and which part draws the hull. Using comments in your more complicated homework assignments can be very helpful to the person grading them and is highly recommended.
Two other useful vocabulary words are superprocedure and subprocedure. A subprocedure is one that's used by another procedure, like triangle is used by boat. A superprocedure is a procedure that uses another procedure; boat is a superprocedure of triangle. Notice that this is a relationship between procedures similar to < or subset;. Procedure boat may be a superprocedure for one procedure and a subprocedure for another. In fact, we'll eventually see examples of procedures that are their own subprocedures (and superprocedures).
? show readlist Hello, how are you? [Hello, how are you?]
We've seen an example of this in procedure hi on page 7:
to hi print [Hi. What is your name?] print sentence [How are you,] word first readlist "? ignore readlist print [That's nice.] end
The operation readlist is very handy, but we can only use its output once. The next thing we'll do is look at a common problem with readlist and see how using a variable can solve it.
What happens when you try to write this converse procedure, shown on page 45 of the text?
? converse Please type your full name. Brian Harvey Your first name is: Brian Your last name is: Harvey ?What goes wrong? Why?
Of course we want to store it in a variable, but how? One handy trick is to use the name as the input to a procedure. The procedure then stores the name in a variable and can pick it apart at leisure:
to converse print [Please type your full name.] conversehelper readlist endThis version of converse asks the user for their name, then gives the name as input to procedure conversehelper. Procedure conversehelper is then very simple to write.
to conversehelper :name print sentence [Your first name is:] first :name print sentence [Your last name is:] last :name endThis is an example of storing a value in Logo by using that value as the input to a procedure.
to increase :number make "number (:number + 1) print :number end
? increase 5 6 ? increase 1 2In algebra, we never say "x = x + 1". In computer programming, we replace a value by a larger value so often that some languages have special abbreviations to do exactly this. What procedure increment says to Logo is "look up the value stored in the variable named number, add one to it, and store the result in the variable named number." The old value in number is erased and replaced by a value one higher.
Procedure make requires two inputs: a variable name and a value. Using the analogy from last class, make goes to the locker whose name matches its first input, removes anything that's in the locker, then stores its second input in the locker.
We can use make to solve the problem with procedure converse in a way you might like. It's not elegant, but we can simply create a variable to store the output of readlist:
to converse print [Please type your full name.] make "fullname readlist print sentence [Your first name is:] first :fullname print sentence [Your last name is:] last :fullname endWe'll learn next class what problems there are with this approach and how to solve some of them.