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([_, _, X | Y]). append (L1, L2, L3) append ([], L, L). ?- append ([1], [2, 3], [1, 2, 3]). H = 1, T1 = [], L2 = [2, 3], T3 = [2, 3] ?- append ([1], [2], X). -check rules ?- append ([1], X, [1, 2, 3]). -check rules, use rule 2 ?- append (X, Y, [1]). -get both answers If you used append (X, Y, Z) would find all possible solution lists member (V, List) => is V a member of List? Don't have to state any rules about failure cases because
if |
Definitions: Lisp
Prolog [1,2,3] = list of elements [] = empty list ; = try query again |