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.
to closetozerop :number if lessp :number 0.1 [if greaterp :number -0.1 [output "true]] output "false endThis procedure illustrates three important points.
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 "falseGuess 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 endCan 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)) endOld 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 endCan 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.