COMP203: Lecture 10

ifelse and Logical Connectives

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.

Vocabulary Quiz Today

Programming with if

(Read this when you finish the quiz.)

This example from the book uses if to write a predicate that checks whether a phrase (list) is about computers.

to about.computersp :phrase
 if memberp "computers :phrase [output "true]
 if memberp "programming :phrase [output "true]
 if memberp "Logo :phrase [output "true]
 output "false
end
We can test this out by sending its output to print:
? print about.computersp [Computers are great!]
true
? print about.computersp [7 8 9 10]
false
but it's more fun and interesting to use it in a program.
to chat
 print [What do you think of COMP203 so far?]
 if about.computersp readlist [print [Yeah, computers are neat!] stop]
 print [Interesting.  What do you think of computers?]
end
Try it out! What happens if you respond with The computer is my friend.? Is this what should happen? Change the predicate about.computersp so that chat responds correctly to this statement.

Try it yourself!

Write a procedure which accepts a number as input and then prints "even" if the number input is even and "odd" if it's not even. There are several methods for doing this -- which do you think is best?

If you finish early, think about how you could use the techniques used in the about.computersp predicate to improve your quiz program:

? writers.quiz
Who is the greatest writer ever?
Tolkien
Yes, that's right!
? writers.quiz
Who is the greatest writer ever?
J. R. R. Tolkien
Yes, that's right!
? sports.quiz
What is your favorite baseball team?
Red Sox
Yeah, me too!
? sports.quiz
What is your favorite baseball team?
Boston Red Sox
Yeah, me too!
? sports.quiz
What is your favorite baseball team?
Yankees
Seriously?
?

If you find you have to hit enter several times to get Logo to respond to you, you probably have a readlist instruction in the wrong place.

Logical Connectives

Study and experiment with the predicate closetozerop below. What does it do?
to closetozerop :number
  if lessp :number 0.1 [if greaterp :number -0.1 [output "true]]
  output "false
end
This procedure illustrates three important points.
  1. A predicate can output the words true or false, not just the output of another predicate.
  2. An if command's list of instructions to follow can include another if command. We call these nested if commands.
  3. This is a confusing and clumsy way of finding out if a number is between -0.1 and 0.1.
This procedure would be useful to us when checking to see if Newton's method had found a number close enough to the x-intercept. How can we make it less confusing and clumsy?

What we really want to ask is whether the input is less than 0.1 and greater than -0.1. Does Logo know the word and? Find out by using help.

Yes! and is a procedure like sum which accepts two or more inputs. Procedure and outputs "true only if all its inputs are true and "false otherwise.

print and "true "true
print and "true "false
print and "false "true
print and "false "false
print (and "true "true "true)
print (and "true "true "true "true "false)
So we can rewrite closetozerop as follows:
to closetozerop :number
  output and (lessp :number 0.1) (greaterp :number -0.1)
end

Test this procedure with some values. Does it do what you expect it to?

Write a procedure which accepts a number as input and outputs true if the number is positive and odd, and false otherwise. (Scroll down to find a copy of procedure oddp.)

Since Logo knows the word and, we expect it to know or as well. It also knows not. Try to predict the result of running the following instructions:

print or "true "true
print or "true "false
print or "false "true
print or "false "false
print (or "true "true "true)
print (or "true "true "true "true "false)
print not "true
print not "false
print not or "true "false
Guess what the result would be of running the following instruction, if vowelp was defined as in lecture 8.
print not or (vowelp "a) (vowelp "b)

Below is an example of how we might use not to write a procedure to check whether a number is even.

to oddp :num
  output equalp (remainder :num 2) 1
end

to evenp :num
  output not oddp :num
end
Can you rewrite evenp so that it still uses not but doesn't need to use oddp as a helper procedure? How many different ways can you think of to write evenp?

Write a procedure integerp which outputs "true if its input contains no decimal point and "false if the input contains a decimal point. How can the procedure not help you do this?

Below are samples of how and and or might appear in a program. Notice that a tilde (~) has been used to tell Logo to continue reading instructions on the next line.

.
.
.
  if and (about.computersp :response) (about.mathp :response) ~
     [print [I think you would like COMP203.]]
.
.
.
When you use and and or, remember that Logo expects the input to come after the procedure name!
.
.
.
  if or (already.guessedp :letter) (vowelp :letter)
     [print [Please choose a consonant which hasn't yet been guessed.]]
.
.
.
Both and and or can accept multiple inputs. Study the example below, then rewrite about.computersp (from the start of this lecture) using or and parentheses.
to about.mathp :statement
 output (or (memberp "math :statement) (memberp "algebra :statement) ~
            (memberp "geometry :statement))
end
Old version:
to about.computersp :phrase
 if memberp "computers :phrase [output "true]
 if memberp "programming :phrase [output "true]
 if memberp "Logo :phrase [output "true]
 output "false
end