Best viewed in 24pt and full-screen
next up previous
Next: From Prolog lists to Up: Representation of lists Previous: Representation of lists

Prolog lists

When lists are represented by Prolog lists, a cons is represented by the binary functor ./2 (noted [ | ]) and the empty list is represented by nil (noted []). For instance, cons(1,cons(2,cons(3,nil))) is represented by 1.2.3.nil (noted [1,2,3]).

Prolog list representation is used in the classical list manipulation predicates: ``concatenate'', ``naïve reverse'' and ``concatenate 3 lists'':

conc([], Y, Y).
conc([A|X], Y, [A|Z]) :- conc(X, Y, Z).

nrev([], []).
nrev([A|X], Y) :- nrev(X, RX), conc(RX, [A], Y).

conc3(A, B, C, ABC) :- conc(B, C, BC), conc(A, BC, ABC).

A mode is a specification of a particular input convention. A mode expression is a literal in which terms are replaced by + (always instantiated), - (never instantiated) and ? (do not know). Among the three predicates above, conc is the only one which can operate in every mode. nrev and conc3 enter a loop for modes nrev(-,+) and conc3(-,-,-,+). So conc can be used to split a Prolog list, whereas conc3 cannot. The order in which lists A, B and C are composed in conc3 is arbitrary. However, for every composition order there is a mode which does not work.

Note that predicate conc can concatenate a list to itself.

twice(L, LL) :- conc(L, L, LL).


next up previous
Next: From Prolog lists to Up: Representation of lists Previous: Representation of lists

Olivier Ridoux
Fri Mar 6 11:12:37 MET 1998