Previously we noticed that the use of a conditional like
if x = 10 then x=> endifdepended on the fact that "=" is a predicate, i.e. a procedure producing a boolean result. This result is left on the stack. You can think of a conditional imperative of the form
if <condition> then <action> endifas being roughly equivalent to something like the following:
1. Put value of <condition> on stack 2. If the top element of the stack is true perform the <action>, otherwise jump to after 'endif'.This means that if the expression between `if' and `then' produces no result, then an error will occur. For example, "3 = x" produces a boolean result, whereas the assignment "3 -> x" is simply an imperative, and does not, so:
if 3 -> x then x => endif; ;;; MISHAP - ste: STACK EMPTY (missing argument? missing result?)The portion of Pop-11 between `if' and `then' produced no result.
Strictly speaking, Pop-11 is defined so that anything other than false can be used as equivalent to true in a condition:
if 99 then 66 => endif; ** 66For this reason it is sometimes convenient to define a procedure so that it returns either false or some object it is discovered. For example, a procedure which searches down a list looking for a word, and if it finds it returns that word as its result, otherwise returns false:
define findword(list) -> result; lvars item; for item in list do if isword(item) then item -> result; return(); endif; endfor; false -> result; enddefine; findword([1 2 3 4 5 6]) => ** <false> findword([1 2 3 4 five 6]) => ** fiveThis might be used with the non-destructive assignment arrow thus:
vars w; if findword(list) ->> w then .... do something with w .... else report_failure(); endif;