
Untitled
By: a guest on
May 17th, 2012 | syntax:
None | size: 0.73 KB | hits: 14 | expires: Never
prolog Searching the Lists
predicate([a,c,b,d,e],[a,b,c,d]).
predicate([a,c,b,e,d],[a,b,c,d]).
has_prefix_perm(List1, List2) :-
permutation(List2, Permutation),
prefix(Permutation, List1),
!.
has_prefix_perm(_, []) :- !.
has_prefix_perm([Head1|List1], List2) :-
once(select(Head1, List2, Rest)),
has_prefix_perm(List1, Rest).
has_prefix_perm(List1, List2) :-
length(List2, L),
length(LittleL1, L),
append(LittleL1, _, List1),
permutation(LittleL1, List2),
!.
perm_prefix(Ls1, Ls2) :- phrase(perm(Ls2), Ls1, _).
perm([]) --> [].
perm(Ls0) --> [L], { select(L, Ls0, Ls1) }, perm(Ls1).
?- perm_prefix([a,c,b,d,e],[a,b,c,d]).
true
?- perm_prefix([a,c,b,e,d],[a,b,c,d]).
false.