Created
July 28, 2016 23:08
-
-
Save vighneshbirodkar/6a46e60c7f40db74b5ee7b3e1dd3b2ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% The sum and subset sum functions were taken from | |
% Borrowed from https://github.com/jdan/code/blob/master/prolog/subset_sum.pl | |
% Atoms | |
good. | |
bad. | |
% Writing output | |
% write(...) Does not work in user mode | |
% Functors | |
goes(mary, college). | |
goes(john, school). | |
goes(jennifer, college). | |
% Variables | |
% goes(Who, Where). | |
% goes(Who, college). | |
% Tree ancestor | |
child(t, a, b). | |
child(t, a, c). | |
child(t, b, d). | |
child(t, b, e). | |
child(t, c, f). | |
child(t, c, g). | |
ancestor(T, X, Y) :- child(T, X, Y). | |
ancestor(T, X, Y) :- child(T, X, Z), ancestor(T, Z, Y). | |
% Tree leaf | |
leaf(T, X) :- child(T, _, X), not(child(T, X, _)). | |
% Arithmetic | |
% N is 5 + 1. | |
% Y is 6*8*9. | |
% Sum | |
% sum(L, N) :- sum of elements in L is equal to N | |
sum([], 0). | |
sum([H|T], N) :- sum(T, N1), N is N1 + H. | |
% Power function | |
power(_, 0, 1). | |
power(X, K, Y) :- K_pred is (K - 1), power(X, K_pred, Y_div), Y is Y_div * X. | |
% Graph cycle | |
edge(g1, a, b). | |
edge(g1, b, c). | |
edge(g1, c, d). | |
edge(g1, d, a). | |
edge(g2, a, b). | |
edge(g2, b, c). | |
edge(g2, c, d). | |
connected(G, X, Y) :- edge(G, X, Y). | |
connected(G, X, Y) :- edge(G, X, Z), connected(G, Z, Y). | |
cycle(G) :- connected(G, X, X). | |
% Example of cut operator | |
fire(charmander). | |
fire(charizard). | |
water(squirtle). | |
water(blastoise). | |
grass(bulbasaur). | |
grass(venasaur). | |
% Cut operator | |
good_team :- fire(X), grass(Y), water(Z). | |
% good_team :- fire(X), ! ,grass(Y), water(Z). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment