User defined predicates

Defining predicates

In constructing tests so far, we have made use of primitive predicates. But there is no need to rely solely on primitive predicates in Logo. You can also define new predicates. And the only difference between defining predicates and defining Logo procedures generally is that in defining a predicate you must take care to ensure that the procedure will only ever output "true or "false.

Suppose, for example, you would like to define a new predicate vowel? so that you could rephrase the first of the three tests above as:

if vowel? first :word [op "l' ].

One possible definition is quite straightforward:

to vowel :letter
if member? :letter [a e i o u] [op "true] [op "false]
end
You can see very clearly that this procedure literally outputs either the word "true or the word "false. To that extent, it demonstrates well how a predicate works. But, in fact, it isn't the neatest or most efficient of solutions.

Notice that the definition of vowel? makes use of the primitive member? Notice too that when the expression member? :letter [a e i o u] outputs a "true or a "false to if, if must run either one or the other of the instruction lists [op "true] or [op "false]. Now, as things are set up, if, of necessity, runs [op "true] - the first instruction list - if it receives "true and [op "false] - the second instruction list - if it receives "false. Surely, there is some pretty wasteful activity here. All that our new predicate vowel? really needs to do is to pass on directly the output of the expression member? :letter [a e i o u]. There is no point in getting an if to organize a repetition of the process.

A better definition of vowel? which takes advantage of this idea - i.e. which takes immediate advantage of the output of member? - is the following:

to vowel? :letter
op member? :letter [a e i o u]
end


Ron Brasington
Department of Linguistic Science
The University of Reading
Reading
UK

E-mail: ron.brasington@rdg.ac.uk