So far we have worked only with single pieces of data, or atoms. Lists give us more power and flexibility. In PROLOG, a list is enclosed in square brackets and its elements are separated by commas. Lists naturally model sentences, because we do not know how many words (atoms) will be in a sentence (list). The imperative grammar I wrote deals with sentences that way. Some examples of queries using lists follow:
append([the],[quick,brown,fox],[the,quick,brown,fox]).The last two examples use the predicate (or rule) append. Append tells whether or not the first two lists can be combined to form the third. Append is built into Gnu PROLOG and QUINTUS PROLOG, but must be defined under certain other implementations of the language. For those systems, Dr. Dougherty defines it thus:
append(,L,L). append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).I cannot fully explain this definition, so if your version of PROLOG doesn't define append, simply cut and past this in.
One of the most powerful features of almost
any language that supports it is recursion, and PROLOG does support
it. For examples of recursion, a simple definition of a noun phrase
will serve well.
nounp(X) :-noun(X). nounp(X) :-append(A,B,X),det(A),nounp(B).Other elements of a grammar could be defined recursively as well, but this code fragment says that a noun phrase is a noun preceded by any number of determiners (articles, adjectives, etc.). Note that in the second line, append precedes the other two statements. This forces the program to break the phrase up before trying to choose words that fit the phrase. I will say more about this under PROLOG Pitfalls.