Sie sind auf Seite 1von 12

AIM: WRITE A PROGRAM FOR FOLLOWING TASK :1] STUDENTS WHO ARE LIVING IN RAJKOT 2] STUDENTS WHOSE AGE

IS GREATER THAN 15 AND LIVING IN RAJKOT 3] STUDENTS WHO HAS MORE THAN 60 % domains predicates student(symbol,symbol,integer,integer) clauses student("Sita","Rajkot",20,70). student("Rita","Delhi",35,55). student("Nita","Bangalore",19,90). student("Mita","Rajkot",55,30). student("Hita","Mumbai",60,67).

Output:Goal: Student (X, Rajkot, Y, Z). X = Sita Y= 20 Z=70 X = Mita Y= 20 Z=70

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 1

Practical:-3
AIM: A Simple Program Using Rule predicates food(symbol) taste(symbol, symbol) likes(symbol, symbol) clauses likes(bill,Food):-food(Food),taste(Food,good). food(pizza). food(samosa). taste(pizza,good). taste(samosa,good). Output: Goal: likes (bill, What). What= pizza What= samosa Define predicates location (city, state).print all cities except Delhi using NOT predicate predicates find location(String,String) chkcity(String) clauses location("ahmedabad","gujrat"). location("delhi","up"). location("bhopal","mp"). location("jaipur","rajsthan"). chkcity("delhi"). find:writef("%10 %-5","City","State"),nl,fail. find:location(City,State), not (chkcity(City)), writef("%10 %-5",City,State),nl, fail.
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 2

Output:Goal: find City ahmedabad, bhopal, jaipur ,

State gujrat mp rajsthan

A Simple program using Backtracking. ANS:human(socrates). human(aristotle). human(plato). god(zeus). god(apollo). mortal(X) :- human(X). mortal_report :write('Report of all known mortals'), nl, nl, mortal(X), write(X), nl, fail. human_report :write(heading), human(X), write(X), fail. goal: mortal_report. Report of all known mortals socrates aristotle plato yes With given database of statements, write the goal statements that gives answers to following questions: Database : Hobbies 1. Mamta ,Ekta, Manan, Hetal and Rakhi are persons. 2. Mamta likes reading and music. 3. Ekta likes eating. 4. hetal likes reading. 5. Manan likes traveling and eating. 6. Rakhi likes music and reading. 7. Manna likes reading and music.
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 3

Question. 1. Display the persons that enjoy reading. 2. Display all the persons that enjoy reading as well as music 3. Display the persons that enjoy all the hobbies. 4. Display the person that enjoys the thing enjoy by person1. 5. Display the person that enjoys the thing enjoy by person1 as well asperson2. 6. Display the person that enjoys nothing. 7. Display the person that enjoys the thing not enjoy by person1. ANS:person(mamta). person(Ekta). person(manan). person(hetal). person(rakhi). likes(mamta, music). likes(mamta, reading). likes(ekta, eating). likes(hetal, reading). likes(manan, traveling). likes(manan, eating). likes(rakhi, music). likes(rakhi, reading). likes(manan, music). likes(manan, reading). 1) goal: likes(X, reading). output: X=mamta, hetal, rakhi, manan 2) goal: likes(X, reading) and likes(X, music). output: X=mamta, rakhi, manan 3) goal: likes(X, reading) and likes(X, music) and likes(X, eating) and likes(X, traveling). output: X=false 4) goal: likes(mamta, X) and likes(Y, X) output: X=reading, music Y=rakhi, manan 5) goal: likes(mamta, X) and likes(ekta, X) and (Y, X). output: X=false Y=false
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 4

Practical 4 Aim: - Write a program to find the maximum and minimum from the given numbers.
domains A,B,C = integer predicates max(integer,integer,integer) min(integer,integer,integer) go clauses go:write("Enter number 1:"), readint(A), write("Enter number 2:"), readint(B), write("Enter number 3:"), readint(C), max(A,B,C), min(A,B,C). max(A,B,C):A>B,A>C,write(A," is the largest"),nl. max(A,B,C):B>A,B>C,write(B," is the largest"),nl. max(A,B,C):C>B,C>A,write(C," is the largest"),nl. min(A,B,C):A<B,A<C,write(A," is the smallest"),nl. min(A,B,C):B<A,B<C,write(B," is the smallest"),nl. min(A,B,C):C<B,C<A,write(C," is the smallest"),nl. Output:Enter number 1: 12 Enter number 2: 23 Enter number 3: 9 23 is the largest 9 is the smallest Yes

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 5

