Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Ai Programs Prolog Programs

This document contains code snippets for 12 AI programs: 1) Finding the maximum of two numbers 2) Calculating the factorial of a number 3) Computing the Nth Fibonacci number 4) Inserting an item into a list at a given position 5) Removing an item from a list at a given position 6) Removing an item from a list before and after a given position 7) Implementing list concatenation 8) Checking if a list is a palindrome 9) Finding the maximum item in a list 10) Summing the items in a list 11) Defining predicates for even-length and odd-length lists 12) Reversing a list

Uploaded by

Sweta Umrao
Copyright
© © All Rights Reserved
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)
45 views9 pages

Ai Programs Prolog Programs

This document contains code snippets for 12 AI programs: 1) Finding the maximum of two numbers 2) Calculating the factorial of a number 3) Computing the Nth Fibonacci number 4) Inserting an item into a list at a given position 5) Removing an item from a list at a given position 6) Removing an item from a list before and after a given position 7) Implementing list concatenation 8) Checking if a list is a palindrome 9) Finding the maximum item in a list 10) Summing the items in a list 11) Defining predicates for even-length and odd-length lists 12) Reversing a list

Uploaded by

Sweta Umrao
Copyright
© © All Rights Reserved
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

AI Programs

1.Maximum of two Numbers:


Code:
max(X,Y):-
(
X=Y ->
write('both are equal');
X>Y ->
(
Z is X,
write(Z)
);
(
Z is Y,
write(Z)
)
Output:

).

2.Factorial of Number:
Code:
fact(0,1).
fact(N,F):-
(

% The below is for +ve factorial.


N>0 ->
(
N1 is N-1,
fact(N1,F1),
F is N*F1
)

).
Output:

3.Fibonacci of Nth Number:


Code:
fib(0,0).
fib(1,1).
fib(N,NT):-
N>1,
A is N-1,
B is N-2,
fib(A,AT),fib(B,BT),
NT is AT+BT.
Output:

4.Insert Nth item:


Code:
mem(X,[X|_]).
mem(X,[_|T]):- mem(X,T).
/* insert a number in the list. */
insert(L,[X|Y],[L|_]).
insert(L,P,[X|Y],[X|M]):-
P>1,
P1 is P-1,
insert(L,P1,Y,M).
insert(L,1,[X|Y],M):- append([L],[X|Y],M).
Output:
5.Remove Nth item:
Code:
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).
Output:

6.Remove Nth item(Before,After):


Code:
delte(1,[_|T],T).
delte(P,[X|Y],[X|R]):-
P1 is P-1,
delte(P1,Y,R).

/* delete before and after. */


daltob(P,L,R):-
P1 is P-1,
delte(P1,L,R1),
/* delete before. */
delte(P,R1,R).
Output:

7.Implement Concatenation:
Code:
conc([],L,L).
conc([X|M],N,[X|Q]):-
conc(M,N,Q).
Output:

8.Implement Palindrome:
Code:
palind([]):- write('palindrome').
palind([_]):- write('palindrome').
palind(L) :-
append([H|T], [H], L),
palind(T)
;
write('Not a palindrome').
Output:

9.Implement maxlist(List,Max):
Code:
maxlist([H|T],R):-
length(T,L),
L>0 ->
(
maxlist(T,R1),
(
H > R1 ->
R is H
;
R is R1
)
)
;
R is H.
Output:

10.Implement Sumlist(List,Max):
Code:
sumlist([],0).

sumlist([H|T],R):-
sumlist(T,R1),
R is H+R1.
Output:

11.Implement Two predicates(evenlength,oddlength):


Code:
evenlength( [ ] ).
evenlength( [ H|T] ):-
oddlength( T ).
oddlength( [ _ ] ).
oddlength( [ H|T ] ) :-
evenlength( T).
Output:

11.Implement Reverse List:


Code:
reverse([H|T],R):-
length(T,L),
L>0 ->
(
reverse(T,R1),

R is H
)
;
R is H.

Output:
12.Implement the cut predicate:
Code:
max(X,Y,X):-X>=Y,!.
max(X,Y,Y):-X<Y.
Output:

You might also like