My E-Portfolio based on work carried out on my Msc Program on Artificial Intelligence and Machine Learning at the University of Essex.
% Facts about family relationships
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
% Rule: X is a grandparent of Z if X is parent of Y and Y is parent of Z
grandparent(X,Z) :- parent(X,Y), parent(Y,Z).
% Rule: X and Y are siblings if they have the same parent
sibling(X,Y) :- parent(Z,X), parent(Z,Y), X \= Y.
?- parent(tom, bob).
% true.
?- parent(bob, X).
% X = ann ;
% X = pat.
?- grandparent(tom, ann).
% true.
?- grandparent(tom, X).
% X = ann ;
% X = pat.
% Check if X is a member of list L
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
% Append two lists
append([], L, L).
append([H|T], L, [H|R]) :- append(T, L, R).
% Find length of a list
length([], 0).
length([_|T], N) :- length(T, N1), N is N1 + 1.
?- member(3, [1,2,3,4]).
% true.
?- append([1,2], [3,4], X).
% X = [1, 2, 3, 4].
?- length([a,b,c], X).
% X = 3.
% Facts about animals
animal(dog).
animal(cat).
animal(robin).
animal(penguin).
% Facts about mammals
mammal(dog).
mammal(cat).
% Facts about birds
bird(robin).
bird(penguin).
% Facts about flying ability
can_fly(robin).
% Rules
warm_blooded(X) :- mammal(X).
warm_blooded(X) :- bird(X).
vertebrate(X) :- mammal(X).
vertebrate(X) :- bird(X).
% Complex rule
can_move_fast(X) :- mammal(X), can_run(X).
can_move_fast(X) :- bird(X), can_fly(X).
:- to define logical implicationsThese examples can be run in SWI-Prolog:
.pl file?- [filename].?- prompt| ← Back to Unit 4 | KRR Module Home |