Skip to content

Instantly share code, notes, and snippets.

@sriki77
Created December 10, 2011 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sriki77/1455663 to your computer and use it in GitHub Desktop.
Save sriki77/1455663 to your computer and use it in GitHub Desktop.
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