Prolog Exercise Solutions: Family Relationships and List Processing, Assignments of Computer Science

Solutions to prolog exercises covering family relationships and list processing. Topics include defining predicates for parent-child relationships, siblings, grandchildren, first cousins, descendants, and list processing functions such as third element, first pair, deleting the third element, checking for duplicate elements, odd and even list sizes, and list prefixes and membership.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-0f5
koofers-user-0f5 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 19 Sample Solutions
Exercise 1
mother(X,Y) :-
female(X),
parent(X,Y).
Exercise 2
father(X,Y) :-
male(X),
parent(X,Y).
Exercise 3 (sibling is from the book):
sibling(X,Y) :-
parent(Z,X),
parent(Z,Y),
not(X=Y).
sister(X,Y) :-
female(X),
sibling(X,Y).
Exercise 4
grandson(X,Y) :-
male(X),
parent(Y,Z),
parent(Z,X).
Exercise 5
firstCousin(Cousin1,Cousin2) :-
parent(Parent1,Cousin1),
parent(Parent2,Cousin2),
sibling(Parent1,Parent2).
Exercise 6
descendant(Desc,Ances) :-
parent(Ances,Desc).
descendant(Desc,Ances) :-
pf3

Partial preview of the text

Download Prolog Exercise Solutions: Family Relationships and List Processing and more Assignments Computer Science in PDF only on Docsity!

Chapter 19 Sample Solutions

Exercise 1

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

Exercise 2

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

Exercise 3 (sibling is from the book):

sibling(X,Y) :- parent(Z,X), parent(Z,Y), not(X=Y). sister(X,Y) :- female(X), sibling(X,Y).

Exercise 4

grandson(X,Y) :- male(X), parent(Y,Z), parent(Z,X).

Exercise 5

firstCousin(Cousin1,Cousin2) :- parent(Parent1,Cousin1), parent(Parent2,Cousin2), sibling(Parent1,Parent2).

Exercise 6

descendant(Desc,Ances) :- parent(Ances,Desc). descendant(Desc,Ances) :-

parent(Ances,X), descendant(Desc,X).

Exercise 7

third([,,Y|_],Y).

Exercise 8

firstPair([X,X|_]).

Exercise 9

del3([A,B,_|Rest],[A,B|Rest]).

Exercise 11

isDuped(Y) :- dupList(_,Y).

Exercise 10

oddSize([]). oddSize([,_|Rest]) :- oddSize(Rest).

Exercise 11

evenSize([]). evenSize([,|Z]) :- evenSize(Z).

Exercise 12

prefix(X,Y):- append(X,_,Y).

Exercise 13

isMember(X,[X|]). isMember(X,[|Z]) :- isMember(X, Z).

Exercise 14

isUnion([],Y,Y). isUnion([H|T],Y,Z) :-