Learning Logo – Part 4

Table of Contents

Chapter Four – Predicates

Key Ideas

A predicate is an operation whose output is always either the word true or the word false.
For example:

? print listp [ hello ]
true
? print listp "hello
false
? print listp [ hello ]
true
? print listp [ ]
true
? print wordp "hello
true

By convention, predicates often end in ‘p’ or ‘?’. Here’s an example of defining your own predicate using a built-in predicate:

to vowel? :letter
  output memberp :letter [a e i o u]
end

? print vowel? "i
true
? print vowel? "y
false

if is a command with two inputs. The first input must be either the word true or the word false. The second input must be a list containing Logo instructions. If the first input is true, the instructions are evaluated.

ifelse is a procedure that requires three inputs. The first input must be either the word true or the word false. The second and third inputs must be lists of Logo instructions. If the first input is true, the list in the second input is evaluated; otherwise, the list in the third input is evaluated. ifelse is unusual in that it can be used as a command or operation.

test is a command that takes one input that is either true or false. test stores its input in special variable that is local to the procedure from which test is invoked. iftrue (abbreviated ift) is a command with one input – a list of Logo instructions which are evaluated if the most recent test command stored true. iffalse (abbreviated iff) is similar except that the list of instructions are evaluated if the recent test was false.

and is a predicate with two inputs each of which must be true or false. The ouput is true if both inputs are true; otherwise, it is false.

or is a similar predicate whose output is true if either input is true.

not is a predicate with one input (true or false) whose output is the opposite of the input.

When ifelse is used as an operation, the output is the result of evaluating the list of instructions in either the second or third inputs depending on whether the first input was true or false respectively. For example:

? print sentence "It's ifelse 2=3 ["correct] ["incorrect]
It's incorrect

to abs :number
  output ifelse :number < 0 [-:number] [:number]
end

? print abs -7
7
? print abs 7
7

stop is a command that takes no inputs that finishes the evaluation of the procedure in which it is used. An output command also finishes the evaluation of the procedure in which it occurs. If you're writing an operation, which should have an output, you use output; if you're writing a command, which doesn't have an output, you use stop.

repeat is a command with two inputs. The first input is a non-negative whole number. The second input is a list of Logo instructions which will be evaluated the number of times given as the first input.

Questions

1) What type of operation always outputs either the word true or the word
   false?

2) Do names of predicates have to end in the letter p? Is it a good idea
   to name your predicates with names ending in the letter p? What would
   be another good way to name a predicate?

3) List eleven primitive predicates that are named in this chapter:

4) What is another way to say the same thing as the following?
   equalp "hello last [goodbye hello]

5) Fill in the blank so the command prints true:
   print memberp ______ [barnes noble]

6) Fill in the blank so the command prints true:
   print memberp _____ "chitty

7) Create a predicate, even?, that returns true if the input is even.

8) Write a procedure that will print the specified output when given
   the specified input:
   input: Brian
   output: [Hello Brian]

   input: [Brian Adkins]
   output: [Hello Mr. Adkins]

   input: [Brian Xavier Adkins]
   output: [Xavier is a nice middle name.]

9) Write integerp, as described on p. 66

10) Write the procedure converse that's described on the bottom of p. 67.

11) Read the section entitled "About Those Brackets" on the top of p. 69 again to
    make sure you understand it.

12) Which primitive predicates takes only one input?

13) Use the operation form of ifelse to output the input word backwards if it has
    an odd number of characters or output it normally if it has an even number of
    characters.

    to evenword? :value
      output equal? 0 (remainder count :value 2)
    end

    to condreverse :value
      output ifelse ______________________________________________
    end

    print condreverse "hello   -> olleh
    print condreverse "hi      -> hi
    print condreverse "daddy   -> yddad

14) How are output and stop similar? How are they different?

15) Write a predicate, palindrome?, that outputs true if a word is a
    palindrome. A word is a palindrome if it is the same forward and backward.

    print palindrome? "hello  -> false
    print palindrome? "mom    -> true
    print palindrome? "wow    -> true

16) Write a procedure that uses the palindrome? predicate you wrote in #15 to
    accept a list and output a list containing only the words that are
    palindromes.

    print palindromes [hello mom daddy wow doogood] -> mom wow doogood

Tags: