For our final example, I will look at lists again, and show how they can be combined with the other things you have learnt. During the 17th Century, ``canting academies'', or books of criminal slang were popular. One such, available in the Bodleian Library, and written by Oxford author Richard Head - best known for his debased and much-banned autobiographical The English Rogue - is entitled just that: The Canting Academy. It contains a number of entries which will tell you for example that the slang term for a sheep was ``bleating cheat'', and for prison, ``queer ken''.
I have copied part of The Canting Academy into the file
HEAD, which defines the predicate
dictionary(Normal,Slang)
. Now the
obvious way to write such a predicate is to use atoms: for example,
if we were converting between English and French, and ignoring niceties
like gender:
dictionary( chat, cat ). dictionary( chien, dog ).However, because many of Head's entries consist of more than one word, it is better to use lists, which are the ideal way to store variable-length sequences.
To see how these are used, have a look at, and experiment with, the
files HEAD and READER. These contain the
dictionary, together with a simple program that reads normal words or
phrases from the terminal and looks up the slang equivalent. These
combine all the features of Prolog you have seen so far, including
lists, command predicates, logic predicates, input, and output. Unlike
in Lesson 7, I have not used read
and
write
for input and output: as I said
then, these are not very convenient. Instead, I have written my own
input-output predicates. By looking at the program, and the predicate
names, see whether you can work out which these are, and how they are
used.