You are on page 1of 25

Lab no 1 2 3

Experiment Name
Write a program to find the greater of the two numbers Write a program to find the greatest among the three numbers.

Date

Write a program to find the relationships between individuals. Write a program to find the factorial of a number. Write a program to understand the concept of unification. Write a program to find the area or volume of any shape. Write a program to find the number of elements in a list. Write a program to check elements in a list. Write a program to find the reverse of a list. Write a program to append the two lists. Write a program to delete a given value from list. Write a program to insert an element in a sorted list. Write a program to implement tower of Hanoi. Write a program for graph color mapping problem Write a program for implementing graph structure & finding path between two nodes. Write a program for implementing Binary search. Write a program for implementing DFA parser. Write a program for browsing, reading and writing the data in file. Write a program for counting the number of literals in a file. Write a program for finding whether the given word is prefix, suffix and infix of the given another word.

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23

Write a program for finding the maximum number of permuted words possible from the given word. Write a program for finding the possible number of combination words from a given word. Write a program for implementing the Anagram.

1. Write a program to find the greater of the two numbers

bigger (N, M) :(N< M), Write (The bigger number is), Write (M). bigger (N, M) :(N> M), Write (The bigger number is), Write (N). bigger (N, M) :(N= : = M), Write (The number s are equal). Queries 1) ? bigger (5,4). The bigger number is 5 Yes 2) ? bigger (5,5). The numbers are equal Yes 3) ? bigger (5,7). The bigger number is 7 Yes

2) Write a program to find the greatest among the three numbers. bigger (L, N, M) :(N< M), (L<M), Write (The bigger number is), Write (M). bigger (L, N, M) :(N> M), (N>L), Write (The bigger number is), Write (N). bigger (L, N, M) :Write (The bigger number is), Write (L). Queries 1) ?- bigger(5, 6, 7). The bigger number is :- 7 Yes 2) ?- bigger(5, 12, 7). The bigger number is :- 12 Yes 3) ?- bigger(28, 12, 7). The bigger number is :- 28

3) Write a program to find the relationships between individuals. Parent ( pam , bob). Parent ( tom , bob). Parent ( tom , liz). Parent ( bob , ann). Parent ( bob , pat). Parent ( pat , jim). Queries 1) ? parent(bob, Y). Y = tim; Y = jim; No 2) ? grandparent(Y, jim) Y = bob Yes

4) Write a program to find the factorial of a number.

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

Queries 1) ? factiroal(5, F). F = 120

5) Write a program to understand the concept of unification.

book ( title1, author1). book ( title2, author1). book ( title3, author2). book ( title4, author3).

Queries 1) ? book(Y, author1). Y = title1; Y = title2; No

6) Write a program to find the area or volume of any shape.

area (X, Y, Z): Z is X * Y, Write (Area is), Write (Z). area (X , Y):Y is 3.14*X*X, Write (Area is), Write(Y).

Queries: 1) ?- area(10, 20, A). Area is 200 A = 200 Yes

7) Write a program to find the number of elements in a list.

size ( [ ], 0). Size ( [H | T], N) : - size (T, N1), N is N1 + 1.

Queries 1) N=0 Yes 2) N=2 Yes 3) N=3 Yes 4) N=6 Yes ? size ( [a, b, c, [d, e ], f, g], N). ? size ( [1, 2, [2, 3] ], N). ? size ( [1, 2], N). ? size ( [ ], N).

8) Write a program to check elements in a list.

member (Element, [Element|1] ). member (Element, [A | Tail]) :- member ( Element, Tail).

Queries 1) No 2) Yes 3) No 4) No 5) Yes ? member (c, [a, b, c, [d, e], f, g]). ? member (d, [a, b, c, [d, e], f, g]). ? member (2, [1, 3]). ? member (2, [1, 2]). ? member (2, [ ]).

9) Write a program to find the reverse of a list.

reverse ([ ], [ ]). reverse ( [H | T], R) :- reverse ( T, R1), append(R1, [H], R). OR reverse ( [X | Y], Z, W) reverse ([ ], X, X). : - reverse ( Y, [X | Z], W).

Queries 1) ? reverse ([1, 2, 3, 4], X)

X = [4, 3, 2, 1] Yes 2) ? reverse ([1, 2], X)

X = [2, 1] Yes 3) ? reverse ([a, b, c, [d, e], f, g], X).

X = [g, f, [d, e], c, b, a] Yes

10) Write a program to append the two lists.

append ( [X | Y], Z, [X|W] ) :- append (Y, Z, W). append ( [ ], X, X).

Queries 1) ? append ( [1, 2, 3, 4], [5, 6], X). X = [1, 2, 3, 4, 5, 6] Yes 2) ? append ( [1, 2, 3], [4, 5], [1, 2, 3, 4, 5]). Yes 3) ? append ( [a, b, c], [d, e], X). X = [a, b, c, d, e] Yes

11) Write a program to delete a given value from list. delete (X, [X|T], T). delete (X, [H|T], [H|U]) :- delete(X, T, U). OR delete (N, [ ], [ ]). delete (N, [N|T], U) :- !, delete( N, T, U). delete (N, [H|T], [H|U]) :- delete(N, T, U).

Queries 1) ? delete (1, [1, 2, 3, 4], A).

A = [2, 3, 4] Yes 2) Yes 3) ? delete (1, [1, 1, 1, 2, 3, 4], X). ? delete (2, [1, 2, 3, 4, 5], [1, 3, 4, 5]).

X = [1, 1, 2, 3, 4] Yes

12) Write a program to insert an element in a sorted list.

insert (N, [ ], [N]). insert (N, [H|T], [N, H|T]) :- H > = N, !. insert (N, [H|T], [H|Y]) :- insert(N, T, Y).

Queries 1) ?- insert (1, [1, 2, 3, 4], A).

A = [1, 1, 2, 3, 4] Yes 2) Yes 3) ?- insert (1, [1, 1, 2, 3, 4], X). ?- insert (2, [1, 3, 4, 5], [1, 2, 3, 4, 5]).

X = [1, 1, 1, 2, 3, 4] Yes

13. Write a program to implement tower of Hanoi.


move(1,X,Y,_) :write('Move top disk from '), write(X), write(' to '), write(Y), nl. move(N,X,Y,Z) :N>1, M is N-1, move(M,X,Z,Y), move(1,X,Y,_),

move(M,Z,Y,X).

?- move(3,left,right,center). Move top disk from left to right Move top disk from left to center Move top disk from right to center Move top disk from left to right Move top disk from center to left Move top disk from center to right Move top disk from left to right yes

14. Write a program for graph color mapping problem.

adjacent(1,2). adjacent(1,3). adjacent(1,4). adjacent(1,5). adjacent(2,3). adjacent(2,4). adjacent(3,4). adjacent(4,5).

adjacent(2,1). adjacent(3,1). adjacent(4,1). adjacent(5,1). adjacent(3,2). adjacent(4,2). adjacent(4,3). adjacent(5,4).

color(1,red,a). color(2,blue,a). color(3,green,a). color(4,yellow,a).

color(1,red,b). color(2,blue,b). color(3,green,b). color(4,blue,b).

color(5,blue,a). color(5,green,b).

conflict(Coloring) :adjacent(X,Y), color(X,Color,Coloring), color(Y,Color,Coloring).

conflict(R1,R2,Coloring) :adjacent(R1,R2), color(R1,Color,Coloring), color(R2,Color,Coloring).

15. Write a program for implementing graph structure & finding path between two nodes.

edge(1,2). edge(1,4). edge(1,3). edge(2,3). edge(2,5). edge(3,4). edge(3,5). edge(4,5). connected(X,Y) :- edge(X,Y) ; edge(Y,X). path(A,B,Path) :travel(A,B,[A],Q), reverse(Q,Path). travel(A,B,P,[B|P]) :connected(A,B). travel(A,B,Visited,Path) :connected(A,C), C \== B, \+member(C,Visited), travel(C,B,[C|Visited],Path).

