% define a predicate app(Xs,Ys,Zs) % <=> Zs contains all elements of Xs and Ys in the same order. % e.g., app([1,2],[3,4],[1,2,3,4]) is true %app([],Ys,Zs) :- Zs=Ys. app([],Ys,Ys). app([X|Xs],Ys,[X|Zs]) :- app(Xs,Ys,Zs). % musterorientiert: falls das erste Argument eine Liste ist, dann passt % hoechstens eine der beiden Klauseln % % ?- app([1,2],[3,4],[1,2,3,4]). % |- % ?- app([2],[3,4],[2,3,4]). % |- % ?- app([],[3,4],[3,4]). % |- % ?- . % % Suchraum: 1-elementig % Berechnung: deterministisch % Add an element at the end of a list: add_list(L, E, LwithE) :- app(L,[E],LwithE). % Last element E of a list L: last(L,E) :- app(_,[E],L). % Is E an element of a list L: elem(E,L) :- app(_,[E|_],L). % deletes an occurrence of an element E in a list Xs, Ys is the remaining list delete(Xs,E,Ys) :- app(L1,[E|L2],Xs), app(L1,L2,Ys). % sublist(Xs,Ys) <=> Xs is a list occurring in Ys sublist(Xs,Ys) :- app(_,L2,Ys), app(Xs,_,L2).