pref(L, P) :- append(P, _, L). suf(L, S) :- append(_, S, L). lst(L, Y) :- append(_, [Y], L). memb(Y, L) :- append(_, [Y|_], L). selct(Y, L, R) :- append(P, [Y|S], L), append(P, S, R). nth(N, L, Y) :- append(P, [Y|_], L), length(P, N0), N is N0+1. app( [], L2, L2 ). app( [X|L1], L2, [X|L3] ) :- app( L1, L2, L3 ). prefR( _L2, [] ). prefR( [X|L3], [X|L1] ) :- prefR(L3, L1 ). sufR( L2, L2 ). sufR( [_X|L3], L2 ) :- sufR( L3, L2 ). lst1( L2, L2 ) :- L2 = [_Y]. lst1( [_X|L3], L2 ) :- L2 = [_Y], lst1( L3, L2 ). lst2( [Y], [Y] ). lst2( [_X|L3], [Y] ) :- lst2( L3, [Y] ). lstR( [Y], Y ). lstR( [_X|L3], Y ) :- lstR( L3, Y ). memb1( L2, L2 ) :- L2 = [_Y|_]. memb1( L2, [_X|L3]) :- L2 = [_Y|_], memb1( L2, L3 ). memb2( [Y|_], [Y|_] ). memb2( [Y|_], [_X|L3]) :- memb2( [Y|_], L3 ). membR( Y, [Y|_] ). membR( Y, [_X|L3]) :- membR( Y, L3 ). :- discontiguous selct0/3, selct1/3, selct2/3. % selct0(Y, L, R) :- append(P, [Y|S], L), append(P, S, R). % selct, case 1. when P = [] selct0(Y, L, R) :- append([], [Y|S], L), append([], S, R). selct1(Y, L, R) :- [Y|S] = L, S = R. selct2(Y, [Y|R], R). % selct, case 2. when P = [X|L1] selct0(Y, L, R) :- append([X|L1], [Y|S], L), append([X|L1], S, R). % append([X|L1], [Y|S], L) <==> L = [X|L4], append(L1, [Y|S], L4) % append([X|L1], S, R) <==> R = [X|L5], append(L1, S, L5) % append(L1, [Y|S], L4), append(L1, S, L5) <==> select(Y, L4, L5) selct1(Y, L, R) :- L = [X|L4], R = [X|L5], select(Y, L4, L5). selct2(Y, [X|L4], [X|L5]) :- select(Y, L4, L5). % We now put the two clauses together (with some variable renaming in the second clause). selctR(Y, [Y|R], R). selctR(Y, [X|L], [X|R]) :- selctR(Y, L, R).