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.
When you've finished, define and run the design program below. Does it work? If not, why not? If so, and if you have time, can you change it to make a design you prefer? This would be a good time to try out the command setpencolor!
to design repeat 10 [right 36 pentagon 80] end
Two other useful vocabulary words are superprocedure and subprocedure. A subprocedure is one that's used by another procedure, like pentagon is used by design. A superprocedure is a procedure that uses another procedure; design is a superprocedure of pentagon. Notice that this is a relationship between procedures similar to < or ⊂. 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. We'll see how to use variables to get around this weakness in readlist.
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?
? reportroot Type in a number. 9 The square root of 9 is 3 or -3. ? reportroot Type in a number. 25 The square root of 25 is 5 or -5. ?
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 end
Notice that in this version of procedure converse we don't know what value is stored in variable name when the procedure starts, and that the variable changes value midway. With this size procedure, that's not confusing to us. In a larger program this could be a source of confusion; see the example below.
to confuse local "num print [Type in a number] make "num readword make "num 3 * :num make "num :num - 5 make "num :num * 3 make "num :num/4 make "num word :num "dilly print :num endThis is a silly example; a more serious example might involve a bank balance, interest payments, fees, withdrawals and deposits. It's very important that programs like that work correctly, so their code should be as easy to proofread as possible!
Some programmers practice functional programming, a technique in which variables never change value mid-procedure. Instead, subprocedures such as conversehelper are used. Logo is a good programming language in which to practice functional programming.
Operations provide information to other procedures by outputting data. The logo command output can only be used in a procedure. It requires one input value, and when it's evaluated it ends the procedure it's used in and causes that procedure to output that input value.
to hyphenate :name1 :name2 output (word :name1 "- :name2) endor:
to tan :angle output quotient sin :angle cos :angle end to hypotenuse :leg1 :leg2 output sqrt (:leg1 * :leg1 + :leg2 * :leg2) endWrite a Logo procedure cube which accepts a number as input and outputs the cube of that number.
? print cube 2 8 ? print cube 3 27 ?Notice that because cube is an operation it produces an output which must then be input to print. This is very important! If we want to use our procedures tan or cube in calculations (e.g. of a side length), they must output a value which can be used as input to sum, product, forward, etc. Displaying the value on the screen is no help to forward.
Write down your favorite mathematical function on a piece of paper. Then try to write a logo program which accepts an input and outputs the result of applying that function to the input.
When you've finished, turn to help your neighbor. When both of you have finished, copy each others' functions and use Logo to compose the two functions. You should be able to do this using a command like:
? print function2 function1 7 2.76534619035 ?Can you write a procedure whose output is the derivative of your favorite functin?
Can you and your partner write a procedure to find the derivative of your composition of functions? Recall that: