Program Prolog tentang Silsilah Keluarga

Berikut ini adalah contoh program prolog tentang silsilah keluarga.

anak(X,Y):-bapak(X,Y).
anak(X,Y):-ibu(X,Y).

cucu(X,Z):-bapak(X,Y),anak(Y,Z).
kakek(Z,X):-anak(Y,Z),bapak(X,Y).
nenek(Z,X):-anak(Y,Z),ibu(X,Y).


bapak(hadi,samsudin).
bapak(hadi,hambali).
bapak(hadi,syafii).
bapak(samsudin,mansyur).
bapak(samsudin,arfan).
bapak(samsudin,mahmudin).
ibu(diyah,samsudin).
ibu(suwarni,mansyur).
ibu(suwarni,arfan).
ibu(suwarni,mahmudin).

dengan output sebagai berikut : 

Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.4.0)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- 
% c:/Users/Mansyur/Documents/Prolog/tugas.pl compiled 0.00 sec, 16 clauses
1 ?- bapak(X,mansyur).
X = samsudin.

2 ?- bapak(X,samsudin).
X = hadi.

3 ?- ibu(Y,mansyur).
Y = suwarni.

4 ?- anak(samsudin,L).
L = mansyur ;
L = arfan ;
L = mahmudin .

5 ?- ibu(Y,samsudin).
Y = diyah.

6 ?- anak(hadi,O).
O = samsudin ;
O = hambali ;
O = syafii .

7 ?- kakek(X,mansyur).
false.

8 ?- kakek(mansyur,X).
X = hadi .

9 ?- nenek(mansyur,X).
X = diyah .

10 ?- cucu(X,hadi).
false.

11 ?- cucu(hadi,C).
C = mansyur ;
C = arfan ;
C = mahmudin .


Semoga informasi ini membantu anda.


No comments:

Post a Comment