TEACH PROBLEMS1.ANS Aaron Sloman, Nov 97 Revised 27 Nov 2000 CONTENTS - (Use g to access required sections) -- Problem 1. A version of RESPOND -- Problem 2&3: a simple diary manager -- Tests for diary manager respond The following index of procedures was produced using the command ENTER indexify define CONTENTS - (Use gg to access required sections) define badword(word) -> bool; define respond(input) -> result; define start_data(); define what_appointment(day, date, time) -> what; define when_subject(month, subject) -> appointments; define duplicates() -> result; ;;; buggy version define duplicates() -> result; define change_appointments(date, num); define respond(input) -> result; -- Problem 1. A version of RESPOND ------------------------------------ ;;; Compare the following with your solution. ;;; You can compile these two procedures, then run the test examples ;;; given later. /* PROCEDURE: badword (word) -> bool INPUTS : word is a Pop-11 word, representing part of user input OUTPUTS : bool is a boolean (explained under PURPOSE) USED IN : Procedure respond, defined below CREATED : 23 Nov 1998 PURPOSE : Check whether word is one of the "bad" words in a list of words that might be used by someone who is feeling bad. If so bool is true, otherwise fale. TESTS: badword("fred")=> ** badword("unwell")=> ** */ define badword(word) -> bool; member(word, [ill bad unwell]) -> bool enddefine; ;;; Some notes on the definition of respond which follows. ;;; Note 1: the way the family case is handled here uses a multiple "or" ;;; condition. It could more elegantly be handled using a ;;; restriction procedure (e.g. a predicate isfamily, which ;;; recognises family members). ;;; Note 2: the problem as stated was ambiguous as to which of the ;;; conditions should take priority when more than one rule is ;;; applicable. The priority is implicit in the order in which ;;; the tests are done. The order could be changed. /* PROCEDURE: respond (input) -> result INPUTS : input is a list of words, representing user input OUTPUTS : result is a list of words to be printed out in response to input USED IN : a conversaiontal program CREATED : 23 Nov 1998 PURPOSE : To find an appropriate response for a given input, by matching the input against a sequence of stored patterns until one matches, which then determines the response to be created. TESTS: ( Note student program files should include sample output as well as tests.) respond([hello there sister])=> respond([you are often seen talking to me])=> respond([you are inferior to me])=> respond([hello dolly])=> respond([hello dolly hello])=> respond([the black cat sat on the black mat])=> respond([i really feel ill today])=> respond([testing testing hello])=> */ define respond(input) -> result; lvars list, rest, word; if input matches [ == mother == ] or input matches [ == father == ] or input matches [ == sister == ] or input matches [ == brother == ] then [tell me more about your family] -> result; elseif input matches ! [you ??list me] then [I always suspected spirits ^^list the prime minister] -> result; elseif length(input) < 3 then [please be more informative] -> result; elseif input matches ! [== ?word == ?word ==] then [please do do not repeat yourself] -> result; elseif input matches ! [??list feel ?word:badword ??rest] then [^^list feel better ^^rest] -> result; else [sorry I do not understand] -> result; endif enddefine; -- Problem 2&3: a simple diary manager -------------------------------- If you have already started using database facilities introduced in TEACH RIVER2 then you can tackle this problem. Define the following procedures which could be part of a diary manager which stores information about your appointments in the format [ [ ]