0% found this document useful (0 votes)
75 views11 pages

Assignment # 1: Program 1: (Family-Hierarchy Problem)

The document contains two programs. Program 1 defines family relationships like parent, child, sibling etc and queries those relationships. Program 2 defines connections between ports and queries reachable ports. It also defines recursive predicates to find connections within 1, 2, 3 or 4 hops.

Uploaded by

mittal3008
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views11 pages

Assignment # 1: Program 1: (Family-Hierarchy Problem)

The document contains two programs. Program 1 defines family relationships like parent, child, sibling etc and queries those relationships. Program 2 defines connections between ports and queries reachable ports. It also defines recursive predicates to find connections within 1, 2, 3 or 4 hops.

Uploaded by

mittal3008
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

ASSIGNMENT # 1

Program 1: (Family-Hierarchy Problem)


parent(ram,harry). % parent fact
parent(ram,liz).
parent(lakshya,priya).
parent(lakshya,supriya).
parent(harry,jack).
parent(harry,bob).
parent(priya,bob).
parent(priya,jack).
parent(liz,lizie).
parent(suzie,pat).
parent(suzie,jenny).
parent(bob,pat).
parent(bob,jenny).
parent(pat,hilary).
male(ram). % male fact
male(lakshya).
male(harry).
male(jack).
male(bob).
male(pat).
female(priya). % female fact
female(liz).
female(supriya).
female(lizie).
female(suzie).
female(jenny).
female(hilary).
grandfather(X,Y):-parent(X,Z),parent(Z,Y),male(X),male(Z).
grandmother(P,Q):-parent(P,R),parent(R,Q),female(P).
greatgrandfather(A,B):-grandfather(A,C),parent(C,B).
child(M,N):-parent(N,M). % defining child
fact
sister(G,H):-parent(I,G),parent(I,H),female(G),G\==H. % defining sister fact
brother(S,T):-parent(U,S),parent(U,T),male(S),S\==T. % defining brother
fact
uncle(C,D):-parent(E,D),brother(C,E). % defining uncle
fact
aunt(X,Y):-parent(Z,Y),sister(X,Z). % defining aunt
fact
daughter(X,Y):-parent(Y,X),female(X). % defining
daughter fact
greatgrandfathers_daughter(X,Y):-greatgrandfather(Z,Y),daughter(X,Z).
grandmothers_sister(X,Y):-grandmother(Z,Y),sister(X,Z). % defining
grandmothers_sister fact

?- parent(X,pat),write(X).
?- child(X,liz),write(X).
?- grandfather(X,pat),write(X).
?- sister(X,pat),write(X).
?- greatgrandfather(X,pat),write(X). % finding great grand father of pat

?- uncle(X,pat),write(X).
?- grandmother(X,pat),write(X).
?- greatgrandfathers_daughter(X,pat),write(X).
?- grandmothers_sister(X,pat),write(X).
?- aunt(X,bob),write(X).

Program 2: (Connecting Port Problem)


connected(piraeus,syros).
connected(piraeus,milos).
connected(piraeus, kythnos).
connected(syros, paros).
connected(serifos,milos).
connected(serifos,paros).
connected(paros,naxos).
connected(naxos,amorgos).
connected(naxos,santorini).

connected1(X,Y):- connected(X,Y).
connected2(X,Y):- connected(X,Z),connected(Z,Y).
connected3(X,Y):- connected(X,Z),connected(Z,A),connected(A,Y). % connected with 2 island in
between
connected4(X,Y):- connected(X,Z),connected(Z,A),connected(A,B),connected(B,Y). % connected
%with 3 island in between

connectedl(X,Y):-connected1(X,Y);connected2(X,Y);connected3(X,Y);connected4(X,Y).
% connection fact
?-connectedl(X,naxos),write(X).
?-connectedl(piraeus,X),write(X). % where can go from piraeus
Q3. Make a program in prolog to find factorial of number
Solution :

factorial(0,1).
factorial(N,F) :-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.
?-factorial(5,F),write(F).

Q4.Make a program in prolog to calculate Fibonacci series.


Solutions:
fib(1,0).
fib(2,1).
fib(N,F) :-
N>2,
N1 is N-1,
N2 is N-2,
fib(N1,F1),
fib(N2,F2),
F is F1+F2.

?-fib(7,F),write(F).

Output:

Q.5: Make a program in prolog to determine whether the number is even or odd.

Sol ution:

find(N,F) :- % find function


N1 is N mod 2,
N1=\=0->F is odd else F is even.

?- find(39,F),write(F). % finding whether 39 is even or odd


Q.6: Make a program in prolog to concatenate 2 strings
Solution:

str_conc(A, B, S):- S is A+B. % str connect function


?-str_conc("connecting string1 ", "and string2", X), write(X). % concate %strings

Q.7: Make a program in prolog to solve 8 Queens Problem.


Solution:

solution([]). % defining exit case for solution

solution([X/Y|Others]) :- % recursive function


solution(Others),
member(Y, [1,2,3,4,5,6,7,8]),
noattack(X/Y, Others).
noattack(_,[]). % defining exit case for noattack

noattack(X/Y,[X1/Y1|Others]) :- % recursive no attack function


Y == Y1,
Y1 - Y == X1 - X,
Y1 - Y == X - X1,
noattack(X/Y,Others).

member(Item,[Item|Rest]). % member function

member(Item,[First|Rest]) :- % recursive member function


member(Item,Rest).

template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]). % template

?- template(S), solution(S),write(S). % finding solution


ASSIGNMENT # 3

Q1. Write a program in prolog to check whether a given sub-list is present in the list or not.

conc([],L, L).

conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).

sublist(S, L) :- conc(L1, L2, L), conc(S, L3, L2).

?- sublist([1,2,3,4],[1,2,3,4,5]).

Q2. Write a program to delete a sub-list from a given list.

delete(X, []).

delete( X, [A | T]) :-

delete(X, T),

A =\= X,

write(A).

?-delete(2, [2, 3, 4, 5]).


3. . Reverse and Append two lists

append([], R, R).

append([H|T], R, [H|X]):-

append(T, R, X).

reverse([X], [X]).

reverse([H|T], R):-

reverse(T, X), append(X, [H], R).

?- reverse([1,2,3],R), write(R),nl,fail.

?- append([1,2,3],[a,b,c], K), write(K),nl,fail.

?- reverse(K,J) ,append([1,2,3],[a,b,c], K), write(J).


4. Write a program in prolog that computes the derivatives of expression containing plus
operator.

deriv(C,X,0) :- number(C).

deriv(X,X,1).

deriv(U+V,X,DU+DV) :- deriv(U,X,DU), deriv(V,X,DV).

?-deriv(x+x,x,A), write(A).
5. Write a program in prolog that computes the derivatives of expression containing minus
operator.
deriv(C,X,0) :- number(C).

deriv(X,X,1).

deriv(C*U,X,C*DU) :- number(C), deriv(U,X,DU).

deriv(U-V,X,DU-DV) :- deriv(U,X,DU), deriv(V,X,DV).

?-deriv((2*x)-x,x,A), write(A).

6. Write a program in prolog that computes the derivatives of expression containing times(*)
operator

deriv(C,X,0) :- number(C).

deriv(X,X,1).

deriv(C*U,X,C*DU) :- number(C), deriv(U,X,DU).

?-deriv(2*x, x, A), write(A).

You might also like