COMP203: Lecture 11

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.

Logical Connectives

Study 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 we need to reapply Newton's method to an estimate. 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 the or predicate with multiple inputs.
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
Can you write a program integersols that accepts the coefficients a, b and c of a quadratic polynomial ax2 + bx + c as input and then outputs any integer solutions to the equation ax2 + bx + c = 0?

You should probably do this in stages using helper procedures. Start by writing a procedure which outputs one result of applying the quadratic formula, then write procedures to check for a negative discriminant and to ensure that the output is an integer (can you combine these two checks by using and or or?  Should you?)  An interesting extra credit project might be to write a procedure to find the integer solutions to any polynomial equation.