Professional Documents
Culture Documents
THEORY: Prolog is a simple, yet powerful programming languge based on the principles of
first order predicate logic, Prolog is for supbolic computation and processing. Some application
areas are
1.Natural Language Programming.
2.Compiler Construction.
3.The developement of expert system.
4.Work in the area of Computer Algebra.
5.The developement of Computer Parallel Architecture
6.Database Systems
Language Programming
Library Module
RESULT
1. Addition
? X is 3+2.
X=5
2. Substraction
? X is 3-2
X=1
3. Multiplication
? X is 3*2
X=6
4. Division
? X is 3/2.
X=1
5. Exponent
? X is 3**2
X=8
6. Modulus
? X is mod(3,2)
X=1
7. Equals
? X 5=:=5.
Yes
Assignment AI-2
THEORY:
Conclusion:-Conition
Input Logic
equals(X,Y):-X=Y.
positive(X):-X>0.
inrange(X,Y,Z):- Y>x,Y<Z.
square(X,Y):-X is Y*Y.
exp(X,Y,X):- X is Y**Z.
negativc(X):- X<0.
Prolog EXECUTION
? - equals(5,4).
No
? - positive(5).
Yes
? - inrange(10,2,1).
Yes
? - inrange(10,12,1).
No
? - square(4,2).
Yes
? - exp(9,3,2).
Yes
? - negativc(-5).
Yes
Assignment AI-3
CODE 1
%% predicates %%
fact(0, 1).
%% clauses %%
fact(N, F):-
N1 is N - 1,
fact(N1, F1),
F is F1 * N.
Output:
% e:/XXX/Documents/Prolog/fact.pl compiled 0.00 sec, 1,076 bytes
1 ?- fact(9, X).
X = 362880 ;
CODE 2
%% Fibonacci %%
fib(0, 0).
fib(1, 1).
fib(N, R):-
N1 is N - 1,
N2 is N - 2,
fib(N1, R1),
fib(N2, R2),
R is R1 + R2.
Output:
% e:/YYY/Documents/Prolog/fib.pl compiled 0.00 sec, 704 bytes
5 ?- fib(3, X).
X=2;
CODE 3
%% gcd %%
gcd(X, X, X).
gcd(X, Y, G):-
X > Y,
X1 is X - Y,
gcd(Y, X1, G).
gcd(X, Y, G):-
Y > X,
Y1 is Y - X,
gcd(X, Y1, G).
Output:
% e:/ZZZ/documents/prolog/gcd compiled 0.00 sec, 464 bytes
[3] 16 ?- gcd(3, 7, X).
X=1.
Assignment AI-4
Code 1:
% member of a list %
mem(M, [M|_]).
mem(M, [_|T]):-
mem(M, T).
Output:
% e:/XXX/Documents/Prolog/mem.pl compiled 0.00 sec, 288 bytes
[3] 27 ?- mem(5, [1, 2, 3]).
false.
Code 2:
% length of a list %
len([], 0).
len([_|T], L):-
len(T, L1),
L is L1 + 1.
Output:
% e:/XXX/Documents/Prolog/len.pl compiled 0.00 sec, 648 bytes
[3] 29 ?- len([1, 2, 3], X).
X = 3.
Code 3:
% product of a list %
prod([], 1).
prod([X|L], P):-
prod(L, P1),
P is X * P1.
Output:
% e:/XXX/Documents/Prolog/prod.pl compiled 0.00 sec, 592 bytes
[3] 30 ?- prod([3, 4, 5], P).
P = 60.
Assignment AI-5
30 50
40 60
CODE
%btree.pl%
mem_btree(X,btree(X,L,R))
mem_btree(X,btree(Y,L,R)):-mem_btree(X,L)
mem_btree(X,btree(Y,L,R)):-mem_btree(X,R)
OUTPUT
?-
mem_btree(20,btree(30,btree(40,void,void),void),btree(50,void,btree(60,void,v
oid))).
X=20
X=30
X=40
X=50
X=60
Assignment AI-6
CODES
pre_btree(void,[])
pre_btree(btree(X,L,R),Y):-pre_btree(L,L1),pre_btree(R,R1),append(X|L1,R1,Y)
in_btree(void,[])
in_btree(btree(X,L,R),Y):-in_btree(L,L1),in_btree(R,R1)
in_btree(btree(X,L,R),Y):-in_btree(L,L1),in_btree(R,R1),append(X|L1,R1,Y)
post_btree(void,[])
post_btree(btree(X,L,R),Y):-post_btree(L,L1),post_btree(R,R1),append(X|
L1,R1,Y)
Append
append(L1,L2,G)
append([],P,P)
append([H|T],P1[H|N)):-append(T,P,N)
Assignment AI-8
TITLE: Sorting algorithms.
1) Bubble sort
2) Merge sort
3) Insertion sort.
Code 1
% Bubble sort %
append([], L, L).
append([H | T1], L2, [H | T3]):-
append(T1, L2, T3).
inord([]).
inord([_]).
inord([A, B | T]):-
A =< B,
inord([B | T]).
Output:
% e:/XXX/Documents/Prolog/bubble.pl compiled 0.00 sec, 820 bytes
[3] 31 ?- bbl([3, 1, 2], L).
L = [1, 2, 3] ;
Code 2
% merge sort %
merge([], L, L).
merge(L, [], L).
merge([A|T1], [B|T2], [A|L2]):-
A =< B,
merge(T1, [B|T2], L2).
merge([A|T1], [B|T2], [B|L2]):-
A > B,
merge([A|T1], T2, L2).
msort([], []).
msort([A], [A]).
msort(L1, L2):-
split(L1, P1, P2),
msort(P1, S1),
msort(P2, S2),
merge(S1, S2, L2).
Output:
% e:/XXX/documents/prolog/merge compiled 0.00 sec, 136 bytes
[3] 37 ?- msort([1, 2, 3, 4, 5, 6, 7], L).
L = [1, 2, 3, 4, 5, 6, 7] .
Code 3
% insertion sort %
insort([], []).
insort([H|T], F):-
insort(T, L),
part(H, L, L1, L2),
append(L1, [H|L2], F).
append([], L, L).
append([H|T1], L2, [H|T3]):-
append(T1, L2, T3).
Output:
% e:/XXX/Documents/Prolog/isort.pl compiled 0.00 sec, 876 bytes
[3] 39 ?- insort([1, 2, 3, 4, 5, 6, 7], L).
L = [1, 2, 3, 4, 5, 6, 7] ;