Tutorial 2 of Artificial Intelligence
1.
Write program to display
digits 1 to 9 also in reverse order using recursion to understand winding down.
predicates
count(integer)
go
clauses
go:-
write("Winding
Down"),nl,
count(1),
write("1"),nl.
count(9):-
write("9"),nl,
write("Now
unwinding"),nl.
count(N):-
write(N),nl,
A
= N+1,
count(A),
write(A),nl.
2.
Write a program to perform login operation using:
i.
Recursion
ii.
repeat predicate
i.using
Recursion
predicates
getip(symbol,symbol)
login(symbol,symbol)
go
clauses
getip(U,P):-
write("Enter
Username : "),
readln(U),
write("Enter
Password : "),
readln(P),
login(U,P).
go:-
getip(_,_),
write("Login
Succeeded"),nl.
go:-
write("Invalid
Username or Password"),nl.
login(ce,ce).
login(ec,ec).
login(ic,ic).
Repeat predicate
predicates
getip(symbol,symbol)
login(symbol,symbol)
go
repeat
clauses
repeat:-
go.
getip(U,P):-
write("Enter
Username : "),
readln(U),
write("Enter
Password : "),
readln(P),
login(U,P).
go:-
getip(_,_),
write("Login
Succeeded"),nl.
go:-
write("Invalid
Username or Password"),nl,
repeat.
login(ce,ce).
login(ec,ec).
login(ic,ic).
3.
Write a program to perform such that user is allowed to input username and password maximum three times.
predicates
getip(symbol,symbol)
login(symbol,symbol)
count(integer)
clauses
count(3).
count(N):-
getip(_,_),
write("Login
succeeded"),nl.
count(N):-
write("Invalid
Username and Password"),nl,
A
= N+1,
count(A).
getip(U,P):-
write("Enter
Username : "),
readln(U),
write("Enter
Password : "),
readln(P),
login(U,P).
login(ce,ce).
login(ec,ec).
login(ic,ic).
4.
Define predicate location(City,State). Write a
program such that it displays all the locations except city “Delhi”. Using:
i.
not predicate
ii.
Combination of cut and fail.
predicates
find
checkcity(string)
location(string,string)
clauses
location("Jaypur","Rajsthan").
location("Delhi","UP").
location("Rajkot","Gujarat").
location("Bhopal","MP").
find:-
writef("%-10%6","CITY","STATE"),nl,
writef("----------------"),nl,
fail.
find:-
location(C,S),
checkcity(C),
writef("%-10%5",C,S),nl,
fail.
find.
checkcity("Delhi"):-
!,fail.
checkcity(_).