Skip to content

Instantly share code, notes, and snippets.

@pilotpirxie
Created October 21, 2019 14:45
Show Gist options
  • Save pilotpirxie/8e2aa19a9108b36405014787d04e530d to your computer and use it in GitHub Desktop.
Save pilotpirxie/8e2aa19a9108b36405014787d04e530d to your computer and use it in GitHub Desktop.
Prolog sample written for learning
% loves - predicate
% romeo - atoms
% juliet - atoms
% romeo and juliet are constants and arguments passed to predicate function
loves(romeo, juliet).
loves(juliet, romeo) :-
loves(romeo, juliet).
male(albert).
male(bob).
male(bill).
male(carl).
male(charlie).
male(dan).
male(edward).
female(alice).
female(betsy).
female(diana).
happy(albert).
happy(alice).
happy(bob).
happy(bill).
with_albert(alice).
% albert is running if albert is happy
runs(albert) :-
happy(albert).
% alice is dancing if alice is happy and alice is with albert
dances(alice) :-
happy(alice),
with_albert(alice).
% check statement and write
does_alice_dance :-
dances(alice),
write('When alice is happy and alice is with albert then alice is dancing').
% wrong case, because near_water does not exists
swims(bob) :-
happy(bob),
near_water(bob).
% different cases
swims(bill) :-
happy(bill).
swims(bill) :-
near_water(bill).
% parent(X, betsy).
% who is a parent of betsy and dances?
% parent(X, betsy), dances(X).
% who is a parent of carl and is a parent of parent of carl
% in other words: who is a parent of carl and who is grandparent of carl
% parent(X, carl), parent(Y, X).
% who is a child of albert and child of albert child
% in other words: who is albert child and albert grandchild
% parent(albert, X), parent(X, Y).
parent(albert, bob).
parent(albert, betsy).
parent(albert, bill).
parent(alice, bob).
parent(alice, betsy).
parent(alice, bill).
parent(bob, carl).
parent(bob, charlie).
% get results and write nice message
get_albert_grandchild :-
parent(albert, X),
parent(X, Y),
write('Albert grandchild is '),
write(Y), nl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment