We can now make a small procedure that produces answers to questions about London. The procedure, which we shall call answer, will have one argument, which will be the query, and will return one result, its response. It will use variables, pattern matching, and conditional instructions. Here is the complete procedure:
define answer(query) -> response; vars x; if query matches [== changing of the guard ==] then [The changing of the guard is at Buckingham Palace] -> response; elseif query matches [== politicians ==] or query matches [== politics ==] then [You can hear political opinions at the Houses of Parliament or at Speakers Corner in Hyde Park] -> response; elseif query matches [== river ==] then [You can go on river trips from Tower Bridge] -> response; elseif query matches [thank you] then [You are welcome] -> response; elseif query matches [== get to ??x] then [I do not know the way to ^^x] -> response; else [Sorry, I do not know about that] -> response; endif; enddefine;
As you can see, this procedure just tries to match the query against different patterns, and if one of the matches succeeds, it returns a reasonably appropriate result. We have also sneaked in a new operator, which you have not met before, but its use should be fairly obvious. The or operator takes two boolean values, one from each side of it (as + does with numbers and matches with lists) and produces a boolean result. If the expressions on both sides of or evaluate to <false>, then the result is <false>, but if either (or both) is <true>, then the result is <true>. Thus the instruction in the second conditional statement gets executed if either `politicians' or `politics' appears in the query. Here is what happens when we try the procedure on a few examples:
answer([Where can I see the changing of the guard?]) =>> The changing of the guard is at Buckingham Palace answer([How do I find out about British politics?]) =>> You can hear political opinions at the Houses of Parliament or at Speakers Corner in Hyde Park answer([How do I get to Islington])=>> I do not know the way to Islington answer([thank you]) =>> You are welcome
But the procedure suffers from the usual Eliza limitations:
answer([Is the river Thames very polluted these days?]) =>> You can go on river trips from Tower Bridge
Still, there is the making of a useful procedure here.
One more useful POP-11 procedure is oneof, which selects at random an element from the list given as its input. It can be used within answer to add variety to the responses:
else oneof([[Sorry, I do not know about that] [I have no answer to that question] [Please ask me only simple questions]])-> response;
To finish this chapter, we shall tie together the welcome and answer procedures into one package.