[KAPLAN]
|
Cevap : Prolog Örnekleri | Prolog Examples
Program 8 6 - efficient factorial - using accumulator and if  then  else
/* ************************************************ */
/* */
/* fac3/2 */
/* Summary: True if Arg2 is the factorial of */
/* Arg1 Calls fac3/3 */
/* Arg 1: Number */
/* Arg 2: Number */
/* Author: P J Hancox */
/* Date: 22 October 1999 */
/* */
/* ************************************************ */
fac3(Numb, Fact) :-
fac3(Numb, 1, Fact)
/* ************************************************ */
/* */
/* fac3/3 */
/* Summary: True if Arg3 is the factorial of */
/* Arg1 Uses if  then  else */
/* Arg 1: Number */
/* Arg 2: Number (accumulator) */
/* Arg 2: Number */
/* Author: P J Hancox */
/* Date: 22 October 1999 */
/* */
/* ************************************************ */
fac3(N, Accum, Fact) :-
( N > 0 ->
N1 is N - 1,
Accum1 is N * Accum,
fac3(N1, Accum1, Fact)
; N =:= 0 ->
Accum = Fact
)
Program 8 7 - maximum of two numbers with a green cut
/* ************************************************ */
/* */
/* max1/3 */
/* Summary: True if Arg3 is the maximum of Arg1 */
/* or Arg2 Works correctly */
/* Arg 1: Number */
/* Arg 2: Number */
/* Arg 2: Number */
/* Author: P J Hancox */
/* Date: 22 October 1999 */
/* */
/* ************************************************ */
max1(X, Y, X) :-
X >= Y, ! % green cut
max1(X, Y, Y) :-
X < Y
Program 8 8 - maximum of two numbers with a red cut - gives incorrect results
/* ************************************************ */
/* */
/* max2/3 */
/* Summary: True if Arg3 is the maximum of Arg1 */
/* or Arg2 Works incorrectly because */
/* it allows max2(2,1,1) */
/* Arg 1: Number */
/* Arg 2: Number */
/* Arg 2: Number */
/* Author: P J Hancox */
/* Date: 22 October 1999 */
/* */
/* ************************************************ */
max2(X, Y, X) :-
X >= Y, !
max2(_X, Y, Y)
Program 8 9 - maximum of two numbers using if  then  else
/* ************************************************ */
/* */
/* max3/3 */
/* Summary: True if Arg3 is the maximum of Arg1 */
/* or Arg2 Works correctly Uses */
/* if  then  else Not used in */
/* lecture */
/* Arg 1: Number */
/* Arg 2: Number */
/* Arg 2: Number */
/* Author: P J Hancox */
/* Date: 22 October 1999 */
/* */
/* ************************************************ */
max3(X, Y, Z) :-
( X >= Y ->
X = Z
; X < Y ->
Y = Z
)
Lecture 9
Program 9 1 - a rule base for defining relationships between parents, children and siblings
The logical inference maker requires rules to work
% The following must be present - but they are better
% placed in the inference engine
:- op(400, xfx, if)
:- op(300, xfy, and)
/* ************************************************ */
/* */
/* Rules of inference */
/* */
/* ************************************************ */
father(Father, Child) if
parent(Father, Child) and
male(Father)
mother(Mother, Child) if
parent(Mother, Child) and
female(Mother)
child(Child, Parent) if
parent(Parent, Child)
sibling(Sibling1, Sibling2) if
father(Father, Sibling1) and
father(Father, Sibling2) and
mother(Mother, Sibling1) and
mother(Mother, Sibling2) and
not_equals(Sibling1, Sibling2)
brother(Brother, Sibling) if
sibling(Brother, Sibling) and
male(Brother)
sister(Sister, Sibling) if
sibling(Sister, Sibling) and
female(Sister)
step_sibling(Sibling1, Sibling2) if
father(Father1, Sibling1) and
father(Father2, Sibling2) and
mother(Mother, Sibling1) and
mother(Mother, Sibling2) and
not_equals(Sibling1, Sibling2) and
not_equals(Father1, Father2)
step_sibling(Sibling1, Sibling2) if
father(Father, Sibling1) and
father(Father, Sibling2) and
mother(Mother1, Sibling1) and
mother(Mother2, Sibling2) and
not_equals(Sibling1, Sibling2) and
not_equals(Mother1, Mother2)
step_brother(Brother, Sibling) if
step_sibling(Brother, Sibling) and
male(Brother)
step_sister(Sister, Sibling) if
step_sibling(Sister, Sibling) and
female(Sister)
married(Husband, Wife) if
father(Husband, Child) and
mother(Wife, Child)
|