<< >> Up Title Contents

3.4. Final Programming Exercise

Write a Prolog program in which you type in an English sentence and Prolog replies with another sentence that is an altered version of the one you have typed in. For example, the program might produce an interaction like the following:

You: you are a computer
Computer: i am not a computer

You: do you speak french
Computer: no, i speak german

It is easy to write a program to do this by simply following these steps:

1. accept a sentence that is typed in by the user
2. change each 'you' in the sentence to 'i'
3. likewise, change any 'are' to 'am not'
4. change 'french' to 'german'
5. change 'do' to 'no'
6. add a comma and a space after 'no',

You should take the input to be a list; so that the interaction goes:

| ?- reply([do, you, know,french]).
no i know german
yes


This program is just a variant of the one we constructed in class to translate English into French, so you can model this program on that one.

A difference between this program and the French translator, however, is that here 'reply' is a one-place predicate. Prolog's answer has to be produced using the write/1 predicate. Note that the Prolog response does not contain any brackets or commas; you therefore will need to write a (recursive) predicate which will 'write' the items on a list one by one, with a space between each one, until it reaches the end of this list and stops - and integrate it into your program.


<< >> Up Title Contents