0% found this document useful (0 votes)
6 views

Assignment (Practical)

Uploaded by

srav69003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment (Practical)

Uploaded by

srav69003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Date: 02-11-2023

Assignment 1 Write a pro-log program to calculate the


sum of two numbers.

#Source Code:

add(X, Y, Z):- Z is X+Y.

Output:

Teacher’s Signature

1 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 2 Write a pro-log program to find the maximum
of three numbers.

#Source Code:

max(A, B, C):- A>B, A>C, write('Maximum is: '), write(A).


max(A, B, C):- B>A, B>C, write('Maximum is: '), write(B).
max(A, B, C):- C>B, C>A, write('Maximum is: '), write(C).

Output:

Teacher’s Signature

2 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 3 Write a pro-log program to calculate the
factorial of a given number.

#Source Code:

fact(0, 1).
fact(N, R):- N>0, N1 is N-1, fact(N1, F1), R is N*F1.

Output:

Teacher’s Signature

3 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 4 Write a pro-log program to calculate the
nth Fibonacci number.

#Source Code:

fibo(1, 0).
fibo(2, 1).
fibo(N, R):- N>2, N1 is N-1, N2 is N-2, fibo(N1, R1), fibo(N2, R2), R is R1+R2.

Output:

Teacher’s Signature

4 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 5 Write a pro-log program, insert_nth(item,
n, into_list, result) that asserts that result is the
list into_list with item inserted as the n‘th element
into every list at all levels.

#Source Code:

insert_nth(Item, N, IntoList, Result) :- insert_nth_(Item, N, IntoList, Result).


insert_nth_(Item, 1, List, [Item|List]) :- !.
insert_nth_(Item, N, [H|T], [H|R]) :- N1 is N - 1, insert_nth_(Item, N1, T, R).
insert_nth_(Item, N, [], [Item]) :- N > 1, !, N1 is N - 1, insert_nth_(Item, N1, [], R).
insert_nth_(Item, _, List, List) :- is_list(List), !, insert_nth(Item, _, List, _).

Output:

Teacher’s Signature

5 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 6 Write a Pro-log program to remove the Nth
item from a list.

#Source Code:

remove_nth(N, List, Result) :- remove_nth_(N, List, Result).


remove_nth_(1, [_|T], T) :- !.
remove_nth_(N, [H|T], [H|R]) :- N1 is N - 1, remove_nth_(N1, T, R).

Output:

Teacher’s Signature

6 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 7 Write a Pro-log program, remove-nth(Before,
After) that asserts the After list is the Before list
with the removal of every n’th item from every list at
all levels.

#Source Code:

add(X, Y, Z):- Z is X+Y.

Output:

Teacher’s Signature

7 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 8 Write a Pro-log program to implement append
for two lists.

#Source Code:

append([], Ys, Ys).


append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).

Output:

Teacher’s Signature

8 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 9 Write a Pro-log program to implement
palindrome(List).

#Source Code:

palindrome(L) :- reverse(L, L).

Output:

Teacher’s Signature

9 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 10 Write a Pro-log program to implement max
(X, Y, Max) so that Max is the greater of two numbers
X and Y.

#Source Code:

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

Output:

Teacher’s Signature

10 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 11 Write a Pro-log program to implement
maxlist (List,Max)so that Max is the greatest number
in the list of numbers List.

#Source Code:

maxlist([X], X).
maxlist([X|Xs], Max) :- maxlist(Xs, MaxXs), max(X, MaxXs, Max).

Output:

Teacher’s Signature

11 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 12 Write a Pro-log program to implement
sumlist (List,Sum) so that Sum is the sum of a given
list of numbers List.

#Source Code:

sumlist(List, Sum) :- sumlist_acc(List, 0, Sum).


sumlist_acc([], Acc, Acc).
sumlist_acc([X|Xs], Acc, Sum) :- AccX is Acc + X, sumlist_acc(Xs, AccX, Sum).

Output:

Teacher’s Signature

12 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 13 Write a Pro-log program to implement two
predicates evenlength(List) and oddlength(List) so
that they are true if their argument is a list of even
or odd length respectively.

#Source Code:

evenlength([]).
evenlength([_|Xs]) :- oddlength(Xs).
oddlength([_]).
oddlength([_|Xs]) :- evenlength(Xs).

Output:

Teacher’s Signature

13 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 14 Write a Pro-log program to implement
reverse (List,ReversedList) that reverses lists.

#Source Code:

reverse(List, ReversedList) :- reverse_acc(List, [], ReversedList).


reverse_acc([], Acc, Acc).
reverse_acc([X|Xs], Acc, ReversedList) :- reverse_acc(Xs, [X|Acc], ReversedList).

Output:

Teacher’s Signature

14 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 15 Write a Pro-log program to implement
maxlist (List,Max) so that Max is the greatest number
in the list of numbers List using cut predicate.

#Source Code:

maxlist([X], X).
maxlist([X|Xs], Max) :- maxlist(Xs, Y), (X > Y -> Max = X ; Max = Y),!.

Output:

Teacher’s Signature

15 | C13P : Artificial Intelligence Lab


Date: 02-11-2023
Assignment 16 Write a Pro-log program to implement GCD
of two numbers.

#Source Code:

gcd(X, Y, Z):- X=:=Y, Z is X.


gcd(X, Y, R):- X<Y, Y1 is Y-X, gcd(X, Y1, R).
gcd(X, Y, R):- X>Y, gcd(Y, X, R).

Output:

Teacher’s Signature

16 | C13P : Artificial Intelligence Lab

You might also like