COMP203: Lecture 9


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.

Review of if

Last class introduced a command if which executed a list of instructions (its second input) if its first input was the word true.
to music.quiz
 print [Who is the greatest musician of all time?]
 if equalp readlist [John Lennon] [print [That’s right!] stop]
 print [No, silly, it’s John Lennon.]
end
Here the first input to if is the output of equalp and the second input to if is the list [print [That’s right!] stop]. This list contains two instructions: print [That's right!] and stop. (The Logo command stop requires no inputs and has the effect of making a procedure stop running. For more inforomation on stop see page 72.)

Choosing Between Alternatives

Let's think about procedure hi again. It would be nice if Logo responded the same way a human might in this conversation. For instance, it would be nice to have different responses to "great" and "lousy" after we ask "How are you?"
to hi
 print [Hi. What’s your name?]
 print sentence [How are you,] word first readlist "?
 if emptyp readlist [print [Cat got your tongue?]]
 print [Oh well, have a nice day!]
end
We could try using equalp the way we did in the quiz program in stead of emptyp. What happens when you run the following procedure? (Remember, you should either erase "hi or use the editor to define this new version of hi.)
to hi
 print [Hi. What's your name?]
 print sentence [How are you,] word first readlist "?
 if equalp readlist [lousy] [print [Gosh, I'm sorry to hear that.]]
 print [That's nice!]
end
In the new version of hi, if you tell Logo that you're feeling lousy it respons with:
Gosh, I'm sorry to hear that.
That's nice!
This is not much of an improvement over the version on page 5. What we'd like Logo to do is say "I'm sorry to hear that" in response to "lousy" and "That's nice!" in response to anything else. We could use a stop instruction as we did in the quiz procedure, but there is a better way. (The stop command is tricky to use; some students have reported that it stops not just the procedure you're in but all the procedures that are running.)

ifelse is a Logo command that requires three inputs -- a true/false result, a list of instructions to follow if the first input is true and a list of instructions to follow if the first input is false..

Erase or edit hi again and let's try using ifelse to improve it:

to hi
 print [Hi. What’s your name?]
 print sentence [How are you,] word first readlist "?
 ifelse ~
  equalp readlist [lousy] ~
  [print [Gosh, I'm sorry to hear that.]] ~
  [print [That's nice!]]
end
Notice how we used three tildes (~) to make the procedure easier to read. The instruction ifelse equalp readlist [lousy] [print [Gosh, I'm sorry to hear that.]] [print [That's nice!]] didn't fit all on one line. We also used indentation to indicate that the output of equalp and the two lists of print instructions were inputs to ifelse. Logo doesn't care if you do that; this is just something you can do to make your (and your grader's) life easier.

Create a plumbing diagram for the instruction starting with ifelse in this procedure.

Defining More Predicates

Recall procedure vowelp:
to vowelp :letter
 output memberp :letter [a e i o u]
end
Having the output of memberp be the input to output is very elegant, but is probably not the easiest thing for a beginning programmer. Now that we know how ifelse works we can write a simpler, less efficient version of vowelp which may be easier for you to understand:
to vowel2p :letter
 ifelse memberp :letter [a e i o u] [output "true] [output "false]
end  
Procedure vowel2p does exactly the same thing as vowelp; if the input is one of a, e, i, o or u, vowel2p outputs true. Otherwise the output is false.

Can you write a predicate lousyp which outputs true if its input is the word "lousy" and false otherwise?

If you finish lousyp early, take a look at this haunted house procedure to see what you can do with just ifelse and readword.

That's the Basics!

You should now know how to write procedures with inputs and outputs, use helper procedures, and choose between two actions in your procedures. That's all you need to know to write most programs; in particular, you can start your midterm project any time you like.

Optional Exercise

Write a game maze that describes a secenario, asks the user for inputs, and reacts according to those inputs -- as in the haunted house example. For example:
? maze
You are lost in a dark maze.  Will you go right or left?
left
You fell in a pit and died.  The end.
? maze
You are lost in a dark maze.  Will you go right or left?
right
You see a light ahead of you and walk toward it.  You are saved!
?
If you finish early, try adding more options -- perhaps the user has to make the correct choice twice in order to escape from the maze; perhaps you should write helper procedures for each location in the maze.

Logical Connectives

Earlier we used equalp to make our hi procedure respond more appropriately to user comments. Page 65 of Chapter 4 describes a predicate about.computersp that helps the computer determine the topic of a conversation.
to about.computersp :sentence
 if memberp "computer :sentence [output "true]
 if memberp "computers :sentence [output "true]
 if memberp "programming :sentence [output "true]
 output "false
end
Try it out!

This seems like a cumbersome way to "output true if one of the words in a sentence is computer, computers or programming and otherwise output false". One way to make this less cumbersome is to use the logical connective or.

Procedures and, or and not are predicates whose inputs are truth values (true or false). They don't work quite the same way in Logo as they do in English because in Logo, procedure names come before inputs. The English statement "2+2=4 and 3<7" becomes and equalp 2+2 4 lessp 3 7 in Logo. If this is confusing to you, try drawing a plumbing diagram for that Logo expression (which is not a complete instruction).

Normally, and and or require two inputs while not requires one. Try them out to see how they work!

? print and "true "false
false
? print and "false "false
false
? print and "true "true
true
? print or "true "false
true
? print not "true
false
? print not "false
true
? print and "true not "false
true
Predicates and and or can accept more than one input if we put parentheses around them. We can use this to rewrite about.computersp in a more natural form:
to about.computersp :sentence
 if (or (memberp "computer :sentence) (memberp "computers :sentence) ~
        (memberp "programming :sentence))~
    [output "true]
 output "false
end
For more practice with connectives, write a procedure comment.on.subjects that accepts a list of subjects as input and prints (displays) different messages for: