0% found this document useful (0 votes)
10 views14 pages

CSC 17 07 Suman

The document contains a collection of Prolog programs that implement various algorithms and data structures, including arithmetic operations, list manipulations, and graph representations. Each program is presented with source code and expected output, covering functionalities such as calculating the sum, finding the maximum, generating Fibonacci numbers, and checking for palindromes. Additionally, it includes programs for manipulating lists, such as removing duplicates, merging, and deleting elements.

Uploaded by

ROHAN PAL
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)
10 views14 pages

CSC 17 07 Suman

The document contains a collection of Prolog programs that implement various algorithms and data structures, including arithmetic operations, list manipulations, and graph representations. Each program is presented with source code and expected output, covering functionalities such as calculating the sum, finding the maximum, generating Fibonacci numbers, and checking for palindromes. Additionally, it includes programs for manipulating lists, such as removing duplicates, merging, and deleting elements.

Uploaded by

ROHAN PAL
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/ 14

ARTIFICIAL INTELLIGENCE PROGRAMS

Q1. Sum of 2 numbers


Source code:
sum(X,Y):- S is X+Y,write(S).
Output:

Q2. Max of 2 numbers


Source code:
max(X,Y,M):- X>Y, M is X.
max(_,Y,M):- M is Y.
Output:

Q3. Factorial of a number


Source code:
fact(0,1).
fact(N,R):-N1 is N-1, fact(N1,R1), R is N*R1.
Output:
Q4. Nth term of fibonacci series
Source code:
fibo(1,0).
fibo(2,1).
fibo(P,N):- P1 is P-1,fibo(P1,R1),P2 is P-2,fibo(P2,R2),N is R1+R2.
Output:

Q5. GCD of two numbers


Source code:
gcd(X,X,X).
gcd(X,Y,D):- X<Y, gcd(Y,X,D).
gcd(X,Y,D):- X>Y,Y1 is X-Y,gcd(Y,Y1,D).
Output:

Q6. Power of a number


Source code:
power(Num,0,Ans):-Ans is 1.
power(Num,Pow,Ans):-P is Pow-1,power(Num,P,Ans1),Ans is Ans1*Num.
Output:
Q7. Multiplication of two numbers
Source code:
multi(N1,1,N1).
multi(N1,N2,Ans):- Temp is N2-1, multi(N1,Temp,Ans1),Ans is Ans1+N1.
Output:

Q8. Tower of Hanoi.


Source code:
mov(0,_,_,_):-!.
mov(N,L,C,R):- N1 is N-1,mov(N1,L,R,C),write("Move disk "),write(N),write(" from
"),write(L),write(" to "),write(C),nl,mov(N1,C,L,R).

toh(N):-mov(N,left,center,right).

%L is treated as Source,R as destination and C as intermediate peg.


Output:

Q9. Cyclic directed graph.


Source code:
edge(p,q).
%edge(q,r).
edge(q,s).
edge(s,t).
route(X,X):-write(X),!.

%route(X,Y):-edge(X,Y),write(X),write("->"),write(Y),!.
route(X,Y):-edge(X,Z),write(X),write("->"),route(Z,Y).

Output:

Q10. X is a member of list or not.


Source code:
ismember(X,[X|T]).
ismember(X,[H|T]):-ismember(X,T).

Output:

Q11. Concatenate 2 lists and store in 3 rd list.


Source code:
conc([],L,L).
conc([H|T],L2,[H|L3]):-conc(T,L2,L3).

Output:
Commands to go back from trace.

Q12. Reverse a list.


Source code:
conc([],L,L).
conc([H|T],L2,[H|L3]):-conc(T,L2,L3).

revlist([],[]).
revlist([H|T],R):-revlist(T,Trev),conc(Trev,[H],R).
Output:
Q13. Check whether a list is palindrome or not.
Source code:
conc([],L,L).
conc([H|T],L2,[H|L3]):-conc(T,L2,L3).

revlist([],[]).
revlist([H|T],R):-revlist(T,Trev),conc(Trev,[H],R).

palind(L):-revlist(L,R), L=R -> write("It is a Palindrome");write("It is not a


palindrome").

Output:

Q14. Sum of elements of given list.


Source code:
sumlist([],0).
sumlist([H\T],S):-sumlist(T,S1),S is H+S1.

Output:
Q15. Even length or odd length list.
Source Code:
len([],0).
len([H|T],N) :- len(T,N1), N is N1+1.
evenlength(List):- len(List,N),mod(N,2)=:=0,write("The list ids even length").
oddlength(List):-len(List,N),mod(N,2)=\=0,write("The list is odd length").
check_list(L):- evenlength(L);oddlength(L).
/* ; mean either goal can be true */

Output:

Q16. Nth element in a list.


Source Code:

nth_element(1,[H|T],H).
nth_element(N,[H|T],X):-N1 is N-1, nth_element(N1,T,X).

Output:
Q17. Write a Prolog program to implement remdup( L, R) to remove duplicates from a
list L to generate a list R.
Source code:
ismember(X,[X|T]).
ismember(X,[H|T]):-ismember(X,T).
remove_dups([],[]).
remove_dups([H|T],R):-ismember(H,T),remove_dups(T,R).
remove_dups([H|T],[H|R]):-not(ismember(H,T)),remove_dups(T,R).
Output:

Q18. Find maximum element in list.


Source code:
max(X,Y,M) :- (X=Y, M is X;
X>Y , M is X;
M is Y
).

maxlist([H|[]], H).
maxlist([H|T],M) :- maxlist(T,M1), max(H,M1,M).
Output:
Q19. Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I
into Nth position of list L to generate a list R.
Source code :
insert_nth(I, 1, L, [I|L]).
insert_nth(I, N, [H|T], [H|R]):- N1 is N-1,insert_nth(I, N1, T, R).
Output :

Q20. Write a Program in PROLOG to implement sublist(S, L) that checks whether the
list S is the sublist of list L or not. (Check for sequence or the part in the same order).
Source code:
sublist([],L).
sublist(S,[]):- false.
sublist([H1|T1],[H1|T2]):- sublist(T1,T2).
sublist([H1|T1],[H2|T2]):- sublist([H1|T1],T2).
Output:
Q21. : Write a Prolog program to implement delete_nth (N, L, R) that removes the
element on Nth position from a list L to generate a list R.
Source code:
delete_nth(1, [H|T], T).
delete_nth(N, [H|T], [H|R]):- N1 is N-1,delete_nth(N1, T, R).

Output:

Q22. Write a program in PROLOG to implement delete_all (X, L, R) where X denotes


the element whose all occurrences has to be deleted from list L to obtain list R.
Source code:
delete_all(X, [], []).
delete_all(X, [X|T], R):- delete_all(X, T, R).
delete_all(X, [H|T], [H|R]):- delete_all(X, T, R).

Output:
Q23. Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first
ordered list and L2 is second ordered list and L3 represents the merged list.

Source code:
merge([],L,L):-!,write("Abc").
merge([H1|T1],[H2|T2],[H3|T3]):-H1>H2,H3 is H2,merge([H1|T1],T2,T3).
merge([H1|T1],[H2|T2],[H3|T3]):-H1<H2,H3 is H1,merge(T1,[H2|T2],T3).
merge([H1|T1],[H2|T2],[H3,H4|T3]):-H1=H2,H3 is H1,H4 is H2,merge(T1,T2,T3).

Output:

Q24. Write a PROLOG program that will take grammar rules in the following format:
NT -> (NT | T)*
Where NT is any nonterminal, T is any terminal and Kleene star (*) signifies any
number of repetitions, and generate the corresponding top-down parser, that is:
sentence -> noun-phrase, verb-phrase
determiner -> [the]will generate the following:
sentence (I, O) :- noun-phrase(I,R), verb-phrase (R,O).
determiner ([the|X], X) :- !.
Source code:
sentence-->np,vp.
np-->det,noun.
vp-->verb.
vp-->verb,np.

det-->[a].
det-->[the].
det-->[an].
noun-->[boy].
noun-->[girl].
noun-->[song].
noun-->[apple].

verb-->[sing].
verb-->[sings].
verb-->[eats].

Output:

Q25. Write a prolog program that implements Semantic Networks.


Source code:
%Facts%
mat(mat1).
sat_on(Cat1,mat1).
cat(Cat1).
cat(tom).
color(tom,black).
owns(john,tom).
bird(bird1).
caught(tom,bird1).
is_coloured(tom,ginger).

%Rules%

animal(X):- mammal(X).
animal(X):- bird(X).
mammal(X):- cat(X).
have(X,fur):- mammal(X).
likes(X,cream):- cat(X).

Output:

Source code:
(b)
ako(mammal,animal).
ako(bird,mammal).
ako(elephant,mammal).
is_a(tweety,bird).
is_a(clyde,elephant).
is_a(neil,elephant).
can(animal,breathe).
can(animal,move).
can(bird,fly).
can(tweety,sing).
has(neil,three_legs).
has(bird,feather).
has(bird,wing).
has(elephant,four_legs).
has(elephant,one_trunk).
has(elephant,one_tail).
has(animal,skin).
has(mammal,head).
color(elephant,grey).
color(tweety,yellow).
like(neil,apple).
%Rules
check(O,P,V):- G=..[P,O,V],G,!;!.
check(O,P,V):-is_a(O,X),check(X,P,V);ako(O,X),check(X,P,V).

Output:

You might also like