Find whether the given year is leap year or not.


predicates go check(integer) clauses go:write("Enter the Year : "), readint(Year), check(Year). check(Y):Z=0, A=Y mod 1000, B=A mod 100, Z=B mod 4, write("Year ",Y," is a Leap Year."). check(Y):write("Year ",Y," is not a leap year."). goal clearwindow(), go. %-----------OUTPUT-------------% Enter the Year : 2004 % Year 2004 is a Leap Year. % Press the SPACE bar % Enter the Year : 2003 % Year 2004 is not a leap year. % Press the SPACE bar

Write a Program to Implement Login System


predicates logon try(String, String) clauses logon:write("Enter username : "), readln(Username), write("Enter password : "),
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 6

readln(Password), try(Username, Password), write("Successful Login"). try("artificial","intelligence"). try("kaushik","raviya"). %-----------OUTPUT-------------Goal: logon. Enter username: kaushik Enter password: raviya Successful Login

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 7

Practical 5:
Write a Program to Demonstrate the FAIL Predicate. predicates location(string,string) go clauses go:location(City,State), writef("%-10 %5 \n",City,State), fail. go. location("Junagadh","Gujarat"). location("Abu","Rajsthan"). location("Shahda","Maharashtra"). Output: Goal: go City Junagadh Abu Shahda

State Gujarat Rajsthan Maharashtra

Write a Program to Implement Login System using recursively


predicates logon getinput(String, String) user(String, String) clauses logon:getinput(Name, Pass), user(Name,Pass), write("logged in"). logon:write("Try again"), logon.
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 8

getinput(Name,Pass):write("Enter username : "), readln(Name), write("Enter password : "), readln(Pass). user("kaushik","raviya"). user("artificial","intelligence"). Output Goal: logon. Enter username: kaushik Enter password: raviya Successful Login

Write a Program to Implement Login System Using Repeat Predicate


predicates logon repeat user(String, String) getinput(String,String) clauses logon:getinput(_,_), write("You are logged on"). logon:repeat, write("Sorry, try again"), getinput(_,_), write("You logged on"). repeat. repeat:repeat. getinput(Name, Pass):write("Enter username : "), readln(Name), write("Enter password : "), readln(Pass), user(Name, Pass). user("gautam","chhatbar"). user("chintan","kotadia").
Prepared By Kaushik Raviya (Assistant Professor CSE/IT) Page 9

Output Goal: logon.

Write a program to demonstrate the CUT (!) Predicate


predicates location(string,string) chkstate(string) go clauses go:writef("%-10 %5 \n","City","State"), fail. go:location(City,State), chkstate(State), writef("%-10 %2 \n",City,State), fail. go. location("Jackson","MS"). location("Releigh","CS"). location("Washington","DC"). chkstate("DC"):!,fail. chkstate(_). Output Goal: go City Jackson Releigh Yes State MS CS

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 10

AIM: Write a program to input character and check if the character is lowercase alphabet and uppercase alphabet or digit.
predicates go check(char) clauses go:write("Enter a character : "), readchar(A), check(A), nl. check(A):A>='A', A<='Z', write("Upper case : ", A). check(A):A>='a', A<='z', write("lower case : ", A). check(A):A>='0', A<='9', write("Digit : ",A). Output: Goal: go. Enter a character: A Upper case Yes.

AIM: Write a program to find length of the string


predicates go clauses go:clearwindow, write("Enter the string:"),nl, readln(S), str_len(S,L), write("String Length:",L),nl.

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 11

Output: Enter the string: Kaushik String Length:7 Yes Goal:

Prepared By Kaushik Raviya (Assistant Professor CSE/IT)

Page 12

Das könnte Ihnen auch gefallen