Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 0.73 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. prolog Searching the Lists
  2. predicate([a,c,b,d,e],[a,b,c,d]).
  3.        
  4. predicate([a,c,b,e,d],[a,b,c,d]).
  5.        
  6. has_prefix_perm(List1, List2) :-
  7.     permutation(List2, Permutation),
  8.     prefix(Permutation, List1),
  9.     !.
  10.        
  11. has_prefix_perm(_, []) :- !.
  12. has_prefix_perm([Head1|List1], List2) :-
  13.     once(select(Head1, List2, Rest)),
  14.     has_prefix_perm(List1, Rest).
  15.        
  16. has_prefix_perm(List1, List2) :-
  17.     length(List2, L),
  18.     length(LittleL1, L),
  19.     append(LittleL1, _, List1),
  20.     permutation(LittleL1, List2),
  21.     !.
  22.        
  23. perm_prefix(Ls1, Ls2) :- phrase(perm(Ls2), Ls1, _).
  24.  
  25. perm([])  --> [].
  26. perm(Ls0) --> [L], { select(L, Ls0, Ls1) }, perm(Ls1).
  27.        
  28. ?- perm_prefix([a,c,b,d,e],[a,b,c,d]).
  29. true
  30.  
  31. ?- perm_prefix([a,c,b,e,d],[a,b,c,d]).
  32. false.