Indice Lección 1 Lección 2 Lección 3 Lección 4 Lección 5

Guía de Ejercicios en Prolog

Ejercicio 1

Utiliza la siguiente base de datos (un extracto de un árbol genealógico) para construir los predicados y contestar las preguntas que aparecen a continuación .

%%
% File : family.pl
% Part of a family tree expressed in Prolog.
%%
%%
% File : family.pl
% Part of a family tree expressed in Prolog.
%%

male(michael).
male(charles_gordon).
male(jim).
male(elmo).
male(greg).
male(peter).

female(cathy).
female(sharon).
female(julie).
female(melody).
female(crystal).
female(stephanie).
female(danielle).
female(hazel).
female(eleanor).
female(maria).



father(michael, cathy).
father(michael, sharon).
father(charles_gordon, michael).
father(charles_gordon, julie).
father(charles, charles_gordon).
father(jim, melody).
father(jim, crystal).
father(elmo, jim).
father(greg, stephanie).
father(greg, peter).
father(greg, maria).
father(greg, danielle).

mother(melody, cathy).
mother(melody, sharon).
mother(hazel, michael).
mother(hazel, julie).
mother(eleanor, melody).
mother(eleanor, crystal).
mother(crystal, stephanie).
mother(crystal, peter).
mother(crystal, maria).
mother(crystal, danielle).

parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
              

1.- Cuáles son los hijos de Michael? Indica la consulta.

2.- Construye el predicado grandfather(X, Y) que es verdadero si X es abuelo de Y. Además, incluya la consulta análoga grandmother(X, Y) y grandparent(X, Y).

3.- Prueba la consultas

?- write(hola).
?- write(hola), write(mundo).
?- write(hola), nl, write(mundo).
?- write(X).
      

3.- Construye el predicado sister(X, Y) que es verdadero si X es hermana de Y, brother(X, Y) que es verdadero si X es hermano de Y, y sibling(X, Y) que es verdadero si X es hermano o hermana de Y. Ojo que brother(michael, michael) es falso.

4.- Escribe un predicado only_child(X) que es verdadero si X es hijo/hija único.

5.- Prueba la consulta:

Prueba la consulta siguiente:

?- mother(crystal, X).


      

Luego, prueba la siguiente:

?- mother(crystal, X), write(X), nl, fail.