Skip to content

Instantly share code, notes, and snippets.

@bogdancj
Created November 22, 2012 00:04
Show Gist options
  • Save bogdancj/4128638 to your computer and use it in GitHub Desktop.
Save bogdancj/4128638 to your computer and use it in GitHub Desktop.
andrei512 first prolog code
domains
element = integer
list = element*
predicates
sort(list, list)
join(list, list, list)
split(list, element, list, list, list, list)
inverse(list, list, list)
dump(list, list, list)
clauses
inverse([], TMP, R):-
R = TMP.
inverse([H|T], TMP, R):-
not(T = []),
inverse(T, [H|TMP], R).
join(A, B, C):-
inverse(A, [], X),
dump(X, B, C).
dump([], B, R):-
R = B.
dump([H|T], B, R):-
dump(T, [H | B], R).
split([], _, TL, TR, L, R):-
L = TL,
R = TR.
split([H|T], P, TL, TR, L, R):-
H < P,
split(T, P, [H|TL], TR, L, R).
split([H|T], P, TL, TR, L, R):-
not(H < P),
split(T, P, TL, [H|TR], L, R).
sort([H|T], R):-
T = [],
R = [H|[]].
sort([H|T], R):-
not(T = []),
split([H|T], H, [], [], L, R),
sort(L, SL),
sort(R, SR),
join(SL, SR, X),
R = X.
goal
sort([1, 3, 8, 6, 2, 7, 6, 4], S), write(S).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment