Assignment # 1: Program 1: (Family-Hierarchy Problem)
Assignment # 1: Program 1: (Family-Hierarchy Problem)
?- 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).
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).
?-fib(7,F),write(F).
Output:
Q.5: Make a program in prolog to determine whether the number is even or odd.
Sol ution:
template([1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8]). % template
Q1. Write a program in prolog to check whether a given sub-list is present in the list or not.
conc([],L, L).
?- sublist([1,2,3,4],[1,2,3,4,5]).
delete(X, []).
delete( X, [A | T]) :-
delete(X, T),
A =\= X,
write(A).
append([], R, R).
append([H|T], R, [H|X]):-
append(T, R, X).
reverse([X], [X]).
reverse([H|T], R):-
?- reverse([1,2,3],R), write(R),nl,fail.
deriv(C,X,0) :- number(C).
deriv(X,X,1).
?-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((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).