[Next] [Up] [Previous]
Next: A Simple Eliza-Like Tourist Up: Appendix: A Program to Previous: Pattern Matching

Deciding What to Do

Our Eliza program will need to take decisions. If the input matches one type of pattern, it will need to produce one output, and if the input matches another type, it will need to produce a different output. POP-11, like most programming languages, allows us to write instructions to make decisions. The ability to follow different paths under different conditions is at the heart of programming languages.

Choices in POP-11 are produced by conditional commands, signalled by the word if. Here is an example:

vars input;
[is your name eliza?]->input;
if input matches [== eliza ==] then
[we were talking about you, not me] =>>
endif;

we were talking about you, not me

Between if and then is the expression

input matches [== eliza ==]

When the computer evaluates this, the result is <true>, so POP-11 carries out the instructions between the word then and the word endif. If the value of the expression had been <false>, then the instructions would have been ignored.

The word else can be used to introduce instructions that are to be carried out when the expression following if turns out to be false. The general form of the if...then...else statement is

if <expression> then
<instructions to do when the expression is true>
else
<instructions to do when the expression is false>
endif;

The else is optional; if it is omitted, then no action is taken if the expression is <false> and POP-11 continues with the next command after the if statement.

For Eliza we need to test the input against a series of patterns until a match is found, at which point the program constructs an appropriate response. If there is no match, then it gives a general-purpose response. What we want is a statement of the form ``If the first condition is true then carry out the first action, otherwise if the next condition is true then carry out the next action, and so on. If no condition is true then carry out a default action.'' In POP-11 this is written as a series of

if then elseif

commands, for example

    if input matches [== eliza ==] then

        [we were talking about you, not me] =>>

    elseif input matches [??x is ??y] then

        [suppose ^^x were not ^^y] =>>

    elseif input matches [== computer ==] then

        [do machines worry you] =>>

    else

        [please go on] =>>

    endif;

POP-11 has many operators that return boolean values, and indeed you can write your own procedures to do so, so the if statement is extremely powerful.


[Next] [Up] [Previous]
Next: A Simple Eliza-Like Tourist Up: Appendix: A Program to Previous: Pattern Matching

Cogsweb Project: luisgh@cogs.susx.ac.uk