Yalnız Mesajı Göster

Prolog Örnekleri | Prolog Examples

Eski 04-07-2009   #1
[KAPLAN]
Icon47

Prolog Örnekleri | Prolog Examples



Lecture 1


Program 11

likes(max, julia)
Program 12

likes(max, julia)
likes(max, amabel)
Program 13 - a definition of jealousy

likes(max, julia)
likes(max, amabel)

jealous(Jealous, Victim) :-
likes(Person, Jealous),
likes(Person, Victim)



Lecture 2


Program 21

lives_in(max, london)
likes(max, amabel)
child(charles, amy, brian)
price(template, 3, 475)
assembly(arm, joint(ball, 3))
Program 22

lives_at(brian, boxgrave_rd)
lives_at(mandy, boxgrave_rd)

neighbours(Pers1, Pers2) :-
lives_at(Pers1, Road),
lives_at(Pers2, Road)
Program 23

pass(Pass_Mark) :-
module_pass(Mod_Pass),
module_no(Mod_No),
Pass_Mark is Mod_Pass * Mod_No

% number of modules to be completed
module_no(6)

% individual module pass mark
module_pass(40)

friend_of(max, julia)
friend_of(max, amabel)
friend_of(amabel, richard)
% etc

% 1
friend(Pers, Friend) :-
friend_of(Pers, Friend)
% 2
friend(Pers, Friend) :-
friend_of(Pers, Inter),
friend(Inter, Friend)



Lecture 3


Program 31 - inefficient factorial

% 1 - "Input" number is 0
factorial(0, _, 1)

% 2 - base when all args unify
factorial(Numb, Numb, Numb)

% 3 - singly recursive
factorial(Numb, Count, Answ) :-
% don't count past Numb
Count < Numb,
% increment Count
Count1 is Count + 1,
% calculate Count1!
factorial(Numb, Count1, Answ1),
% calculate Count!
Answ is Answ1 * Count

% factorial/2 calls factorial/3
factorial(Numb, Result) :-
factorial(Numb, 1, Result)
Program 32 - fibonacci numbers - not used in 2002/03

% 1 - terminating
fibonacci(1, 1)

% 2 - terminating
fibonacci(2, 1)

% 3 - doubly recursive
fibonacci(Numb, Fib) :-
Numb > 2,
Numb1 is Numb - 1,
Numb2 is Numb - 2,
fibonacci(Numb1, Fib1),
fibonacci(Numb2, Fib2),
Fib is Fib1 + Fib2
Program 33 - displaying a binary tree

display_label(Label, Offset) :-
tab(Offset),
write(Label),
nl

% 1 - boundary
display_tree(nil, _Offset)
% 2 - recursive
display_tree(bt(Left, Label, Right), Offset) :-
Offset1 is Offset + 5,
display_tree(Left, Offset1),
display_label(Label, Offset),
display_tree(Right, Offset1)
Program 34 - efficient factorial

factorial(Numb, Answ) :-
factorial(Numb, 1, 1, Answ)

% 1 - Number is 0
factorial(0, _, _, 1)
% 2 - Number is 2
factorial(Numb, Numb, Answ, Answ)
% 3 - recursive
factorial(Numb, Count, Answ0, Answ) :-
Numb > Count,
Count1 is Count + 1,
Answ1 is Answ0 * Count1,
factorial(Numb, Count1, Answ1, Answ)



Lecture 4


Program 41 - graph search

path(a,1)
path(a,3)

path(1,2)
path(1,4)

path(2,5)

path(3,4)

path(4,5)

% 1 - boundary
route(Start, End) :-
path(Start, End)
% 2 - recursive
route(Start, End) :-
path(Start, Via),
route(Via, End)
Program 42 - finding the length of a list


% 1 - terminating
len_of_list([], Length, Length)
% 2 - recursive
len_of_list([_Head|Tail], Length1, Length) :-
Length2 is Length1 + 1,
len_of_list(Tail, Length2, Length)

Program 43 - is an element a member of a list?


% 1 - terminating
memb(Elem, [Elem|_])
% 2 - recursive
memb(Elem, [_|Tail]) :-
memb(Elem, Tail)
Program 44 - finding the element at the nth position in a list


% 1 - recursive
nth(Count, Item, [_|Tail]) :-
Count > 1,
Count0 is Count - 1,
nth(Count0, Item, Tail)
% 2 - terminating
nth(1, Head, [Head|_])
Program 45 - appending two lists to make a third - or splitting a list into two lists


% 1 - terminating
app([], List, List)
% 2 - recursive
app([Head|L1], L2, [Head|L3]) :-
app(L1, L2, L3)
Program 46 - deleting an element from a list - an extension of memb/2 that also inserts an item into a list


% 1 - terminating
delete(Head, [Head|Tail], Tail)
% 2 - recursive
delete(Item, [Head|Tail], [Head|New_Tail]) :-
delete(Item, Tail, New_Tail)

Alıntı Yaparak Cevapla