Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Prolog Day 2
#!/opt/local/bin/gprolog --consult-file
rev([X],[X]).
rev([H|T],RL) :- rev(T,RT), append(RT,[H],RL).
#!/opt/local/bin/gprolog --consult-file
small(X,Y,Y) :- X > Y.
small(X,Y,X) :- Y >= X.
small([H|T],S) :- \+(T=[]),small(T,ST),small(H,ST,S),!.
small([X],X).
#!/opt/local/bin/gprolog --consult-file
small(X,Y,Y) :- X > Y.
small(X,Y,X) :- Y >= X.
small([H|T],S) :- \+(T=[]),small(T,ST),small(H,ST,S),!.
small([X],X).
minusAcc(L,[],_,L) :- !.
minusAcc([],_,A,A) :- !.
minusAcc([H|T],SL,A,W) :- \+memberchk(H,SL),!,
append([H],A,AL),
minusAcc(T,SL,AL,W).
minusAcc([_|T],SL,A,W) :- minusAcc(T,SL,A,W).
minus(L,SL,W) :- minusAcc(L,SL,[],W).
sortAcc([],A,A) :- !.
sortAcc(UL,A,SL) :- \+(UL=[]),
small(UL,Min),
append(A,[Min],AL),
minus(UL,AL,RL),
sortAcc(RL,AL,SL),!.
sortList(UL,SL) :- sortAcc(UL,[],SL).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment