Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Program Examples

Similar presentations


Presentation on theme: "Arithmetic Program Examples"— Presentation transcript:

1 Arithmetic Program Examples
Chapter 8 Arithmetic Program Examples

2 Factorial1.pro factorial(N, Result) :- N > 1,
predicates factorial(integer, integer) clauses factorial(1, 1). factorial(N, Result) :- N > 1, M = N-1, factorial(M, RET), Result = N*RET. factorial(N,M) :- M = N, write(“end of datatbase”). Chapter 8

3 Factorial2.pro factorial(N, Result) :- M = N-1, factorial(M, RET),
predicates factorial(integer, integer) clauses factorial(1, 1) :- !. factorial(N, Result) :- M = N-1, factorial(M, RET), Result = N*RET. Chapter 8

4 Factorial Ex07EX03.pro factorial(X, FactX)
/* Recursive program to compute factorials. */ predicates factorial(integer, real) clauses factorial(1, 1) :- !. factorial(X, FactX) :- Y = X-1, factorial(Y, FactY), FactX = X*FactY. Chapter 8

5 Factorial Ex07EX07.pro predicates factorial(integer, real)
factorial_aux(integer, real, integer, real) /* Numbers likely to exceed are declared as reals. */ clauses factorial(N, FactN) :- factorial_aux(N, FactN, 1, 1). factorial_aux(N, FactN, I, P) :- I <= N, !, NewP = P * I, NewI = I + 1, factorial_aux(N, FactN, NewI, NewP). factorial_aux(N, FactN, I, FactN) :- I > N. Chapter 8

6 Factorial Ex07EX08.pro /*Turbo Prolog 2.0 Chapter 7, Example Program 8*/ predicates factorial(integer,real) factorial(integer, real, integer, real) /* Numbers likely to exceed are declared as reals. */ clauses factorial(N,FactN):- factorial(N,FactN,1,1). factorial(N, FactN, N, FactN):- !. factorial(N, FactN, I, P):- NewI = I+1, NewP = P*NewI, factorial(N, FactN, NewI, NewP). Chapter 8

7 Status Ex3.6status.pro /* Ex3.6 from thai book */ predicates
status(symbol). sex(symbol,symbol). father(symbol,symbol). husband (symbol,symbol) clauses husband (suchart,malee). husband (somchai,monta). father(suchart,poo). father(somchai,tik). father(somchai,tum). father(somchai,ta). father(somchai,tu). sex(female,malee). sex(female,monta). sex(male,suchart). sex(male,somchai). sex(female,susy). sex(male,mike). Chapter 8

8 Status Ex3.6status.pro status(X) :- sex(S,X),write(X, “ is “,S," "),nl,fail. status(X) :- husband(X,W),!, write("married wife = ",W),nl, write(" Chiledren : "),nl, father(X,C),write(" ",C),nl, fail. husband (H,X),!, write("married husband = ",H),nl, father(H,C),write(" ",C),nl, write(X, " is single. \n "). Chapter 8

9 โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า A = (X/B +B)/2
Chapter 8

10 Ex5.2 Solution.pro /* Ex5.2 from thai book */
/* Find A = (X/B +B)/2 */ predicates solve(real,real,real). clauses solve(A,X,B) :- A = (X/B + B)/2. Chapter 8

11 โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า
AX^2 + BX + C = 0 X = ( -B +- sqrt(B*B -4AC))/2A Chapter 8

12 Ex5.3 Solution.pro /* Ex5.3 from thai book */
/* Find AX^2 =BX + C = 0 */ /* Find X = ( -B +-sqrt(B*B -4AC))/2A */ predicates solve(real,real,real). run(real,real,real). clauses run(A,B,C) :- D = B*B - (4*A*C), solve(A,B,D),nl. solve(_,_,D) :- D < 0, write("No solution"). solve(A,B,D) :- D = 0, X = -B / (2*A), write(" x = ",X),!. S = sqrt(D), X1 = (-B + S)/(2*A), X2 = (-B - S)/(2*A), write(" x1 = ",X1," x2 = ",X2). Chapter 8

13 โจทย์ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อคำนวณหาค่า
ระยะห่างจากเมือง 2 เมืองใดๆ route(town, town, distance) road(town1, town2, 200) Chapter 8

14 Ex18Ex02.pro : Distance /*Turbo Prolog 2.0 Chapter 18, Example Program 2 */ domains town = symbol distance = integer predicates road(town, town, distance) route(town, town, distance) clauses road(tampa, houston, 200). road(gordon, tampa, 300). road(houston, gordon, 100). road(houston, kansas_city, 120). road(gordon, kansas_city, 130). route(Town1, Town2, Distance) :- road(Town1, Town2, Distance). road(Town1, X, Dist1), route(X, Town2, Dist2), Distance=Dist1+Dist2, !. Chapter 8

15 EX18EX01.pro : Animal goal: run predicates clauses animal_is(symbol)
it_is(symbol) ask(symbol, symbol, symbol) positive(symbol, symbol) negative(symbol, symbol) clear_facts run clauses animal_is(cheetah) :- it_is(mammal), it_is(carnivore), positive(has, tawny_color), positive(has, dark_spots). animal_is(tiger) :- it_is(mammal), positive(has, black_stripes). goal: run Chapter 8

16 EX18EX01.pro : Animal (cont.)
animal_is(giraffe) :- it_is(ungulate), positive(has, long_neck), positive(has, long_legs), positive(has, dark_spots). animal_is(zebra) :- it_is(ungulate), positive(has,black_stripes). animal_is(ostrich) :- it_is(bird), negative(does, fly), positive(has, black_and_white_color). animal_is(penguin) :- it_is(bird), positive(does, swim), animal_is(albatross) :- it_is(bird), positive(does, fly_well). Chapter 8

17 EX18EX01.pro : Animal (cont.)
it_is(mammal) :- positive(has, hair). it_is(mammal) :- positive(does, give_milk). it_is(bird) :- positive(has, feathers). it_is(bird) :- positive(does, fly), positive(does,lay_eggs). it_is(carnivore) :- positive(does, eat_meat). it_is(carnivore) :-positive(has, pointed_teeth), positive(has, claws), positive(has, forward_eyes). it_is(ungulate) :- it_is(mammal), positive(has, hooves). it_is(ungulate) :- it_is(mammal), positive(does, chew_cud). positive(X, Y) :- ask(X, Y, yes). negative(X, Y) :- ask(X, Y, no). Chapter 8

18 EX18EX01.pro : Animal (cont.)
ask(X, Y, yes) :- !, write(“Question > “, X, " it ", Y, “?”,’ \n’), readln(Reply), frontchar(Reply, 'y', _). ask(X, Y, no) :- !, write(“Question > “,X, " it ", Y, “?”,’\n’), frontchar(Reply, 'n', _). clear_facts :- write("\n\nPlease press the space bar to exit\n"), readchar(_). run :- animal_is(X), !, write("\nAnswer.... => Your animal may be a (an) ",X), nl, nl, clear_facts. write("\n Answer.... => Unable to determine what"), write("your animal is.\n\n"), clear_facts. Chapter 8

19 Logic Programming Symbolic Expert System VB Propositional
Predicate Logic Prolog True False Fact Rule pred_name(att1,att2,var1) if....then... Chapter 8

20 Turbo Prolog + - * / mod Symbolic interface ! cut, fail and, or, not
factorial interface ! cut, fail Turbo Prolog write read and, or, not window recursive predicates clauses goal domains string variable matching symbol fact rule integer List pred_name(att1,att2,var1) member reverse append head/tail length Chapter 8

21 Expert System Symbolic Knowledge Engineer Expert Specific Domain
Artificial Intelligence Knowledge Representation Expert System Knowledge Engineer Expert Specific Domain User Interface Inference Engine Easy to use Tool Explanation Fact Rule Prolog VB Others if....then... Chapter 8

22 Knowledge Representation
Symbolic Expert System Knowledge Representation semantic network frame conceptual graph predicate Logic slot value isa inheritant pred_name(att1,att2) Chapter 8

23 DEMO EXPERT SYSTEMS Chapter 8


Download ppt "Arithmetic Program Examples"

Similar presentations


Ads by Google