Professional Documents
Culture Documents
071BCT532
ARTIFICIAL INTELLIGENCE
Lab-1
Code :
PREDICATES
gcd(integer,integer,integer)
CLAUSES
gcd(A,B,C):-
A mod B=0,C=B.
gcd(A,B,C):-
A mod B<>0,S=A mod B,gcd(B,S,C).
GOAL
gcd(20,25,C).
Output :
C=5
1 Solution
2. Write a program of your choice. Give some facts and use some rules to make afew deductions.
This program is to find maximum of X and Y.
Code:
PREDICATES
go.
max(integer,integer,integer).
CLAUSES
go:-
readint(X), readint(Y),
max(X,Y,_).
max(X,Y,X):- X>Y ,!.
max(X,Y,Y).
GOAL
max(22,33,X).
Output:
X=33
1 Solution
Sagar Bhandari
071BCT532
3. Write a program to add the content of an integer list and display it.
Code:
DOMAINS
list=integer*
PREDICATES
add(list,integer)
CLAUSES
add([],0).
add([H|T],P):-
add(T,P1),P=P1+H.
GOAL
add([5,1,3],D).
Output:
D=9
1 Solution
findlen([],X):-
X=0.
findlen([X|Tail],Count):-
findlen(Tail,Prev),
Count = Prev + 1.
GOAL
len([a,b,c,d,e]).
Output:
Length of list : 5
1 Solution
Sagar Bhandari
071BCT532
DOMAINS
list=integer*
PREDICATES
app(list,list,list)
CLAUSES
app([H|T],L,[H|A]):-
append(T,L,A),!.append([],L,L).
GOAL
app([1,3,5],[2,4,6,8,10,12],D).
Outcome:
D=[1,3,5,2,4,6,8,10,12]
1 Solution
6. Write a program which takes a list of integers and displays only 1s and 2s. ( If theinput is
[1,2,4,5,2,4,5,1,1] the solution list should be [1,2,2,1,1]. )
Code:
DOMAINS
list=integer*
PREDICATES
compare(list,list).
CLAUSES
compare([],[]).
compare([H|T],A):-
not(H=1),not(H=2),compare(T,A),!.
compare([H|T],[H|A]):-
compare(T,A).
GOAL
compare([1,2,4,5,2,4,5,1,1],X).
Output:
X=[1,2,2,1,1]
1 Solution
delete(1,[1,5,4,2,6,8],X).
Output:
X=[5,4,2,6,8]
1 Solution
Conclusion :
In this lab session we were able to take the knowledge of basic prolog programming tool. We
studied the various data types of prolog and wrote the simple prolog program as introductory lab.