You are on page 1of 35

Q1.

Path Searching
arc(g,h). arc(g,d). arc(e,d). arc(h,f). arc(e,f). arc(a,e). arc(a,b). arc(b,f). arc(b,c). arc(f,c).

path(X,X). path(X,Y):-arc(X,Z),path(Z,Y).

Some Queries
| ?- ['F:prolog/p1']. compiling F:/prolog/p1.pl for byte code... F:/prolog/p1.pl compiled, 12 lines read - 1222 bytes written, 16 ms

yes

| ?- path(f,f).

true ? ;

no | ?- path(a,c).

true ? ;

true ? ;

true ? ;

no | ?- path(g,x).

no | ?- path(g,X).

X=g?;

X=h?;

X=f?;

X=c?;

X=d?;

no | ?- path(g,e).

no | ?- path(X,h).

X=h?;

X=g?;

No

Q2. Following rules provide a means to find a list which is the result of appending first parameter to the second:

lappend([],X,X). lappend([A|X],Y,[A|Z]):-lappend(X,Y,Z).

Some Queries | ?- ['f:prolog/p13']. compiling F:/prolog/p13.pl for byte code... F:/prolog/p13.pl compiled, 3 lines read - 453 bytes written, 0 ms

yes | ?- lappend(X,Y,[a,b]).

X = [] Y = [a,b] ? ;

X = [a]

Y = [b] ? ;

X = [a,b] Y = [] ? ;

no | ?- lappend(Y,X,[b,a]).

X = [b,a] Y = [] ? ;

X = [a] Y = [b] ? ;

X = [] Y = [b,a] ? ;

no

| ?- lappend([a,b],X,Y).

Y = [a,b|X]

Yes

Q3.Find out square of number

square(1,1). square(2,4). square(3,9). square(4,16). square(5,25). square(6,36).

Some Queries | ?- ['f:prolog/p14']. compiling F:/prolog/p14.pl for byte code... F:/prolog/p14.pl compiled, 5 lines read - 647 bytes written, 0 ms

yes | ?- square(2,X).

X=4

yes | ?- square(X,5).

no | ?- square(X,Y).

X=1 Y=1?;

X=2 Y=4?;

X=3 Y=9?;

X=4

Y = 16 ? ;

X=5 Y = 25 ? ;

X=6 Y = 36

(15 ms) yes | ?- square(2,3).

No

Q4. (a) Representing a symmetric relation. (b) Implementing a strange ticket condition How to represent this relation? Note that borders are symmetric.

border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire).

adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X).

valid(X, Y) :adjacent(X, Z),

adjacent(Z, Y).

Some Queries compiling F:/prolog/p16.pl for byte code... F:/prolog/p16.pl compiled, 16 lines read - 1639 bytes written, 0 ms

yes | ?- valid(wiltshire, sussex).

true ? ;

no | ?- valid(wiltshire, kent).

no | ?- valid(hampshire, hampshire). true ? ;

true ? ;

true ? ;

true ? ;

no | ?- valid(X, kent).

X = sussex ? ;

X = hampshire ? ;

X = hampshire ? ;

X = berkshire ? ;

X = kent ? ;

X = surrey ? ;

X = kent ? ;

no | ?- valid(sussex, X).

X = sussex ? ;

X = surrey ? ;

X = kent ? ;

X = sussex ? ;

X = hampshire ? ;

X = berkshire ? ;

X = sussex ? ;

X = surrey ? ;

X = berkshire ? ;

X = wiltshire ? ;

no | ?- valid(X, Y).

X = sussex Y = sussex ? ;

X = sussex Y = surrey ? ;

X = sussex Y = kent ? ;

X = sussex Y = sussex ? ;

X = sussex Y = hampshire ? ;

X = sussex Y = berkshire ? ;

X = surrey Y = sussex ? ;

X = surrey Y = surrey ? ;

X = hampshire Y = kent ? ;

X = hampshire Y = surrey ? ;

X = hampshire Y = hampshire ? ;

X = hampshire Y = kent ? ;

X = hampshire Y = sussex ? ;

X = hampshire Y = hampshire ? ;

X = hampshire Y = berkshire ? ;

X = hampshire Y = surrey ? ;

X = hampshire Y = hampshire ? ;

X = hampshire Y = wiltshire ? ;

X = berkshire Y = kent ? ;

X = berkshire Y = sussex ? ;

X = berkshire Y = hampshire ? ;

X = berkshire Y = berkshire ? ;

X = wiltshire Y = sussex ? ;

X = wiltshire Y = surrey ? ;

X = wiltshire Y = berkshire ? ;

X = wiltshire Y = wiltshire ? ;

X = wiltshire Y = surrey ? ;

X = wiltshire Y = hampshire ? ;

X = wiltshire Y = wiltshire ? ;

X = kent Y = kent ? ;

X = kent Y = surrey ? ;

X = kent Y = hampshire ? ;

X = surrey Y = kent ? ;

X = surrey Y = surrey ? ;

X = surrey Y = hampshire ? ;

X = kent Y = kent ? ;

X = kent Y = sussex ? ;

X = kent Y = hampshire ? ;

