Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a PROLOG program that investigates family relationships using lists. Th

ID: 3730220 • Letter: 1

Question

1. Write a PROLOG program that investigates family relationships using lists. The facts should be organized as follows: m ([first_male_name, second_male_name, ..., last_male_name]). f([first female name, second female name, ..., last female name]). family ( [father, mother, [child 1, child_2,..., child_n]]). Write rules that define the following relations: male (X) female (X) father, mother, parent siblingsl, siblings2 brotherl, brother2 sisterl, sister2 cousins uncle, aunt grandchild, grandson, granddaughter greatgrandparent ancestor For each of these rules show an example of its use. 2. Write a PROLOG program that includes the following operations with lists: membership testing (is an element member of a list?) first element last element two adjacent elements three adjacent elements append list1 to list2 producing list3 delete element from a list append element to a list insert element in a list Compute the length of list reverse a list check whether a list is a palindrome display a list For each of these operations write your implementation of the operation and show an example of its use. If a predicate already exists (predefined in Prolog), modify its name (e.g. myappend or appendl). Lists to be processed can be created by an auxiliary program, defined as facts, or entered from the keyboard. 3. Write a PROLOG program that solves the 8 queens problem (location of 8 queens on a chess board so that no queens have each other in check, i.e. are not located in the same row/column/diagonal).

Explanation / Answer

Answer :

1.

y.trace

Predicates

Father(symbol,symbol)

Mother(symbol,symbol)

Male(symbol)

Female(symbol)

Sister(symbol,symbol)

Brother(symbol,symbol)

Uncle(symbol,symbol)

Wife(symbol,symbol)

Aunty(symbol,symbol)

Bhabi(symbol,symbol)

Cousine(symbol, symbol)

Nephew(symbol, symbol)

Grandson(symbol, symbol)

Grand daughter(symbol, symbol)

                Clauses

Sister(x,y):-father(X,A),father(Y,A),female(Y)

Father(male1,male2)

Father(male3,male4)

Father(male5,male6)

Mother(female1,female2)

Mother(X,Y):-father(X,Z),wife(Y,Z)

Brother(X,Y):-father(X,A),father(Y,A)male(X)

Brother(X,Y):-brother(Y,X)

Male(male1)

Male(male2)

Male(male3)

Male(male4)

Female(female1)

Female(female2)

Female(female3)

Female(female4)

Wife(male1,female1)

Wife2(male2,female2)

Wife(male3,female3)

Grandson(x,x):father(A,Y),father(X,A),male(X)

Uncle(X,Y):-father(X,Z)

Aunty(X,Y):-wife(Y,Z);

                                Brother(Z,W);

                     Father(X,W);

Cousin(X,Y)

Nephew(X,Y):-wife(Y,A),male(X)

2.

Two adjecent element:

       adjacent(X0, X1, [E0,E1|Es]) :-

       adjacent_(Es, E0, E1, X0, X1).

       adjacent_([], E0, E1, E0, E1).

       adjacent_([E2|Es], E0, E1, X0, X1) :-

       if_(( E0 = X0, E1 = X1 ),

           true,

           adjacent_(Es, E1, E2, X0, X1))

(b)

last element:

domains

list=symbol*

predicates

last(list)

clauses

last([X]):-

write(" Last element is : "),

write(X).

last([Y|Tail]):-

last(Tail).

(c)

First element:

                  

   domains

list=symbol*

   predicates

last(list)

   clauses

first([X]):-

write(" Last element is : "),

write(X).

first([Y|Head]):-

last(Head).

(d)

Append two list:

   concat([], List, List).

   concat([Head|[]], List, [Head|List]).

   concat([Head|Tail], List, Concat) :- concat(Tail, List, C), concat(Head, C, Concat).