Você está na página 1de 5

Name: Dil Prasad Kuwor Date: 10/5/2010

Roll no. : 02/064


Subject: Artificial Intelligence
Assignment no. : Lab No. 1

Q.1 Write a program to find the hcf of two numbers.

Prolog Code:

PREDICATES
hcf(integer,integer,integer)
CLAUSES
hcf(X,Y,R):-
X mod Y=0,R=Y.
hcf(X,Y,R):-
X mod Y<>0,S=X mod Y,hcf(Y,S,R).
GOAL
hcf(10,15,A).

Output:

Q.2 Write a program of your choice. Give some facts and use some rules to make a
few deductions.

Prolog Code:
PREDICATES
gson(string,string)
father(string,string)
CLAUSES
father("HARI","SHYAM").
father("SHYAM","RAM").
gson(X,Z):-
father(Z,Y),father(Y,X).
GOAL
gson("RAM",X).

Output:

1
Q.3 Write a program to add the content of an integer list and display it.

Prolog Code:
DOMAINS
list=integer*
PREDICATES
add(list,integer)
CLAUSES
add([],0).
add([H|T],S):-
add(T,S1),S=S1+H.
GOAL
add([2,3,5],X).

Output:

Q. 4 Write a program to append two lists.

Prolog Code:
DOMAINS
list=integer*
PREDICATES
append(list,list,list)
CLAUSES
append([H|T],Y,[H|A]):-
append(T,Y,A),!.
append([],Y,Y).
GOAL
append([2,3,4],[2,3,4,5,6,8],X).

Output:

2
Q. 5 Write a program which takes a list of integers and displays only 1s and 2s. ( If the
input is [1,2,4,5,2,4,5,1,1] the solution list should be [1,2,2,1,1]. )

Prolog 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:

Q. 6 Write a program to delete a given element from the list.

Prolog Code:
DOMAINS
list=integer*
PREDICATES
delete(integer,list,list)
CLAUSES
delete(X,[],[]).
delete(X,[H|T],[H|A]):-
not(X=H),delete(X,T,A).
delete(X,[X|T],A):-
delete(X,T,A).
GOAL
delete(2,[2,3,4,5,6,8],X).
Output:

3
Q. 7 Write a program to find the factorial of a program.

Prolog Code:
PREDICATES
factorial(integer,integer)
CLAUSES
factorial(X,Y):-
X=0,Y=1.
factorial(X,Y):-
X<>0,A=X-1,factorial(A,R),Y=R*X.
GOAL
factorial(5,X).

Output:

Q. 8 Write a program to find nth Fibonacci number

Prolog Code:
DOMAINS
list=integer*.
PREDICATES
fibonacci(integer,integer)
display(integer)
find_fibo(integer)
CLAUSES
find_fibo(X):-
fibonacci(X,R),
write("The"),write(" "),write(X),write("th fibonacci no is:"),write(" "),
display(R),nl.
fibonacci(1,1).
fibonacci(2,1).
fibonacci(X,F):-
X>2,
X1=X-1,
X2=X-2,
fibonacci(X1,F1),
fibonacci(X2,F2),
F=F1+F2.
display(F):-
write(F).

4
GOAL
find_fibo(5).

Output:

The End 

Você também pode gostar