X = kent Y = berkshire ? ;

X = sussex Y = sussex ? ;

X = sussex Y = surrey ? ;

X = sussex Y = berkshire ? ;

X = sussex Y = wiltshire ? ;

X = surrey Y = sussex ? ;

X = surrey Y = surrey ? ;

X = surrey Y = berkshire ? ;

X = surrey Y = wiltshire ? ;

X = berkshire Y = sussex ? ;

X = berkshire Y = surrey ? ;

X = berkshire Y = berkshire ? ;

X = berkshire Y = wiltshire ? ;

X = surrey Y = surrey ? ;

X = surrey Y = hampshire ? ;

X = surrey Y = wiltshire ? ;

X = hampshire Y = hampshire ? ;

X = hampshire Y = berkshire ? ;

X = berkshire Y = hampshire ? ;

X = berkshire Y = berkshire ? ; (16 ms) no

Q5. Maximum number


max(X,Y,X):- X >= Y. max(X,Y,Y):- Y > X.

Some Queries | ?- ['D:prolog/p8.pl']. compiling D:/prolog/p8.pl for byte code... D:/prolog/p8.pl compiled, 2 lines read - 374 bytes written, 15 ms

yes | ?- max(5,3,R).

R=5?;

no | ?-

Q6. Verify if a word is a palindrome.

palindrome(L):- reverse(L,L). my_reverse([],[]). my_reverse([H|T],R):- my_reverse(T,T1),append(T1,[H],R).

| ?- ['D:prolog/p9.pl']. compiling D:/prolog/p9.pl for byte code... D:/prolog/p9.pl compiled, 3 lines read - 778 bytes written, 15 ms

yes | ?- reverse([a,b,c,d],X).

X = [d,c,b,a]

yes | ?- reverse([g,u,e,s,s],X).

X = [s,s,e,u,g]

yes | ?- palindrome([r,a,c,e,c,a,r]).

yes | ?- palindrome([l,o,v,e]).

No

Q7 Counts vowels in a list

vowel(X):- member(X,[a,e,i,o,u]). nr_vowel([],0). nr_vowel([X|T],N):- vowel(X),nr_vowel(T,N1),N is N1+1,!. nr_vowel([X|T],N):- nr_vowel(T,N).

Some Queries | ?- ['D:prolog/p13.pl']. compiling D:/prolog/p13.pl for byte code... D:/prolog/p13.pl:4: warning: singleton variables [X] for nr_vowel/2 D:/prolog/p13.pl compiled, 3 lines read - 1205 bytes written, 31 ms

yes | ?- nr_vowel([],X).

X=0

yes | ?- nr_vowel([a,r,e,d,i],X).

X=3

yes | ?- nr_vowel([m,r],X). X=0 yes | ?- nr_vowel([s,e,e,d],X). X=2 yes | ?-

Q8. Finding the greatest common divider (gcd )and the least common multiple (lcm) of two integers.
gcd(X,0,X). gcd(X,Y,D):- R is X mod Y, gcd(Y,R,D). lcm(X,Y,M):- gcd(X,Y,D),M is (X*Y)/D.

Some Queries

| ?- ['D:prolog/p16.pl']. compiling D:/prolog/p16.pl for byte code... D:/prolog/p16.pl compiled, 2 lines read - 1131 bytes written, 15 ms (16 ms) yes | ?- gcd(6,15,X). X=3? | ?- lcm(6,7,Y). Y = 42.0 ? yes

| ?- lcm(16,14,R). R = 112.0 ? Yes

Q9. Put an element in front of each element of a given list.


inser(X,A,[],[]). inser(X,A,[A|T],[X,A|T]):-!. inser(X,A,[H|T],[H|R]):-inser(X,A,T,R).

Some Queries
| ?- ['D:prolog/p17.pl']. compiling D:/prolog/p17.pl for byte code... D:/prolog/p17.pl:1: warning: singleton variables [X,A] for inser/4 D:/prolog/p17.pl compiled, 2 lines read - 712 bytes written, 15 ms yes | ?- inser(a,1,[2,1,3,4],R). R = [2,a,1,3,4] yes | ?- append(X,Y,[a,b]). X = [] Y = [a,b] ? ; X = [a] Y = [b] ? ; X = [a,b] Y = [] yes

Q10. This definition forces X and Y to be distinct


drinks(john, martini). drinks(mary, gin). drinks(susan, vodka). drinks(john, gin). drinks(fred, gin).

pair(X, Y, Z) :drinks(X, Z), drinks(Y, Z).

Some Queries
| ?- ['D:prolog/p20.pl']. compiling D:/prolog/p20.pl for byte code... D:/prolog/p20.pl compiled, 9 lines read - 908 bytes written, 15 ms

yes | ?- pair(X, john, martini).

X = john ?

yes | ?- pair(bertram, lucinda, vodka).

no | ?- pair(X, Y, Z).

X = john Y = john Z = martini ?

yes | ?- pair(mary, susan, gin).

no | ?- pair(john, mary, gin).

yes | ?- pair(john, john, gin).

yes | ?- pair(X, Y, gin).

X = mary Y = mary ?

yes

You might also like