16. Write a program for implementing Binary search.


bsearch(X,Lo,Hi,Y):-Hi=:=Lo+1, X is Lo. bsearch(X,Lo,Hi,Y):-Hi < Lo+1, fail. bsearch(X,Lo,Hi,Y):-Hi>= Lo+1, Mid is (Lo+Hi)//2, f(Mid, Val), ( Val=<Y, bsearch(X,Mid,Hi,Y) ; Val>Y, bsearch(X,Lo,Mid,Y) ). ?:-bsearch(X,1,100,49), nl,nl,print(X),nl.

17. Write a program for implementing DFA parser.


parse(L) :- start(S), trans(S,L). trans(X,[A|B]) :delta(X,A,Y), write(X), write(' '), write([A|B]), nl, trans(Y,B). trans(X,[]) :final(X), write(X), write(' '), write([]), nl. delta(0,a,1). delta(0,b,0). delta(1,a,1). delta(1,b,2). delta(2,a,2). delta(2,b,2). start(0). final(2). /* X ---A---> Y */

18. Write a program for browsing, reading and writing the data in file.
browse :seeing(Old), see(user), write('Enter name see(File), repeat, read(Data), process(Data), seen, see(Old), !. /* save for later */ of file to browse: '), read(File), /* open this file */ /* read from File */ /* close File */ /* previous read source */ /* stop now */

process(end_of_file) :- !. process(Data) :- write(Data), write('.'), nl, fail. my_save(ToFile) :telling(Old), tell(ToFile), listing, told, tell(Old).

/* /* /* /* /*

current write output */ open this file */ list all clauses in memory */ close ToFile */ resume this output */

%% %% Load a file or Prolog terms into a List. %% file_to_list(FILE,LIST) :see(FILE), inquire([],R), % gather terms from file reverse(R,LIST), seen. inquire(IN,OUT):read(Data), (Data == end_of_file -> % done OUT = IN ; % more inquire([Data|IN],OUT) ) .

19. Write a program for counting the number of literals in a file


start:- write('Text is in file= '),read(F), write('What letter do you want to count='),read(Let), see(F),process(Let,0),seen. process(Let,N):- get0(Char), process1(Char,Let,N). /* The end of the file is -1 */ process1(-1,Let,N):- write('This letter occurs '),write(N), write(' times.'),!. process1(Char,Let,K):- name(Let,[Char]),K1 is K+1,process(Let,K1). process1(Char,Let,K):- process(Let,K).

20. Write a program for finding whether the given word is prefix, suffix of the givn another word.
put_prefix(P,C,R):name(P,Pcode),name(C,Ccode), append(Pcode,Ccode,Rcode), name(R,Rcode). name(S,Scode),name(C,Ccode), append(Ccode,Scode,Rcode), name(R,Rcode).

put_suffix(S,C,R):-

21) Write a program for finding the maximum number of permuted words possible from the given word.
add(X,L,[X|L]). add(X,[L|H],[L|R]):- add(X,H,R). permut([],[]). permut([L|H],R):- permut(H,R1),add(L,R1,R). permutations(L,R):- findall(P,permut(L,P),R).

22) Write a program for finding the possible number of combination words from a given word.
comb(N,L,X):- length(X,N),mem1(X,L). mem1([],Y). mem1([H|T],Y):- member(H,Y),rest(H,Y,New),mem1(T,New).

?- comb(2,[a,b,c],I). I = [a,b] I = [a,c] I = [b,c]

23) Write a program for implementing the Anagram.


start:write('Write a word='),read(X),nl, name(X,L),permut(L,R), name(Cuv,R),write(Cuv),tab(5),fail. add(X,L,[X|L]). add(X,[L|H],[L|R]):- add(X,H,R). permut([],[]). permut([L|H],R):- permut(H,R1),add(L,R1,R).

You might also like