On page [*] we showed the structure of Eliza as a repeated cycle of actions:
In POP-11 the words repeat and endrepeat are used to enclose a series of commands to be repeated. Thus
repeat [Hi there] => endrepeat
prints out
** [Hi there]
over and over again, until the program is interrupted (on most systems this is done by holding down the CTRL key and typing the character `c'). A repeat loop forms the outline of the Eliza program.
We can call up answer to generate a response, but we need a command that will accept words typed by the user and pass them to answer. The POP-11 procedure readline does this. If we insert readline into the program, then when it is called it displays a query on the screen and waits for the user. Whatever the user types (terminated by pressing the RETURN key) is formed into a list which is returned as the result of readline.
Here is a procedure to control Eliza. It repeatedly calls readline to receive input from the user, which is stored in the variable query. The value of query is passed to answer, which returns a response, to be printed out by =>>:
define respond(); vars query; repeat; readline()->query; answer(query) =>> endrepeat; enddefine;
The procedure will work; the only problem is that it can only be halted by interrupting the program. The built-in procedure quitif evaluates the expression query = [bye] and, if it is <true>, then breaks the repeat loop:
define respond(); vars query; [Type bye to finish the consultation.]=>> repeat readline()->query; quitif(query=[bye]); answer(query)=>> endrepeat; enddefine;
We can now combine respond with the welcome procedure defined in chapter 1 to create the first version of the Tourist Guide:
define guide(); welcome(); respond(); enddefine;
The guide procedure starts with the welcoming message and then calls respond. This in turn calls readline and answer:
guide();Welcome to the London Tourist Guide
This guide can answer your queries about landmarks ,
attractions and events in London
Please type your query in English , for example :
How do I get to the National Gallery ?
Press the RETURN button to finish a query
Type bye to finish the consultation.
? Where can I go for a trip on the river?
You can go on river trips from Tower Bridge
? How can I get to Trafalgar Square?
I do not know the way to Trafalgar Square
? bye