Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created September 16, 2011 04:55
Show Gist options
  • Save passingloop/1221229 to your computer and use it in GitHub Desktop.
Save passingloop/1221229 to your computer and use it in GitHub Desktop.
7l7w-prolog-day2
% -*- mode: prolog; -*-
fact(1, 0).
fact(F, N) :-
N > 0,
N1 is N - 1,
fact(F1, N1),
F is F1 * N.
% -*- mode: prolog; -*-
fib(0, 0).
fib(1, 1).
fib(F, N) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fib(F2, N2),
fib(F1, N1),
F is F1 + F2.
% -*- mode: prolog; -*-
min(X, [X]).
min(X, [A, B]) :-
A < B, X = A.
min(X, [A, B]) :-
A >= B, X = B.
min(X, [Head|Tail]) :-
min(Tmin, Tail),
min(X, [Head, Tmin]).
% -*- mode: prolog; -*-
bird(kiwi).
bird(duck).
fly(duck).
penguin1(X) :-
\+fly(X), bird(X).
penguin2(X) :-
bird(X), \+fly(X).
% -*- mode: prolog; -*-
min(X, [X]).
min(X, [A, B]) :-
A < B, X = A.
min(X, [A, B]) :-
A >= B, X = B.
min(X, [Head|Tail]) :-
min(Tmin, Tail),
min(X, [Head, Tmin]).
selection_sort([], []).
selection_sort(L, [X|S]) :-
min(X, L),
delete(L, X, R),
selection_sort(R, S).
% -*- mode: prolog; -*-
hanoi(0, _, _, _).
hanoi(N, A, B, C) :-
N > 0,
N1 is N - 1,
hanoi(N1, A, C, B),
write((move, N, from, A, to, B)), nl,
hanoi(N1, C, B, A).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment