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.
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 endWe can test this out by sending its output to print:
? print about.computersp [Computers are great!] true ? print about.computersp [7 8 9 10] falsebut 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?] endTry 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.
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.
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 or and parentheses.
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 end