prolog2.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. maxsum([X], [Y], M) :- M is X+Y, !.
  2. maxsum([X|Xs], [Y|Ys], V) :- V is X+Y, maxsum(Xs, Ys, M), V > M, !.
  3. maxsum([_|Xs], [_|Ys], M) :- maxsum(Xs, Ys, M).
  4. lile(L) :- length(L,X), member(X,L).
  5. lileg(L) :- lile(L), iflc(L).
  6. iflc([]) :- !.
  7. iflc([X|Xs]) :- atomic(X), !, iflc(Xs).
  8. iflc([X|Xs]) :- lileg(X), iflc(Xs).
  9. arrange(L1,L2,V,S1,S2) :- part(L1,V,S11,S12), part(L2,V,S21,S22), append(S11,S21,S1), append(S12,S22,S2).
  10. part([],_,[],[]).
  11. part([X|Xs],V,[X|L1],L2) :- X < V, !, part(Xs,V,L1,L2).
  12. part([X|Xs],V,L1,[X|L2]) :- X > V, !, part(Xs,V,L1,L2).
  13. part([_|Xs],V,L1,L2) :- part(Xs,V,L1,L2).
  14. sumoftwo(L,N,X,Y) :- deepmember(X,L), deepmember(Y,L), N is X+Y.
  15. deepmember(X,[X|_]) :- atomic(X).
  16. deepmember(X,[Y|Ys]) :- deepmember(X,Y) ; deepmember(X,Ys).
  17. deeprev([],[]) :- !.
  18. deeprev([X|Xs], F) :- !, deeprev(X,R), deeprev(Xs,Vs), append(Vs,[R],F).
  19. deeprev(X,X).
  20. palindrome(X) :- reverse(X,Y), X == Y.
  21. pack([], []).
  22. pack([X|XS], [Z|ZS]) :- pack_helper(X, XS, YS, Z), pack(YS, ZS).
  23. % third arg is the rest of the list, fourth is the list of the adiacent occurences of X
  24. pack_helper(X, [], [], [X]).
  25. pack_helper(X, [Y|YS], [Y|YS], [X]) :- X \= Y.
  26. pack_helper(X, [X|XS], YS, [X|ZS]) :- pack_helper(X, XS, YS, ZS).