0% found this document useful (0 votes)
92 views9 pages

AI

The document describes algorithms for sorting lists in Prolog, including bubble sort, merge sort, and insertion sort. Code samples are provided for each algorithm that take a list as input and return the sorted list. For bubble sort, a predicate is defined to check if a list is in order. For merge sort, predicates are defined for splitting a list, merging two sorted lists, and calling a merge sort recursively. For insertion sort, predicates are defined for inserting an element into a sorted list and appending lists. The output shows examples of calling each sorting algorithm.

Uploaded by

Arijit Das
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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views9 pages

AI

The document describes algorithms for sorting lists in Prolog, including bubble sort, merge sort, and insertion sort. Code samples are provided for each algorithm that take a list as input and return the sorted list. For bubble sort, a predicate is defined to check if a list is in order. For merge sort, predicates are defined for splitting a list, merging two sorted lists, and calling a merge sort recursively. For insertion sort, predicates are defined for inserting an element into a sorted list and appending lists. The output shows examples of calling each sorting algorithm.

Uploaded by

Arijit Das
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 PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment AI-1

TITLE: Study of Arithmetical and Relational Operation wih Prolog Interpreeter

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

BLOCK DIAGRAM OF PROCESSING

GOAL./Input EXECUTER True/False

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

TITLE: Demo of Prolog Program For Verification Of Rules

THEORY:
Conclusion:-Conition

GOAL./Input EXECUTER True/False

Input Logic

INPUT LOGIC- Content of .pl File

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

TITLE: Write a prolog program to find


1) Factorial of a number
2) Fibonacci series
3) GCD of two variable.

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

TITLE: List processing.


1) Member of a list
2) Length of a list
3) Multiplication of the elements of the list.

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.

[3] 28 ?- mem(3, [1, 2, 3]).


true ;

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

TITLE: Represent a b-tree in Prolog.


Use the sample btree
20

30 50

40 60

THEORY: A btree can be represented by a function


btree(root,leftchild,rightchild), where each of the left child and right child are
another btree as well and root is the node id of the root node.

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

% e:/XXX/Documents/Prolog/btree.pl compiled 0.00 sec, 248 bytes

?-
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

TITLE: In-order, Pre-order and Post-order traversal of trees

THEORY: A tree can be traversed in vaious ways. A genral way to describe


tree traversal in prolog is
btree(T,Y) -> List
Above predicate is true when the list y will be unified by traversing.

CODES

Pre-Order Traversal pre_btree(T,Y)

pre_btree(void,[])
pre_btree(btree(X,L,R),Y):-pre_btree(L,L1),pre_btree(R,R1),append(X|L1,R1,Y)

In-Order Traversal pre_btree(T,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-Order Traversal pre_btree(T,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 %

bbl(L, L):- inord(L).


bbl(L1, L2):-
append(X, [A, B | Y], L1),
A > B,
append(X, [B, A | Y], T),
bbl(T, L2).

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 %

split([], [], []).


split([X], [X], []).
split([A, B | T], [A|P1], [B|P2]):-
split(T, P1, P2).

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).

part(_, [], [], []).


part(E, [H|T], [H|T1], L2):-
H < E,
part(E, T, T1, L2).
part(E, [H|T], L1, [H|T2]):-
H >= E,
part(E, T, L1, T2).

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] ;

You might also like