next up previous
Next: Operators Up: Prolog Terms Previous: Compound Terms

Lists

Lists are a special class of compound term. They are represented in Prolog as either:

For example the list containing the sequence of elements a, b and c is represented by the term .(a,.(b,.(c,[]))). Since this is hard to read, you can use an alternative notation. Simply write the list as a sequence of elements separated by commas and enclosed in square brackets. The previous example could we rewritten using this notation as [a,b,c].

You can confirm this by giving the following goals to Prolog:

| ?- functor(.(a,.(b,.(c,[]))), Functor, Arity).
     Functor=(.)
     Arity=2 
yes
| ?- functor([a,b,c], Functor, Arity).
     Functor=(.)
     Arity=2 
yes
| ?- .(a,.(b,.(c,[]))) = [a,b,c].
yes



Dave Stuart Robertson
Tue Jul 7 10:44:26 BST 1998