COMP203: Lecture 6

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.

Turn in plumbing diagrams for Assignment 2

Procedures

Reading: pp. 49-54

Warmup

Write a procedure pentagon which accepts one input, a side length, and draws a pentagon with that side length.

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

Programs, Superprocedures and Subprocedures

When you write a procedure like boat or design, you're writing a Logo program. A program is a collection of procedures working together to perform a specific task like drawing a picture or playing tic-tac-toe.

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

Variables and readlist

We've seen the Logo operation readlist which outputs whatever is typed at the keyboard, in list format.
? 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?

Bonus Problem

If you have time, try writing the program reportroot described below. Will you use readword or readlist to write this program?
? 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.
?

Subprocedures and Storage

The most common problem with writing the converse procedure is that Logo forgets the last name as it displays the first name on the screen. How can we store the last name for later use?

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
end
This 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
end
This is an example of storing a value in Logo by using that value as the input to a procedure.

Changing the value of a variable

You've stopped thinking like a mathematician and started thinking like a programmer when the operation below makes sense:
to increase :number
  make "number (:number + 1)
  print :number
end
? increase 5
6
? increase 1
2
In 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.

So what?

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

Functional Programming

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
end
This 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.

Writing Operations

So far all the procedures we have written have been commands, not operations. What do we need to be able to do with Logo in order to write an operation?

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.

For example:

to hyphenate :name1 :name2
  output (word :name1 "- :name2)
end
or:
to tan :angle
  output quotient sin :angle cos :angle
end


to hypotenuse :leg1 :leg2
 output sqrt (:leg1 * :leg1 + :leg2 * :leg2)
end
Write 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.

Practice with Operations

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:

(f g)'(x) = f'(g(x)) g'(x).

Scope of Variables

Things to think about for next class: Read the textbook starting from page 49 to learn some of the answers to these questions.