List Processing in Prolog

 

 List Iterators:
 [Head | Tail]  get the head and tail of the list

 [First, Second | Rest]  get the first two elements of the list

 [First, Second | _ ] get the first two elements and not care 
 about the rest of the list

 Examples:
 P([1, 2, 3, 4]).
 ?- P([X | Y]).
 X = 1, Y = [2, 3, 4]

 ?- P([_, _, X | Y]).
 X = 3, Y = [4]

 append (L1, L2, L3)
 L1 + L2 = L3.

 append ([], L, L).
 append ([H | T1], L2, [H | T3]) :- append (T1, L2, T3)

?- append ([1], [2, 3], [1, 2, 3]).
 YES
 -how does system know this?
 -matches facts to query

 H = 1, T1 = [], L2 = [2, 3], T3 = [2, 3]
 is this true?  YES
 -append ([], [2, 3], [2, 3]).
 -recursive call check against facts

?- append ([1], [2], X).   -check rules
 H = 1, T1 = [], L2 = [2], X = [1 | T3]
 now solve append ([], [2], [2]).
 X = [1, 2]

?- append ([1], X, [1, 2, 3]).   -check rules, use rule 2
 H = 1, T1 = [], L2 = X, T3 = [2, 3]
 -now solve append ([], L2, [2, 3]).
 -check rule #1  L2 = [2, 3]

?- append (X, Y, [1]).    -get both answers
 -check rules use #1  X = [], Y = [1]

?-;  -try again
 -use #2 [H | T] = X, L2 = Y, H = 1, T3 = []
 -so [1, T1] = X
 -solve the if append (T1, L2, []).
 -use #1 T1 = [], L2 = []
 X = [1, []], L2 = [], H = 1, T3 = []
 -return YES X = [1], Y = []

 If you used append (X, Y, Z) would find all possible solution lists

 member (V, List) => is V a member of List?
 member (X, [X | _]).
 member (X, [_ | Y) :- member (X, Y).

 Don't have to state any rules about failure cases because if 
 there is no rule for the case then it automatically fails.

 Definitions:

 Lisp                    Prolog
 cons (a . b) <=> .(a,b)

 [1,2,3] = list of elements

 [1, bob, joe] = multitype list

 [] = empty list

 [1] = .(1,[])

 [2,1] = .(2, .(1,[])) 

 _ = don't care for a list element

 + = string concatenation

 ; = try query again