Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.ssau.ru/files/resources/sotrudniki/soldatova_lezina.pdf
Äàòà èçìåíåíèÿ: Wed Jun 16 00:00:00 2010
Äàòà èíäåêñèðîâàíèÿ: Tue Oct 2 15:50:37 2012
Êîäèðîâêà: ISO8859-5

Ïîèñêîâûå ñëîâà: m 5
Visual Prolog

: .. , ..

2010


004.89(075.8)

: - ? Ë .., ..., .. - ? Ë - , ..., ..

.. , .. Visual Prolog: : , 2010 -81 ., . ISBN - 978-593424-486-7

-

010400 ? Ë 230102 ? Ë, ? Ë, ? Ë, ? Ë ? Ë.



ISBN - 978-593424-486-7

? .. , .. ,2010

2


............................................................................................................. 5 1 ......................... 5 1.1 ......................................................................................... 5 1.2 . ............................... 7 1.2.1 ......................................................................................... 7 1.2.2 . ......................................................... 8 1.2.3 . .................................. 11 1.2.4 . .......................................... 13 1.2.5 ........................................ 15 2 . ........................ 16 2.1 ....................................................................................... 16 2.2 ............................................... 17 2.3 . ............................................... 21 2.4 . .............................................................................. 21 2.5 . . .................................................... 22 2.6 . .................................................................. 24 2.7 . ............................................................................ 25 2.8 . ................................................................... 26 2.9 ............................................................. 28 2.10 ............................................................................ 30 2.11 ........................................................ 32 2.11.1 ....................................................................... 32 2.11.2 ................................................................... 33 2.11.3 ................................................................... 34 2.11.4 ............... 35 2.11.5 ............................................................................... 35 2.11.6 ............................................................... 37 2.12 .......................................................... 38 2.12.1 ................................................................................. 38 2.12.2 ............................................................. 39 2.12.3 , ........................... 41 2.13 ................................................................ 42 2.14 ....................................................... 45 2.15 . ............................................................. 49 2.16 ......................................................... 51 2.17 ......................................................... 52 2.18 ................................................... 55 2.19 . ................................................................................ 57 2.20 " " ............................................................. 59 3 . ............................................................................................................... 62 3.1 .............................................................. 62 3.2 ......... 64 3


3.2.1 ....................................................................................... 3.2.2 ........................................................................................ 3.3 / . ..................................... 3.4 /- ................................ 3.5 ................................................. .............................................................................................................

64 67 71 74 76 80

4


. , , Visual Prolog- .. .. ? Visual PrologË - 2003 . , , , . , , . , Visual Prolog . Visual Prolog, . 1 1.1 , , . , , , . , . ( ) , , [1]. 1984 , () . , 5


. ( ) , , . : 1. : ( ); 2. (): . , . , , . 3. ( ): . , , -- P = P. : = P --P. - . . [1] , , , . . XIX . XIX .. ., , , . , . , , . , , . (), M = . T , 6


, . T . , , x T . P(T). S . T . , , . P(S), , X . A. A . , P(A), A. B . A, , B. . P(B), , , . , . , - . , . , . - . 1.2 . 1.2.1 XVII . XX , 1936 , , , . 7


, , , , ( , , ). 1930 . , . , . , , . . 1959 . , , . 1960 . , . 1965 , , , . . , -, , . 1.2.2 . , , . , , . : 1. , , . 2. , . 3. , .

8


. . . 1. , . 2. , , . 3. , . (K1x1)...(Knxn) (M), (Kixi), i = 1,...,n, (xi) (xi), M () . . 1. (K1x1)...(Knxn) (M), M . Kr (K1x1)...(Knxn), 1<=r<=n. 2. Kr - c, , M, xr M c Krxr . K1,...Ki - , M Kr, 1< i

. , , . 2: S S , S . , . 2. (x) (P(x) (y)(? (z)Q(x, y) (u)R(u, x, y))) . 1. : (x) (P(x) (y)(? ?(z)Q(x, y) (u)R(u, x, y))). 2. : (x) (P(x) (y)(? ?Q(x, y) (u)R(u, x, y))). 3. : (x) (P(x) (y)(Q(x, y) (u)R(u, x, y))). 4. : (x)(y)(u) (P(x) (Q(x, y) R(u, x, y))), . (x)(y)(u)(P(x) (Q(x, y) R(u, x, y))), . : (u) (x),(y) u f(x, y). , : (x)(y)(P(x) (Q(x, y) R(f(x, y), x, y))). . : {P(x), (Q(x, y) R(f(x, y), x, y))}. . 3. (x)(y)(z)(( ? P(x, y) Q(x, z)) R(x, y, z) (? Q(x, z) P(x, y))). : ((? P(x, y) R(x, y, z )) (Q(x, z) R(x, y, z)) (? Q(x, z) P(x, y))). : (y)(z) (x), y, z f(x), g(x). , : (x)(((? P(x, f(x)) R(x, f(x), g(x))) (Q(x, g(x)) R(x, f(x), g(x)) (?Q(x, g(x)) P(x, f(x)))). : 10


{?P(x, f(x)) R(x, f(x), g(x)), Q(x, g(x)) R(x, f(x), g(x)), ?Q(x, g(x)) P(x, f(x))}. 1. S - , F. F , S . 2. F1, F2,..., Fn G. G F1, F2,..., Fn , ((F1 F2 ... Fn)G) . 3. F1, F2,..., Fn G. G F1, F2,..., Fn , (F1 F2 ... Fn ?G) . . , , , : ?((F1 F2 ... Fn)G)= ?(?(F1 F2 ... Fn) G)= (F1 F2 ... Fn) ?G). 1 3 , G F , S ?G, S1 S2 ...Sn ?G. , S ?G , , G S. 1.2.3 . , , . , (). , : . , , . , . , , . 3: A , A ?A , { A, ?A } . , , . 4: : 11


C1 C2, L1 C1, L2 C2, L1 L2 C1 C2 , () C1 C2. 4: : C1: P R, C2: ?P Q. C1 P, ?P C2. , P ?P C1 C2 , R Q R Q. , C1 C2 C1 C2. . 4. C1 C2. C C1 C2 C1 C2. , , , . , . 5: S - . C S 1, C2,..., Ck , Ci S , Ci, Ck=C. S ( ) S. 5. S: 1. ?P Q, 2. ? Q, 3. P. 1 2 4. ?P. 4 3 5. . S , 4 S, , S . . 6: , . 6: 1 2 ...n ?N1 ?N2... ?Nm 28: , . 7: . 12


?N1 ?N2... ?Nm ?N1 ?N2... ?Nm =? (N1 N2 ...Nm) P (N1 N2 ...Nm) P N1, N2,...Nm , . , , . , , , . : , , ( ). 1.2.4 . , . . , , . 8. : C1: P(y) Q(y), C2: ?P(f(x)) R(x). C1, - C2. , f(a) y C1 a x C2, : C1': P(f(a)) Q(f(a)), C2': ?P(f(a)) R(a). P(f(a)) ?P(f(a)), C3': Q(f(a)) R(a). , f(x) y C1, C1'': P(f(x)) Q(f(x)). P(f(x)) C1'' ?P(f(x)) C2. , C3: Q(f(x)) R(x). , , . , C3 8 , , . 7: - {t1/v1,...,tn/vn}, vi - , ti - , vi, vi . 13


8: {E1,..., Ek} , E1=E2=... Ek. {E1,..., Ek} , . . : 1. , , . 2. , , , . 3. , . 4. , , , . 5. , . 9. : 1. Q(a, b, c) Q(a, d, l). . 2. Q(a, b, c) Q(x, y, z). . - Q(a, b, c). 9: {E1,..., Ek} , , = À , . 10: À , ( À ) [t]=[ [t]], t - , , [t] - , t . 11: {E1,..., Ek} () , E , , , . E. 10. : {P(x, f(y, z)), P(x, a), P(x, g(h(k(x))))}. , {f(x, y), a, g(h(k(x)))}. . 14


E - , D - , k - , k k- . 1. k=0, k=e ( ), Ek=E. 2. Ek Dk, : k - E. Dk. 3. vk tk Dk, vk , tk, 4. : E . 4. k+1=k { tk / vk}, Ek tk vk. 5. K=k+1. 2. 11. : E={P(f(a), g(x)), P(y, y)}. 1. E0=E, k=0, 0=e. 2. D0={f(a),y}, v0=y, t0=f(a). 1=={f(a)/y}, E1={P(f(a), g(x)), P(f(a), f(a))}. 3. 4. D1={g(x),f(a)}. 5. D1. , , E - . 1.2.5 . : x, x, x, . , . 12: ( ) C , C - C. 12. : C= P(x) P(f(y)) ?Q(x). 1 2 = {f(y)/x}. , C=P(f(y)) ?Q(f(y)) C. 13: C1 C2 - , . L1 L2 - C1 C2 . L1 ?L2 , (C1 - L1) (C2 - L2) C1 C2. 13. C1= P(x) Q(x) C2= ?P(a) R(x). x C1 C2, C2 C2= ?P(a) R(y). L1= P(x) L2=?P(a). L1 L2 ={a/x}. , Q(a) R(y) - C1 C2. 15


14. , R(a, z) S={?P(x, f(x)) R(x, f(x), g(x)), Q(x, g(x)) R(x, f(x), g(x)), ?Q(x, g(x)) P(x, f(x))}. C1=?P(x, f(x)) R(x, f(x), C2= Q(x, g(x)) R(x, f(x), C3=?Q(x, g(x)) P(x, f(x)). S C4=?R(a, z). C1 , C2 , C3 x, C2 y, C3 u. , : C1=?P(x, f(x)) R(x, f(x); C2= Q(y, g(y)) R(y, f(y); C3=?Q(u, g(u)) P(u, f(u)); C4=?R(a, z). C5 C1 C3 , x u, , u x, C5= R(x, f(x)) ?Q(x, g(x)). C6 C2 C5 , y x, , y x, C6= R(x, f(x)) R(x, f(x))= R(x, f(x)). C7 C2 C6 , x a, z f(a), C7= , . . 1. S , , 2. 2. S C1 C2, L1 C1 L2 C2. , , 3. 3. C1 C2 S. 1. 2 . 2.1 : . . , . . 16


( clauses) ( goal), , , , . ( ). : - . , S?P?Q?R S: - P, Q, R. : 1. NOD (x, x, x): -. NOD (x, y, z): - B (x, y), NOD (f (x, y), y, z). 2. 3. NOD (x, y, z): -B (y, x), NOD (x, f (y, x), z). NOD - z x y, B - ?Ë, f - . B f , : 1. NOD (x, x, x): -. 2. NOD (x, y, z): - x>y, NOD ((x- y), y, z). 3. NOD (x, y, z): -y>x, NOD ((x, y- x), z). , 4 6, : 4. : - NOD (4, 6, z). - , . 2.2 (PROgramming LOGic) . - , . . () , , [2]. , . , . 17


. , , , . , , . , . , , . - : Ç , Ç , Ç ( ), -, . . . . . , . , , . , (), . . , , . . : , - . , : Ç (, ), ; Ç (, ) - . ?_Ë , , 18


. , . . . , . - . . , , , , : ( , ). ( , ). , , , . . - , . () , : : (, ). , . , , : : ( X, ). : : Q1, Q2,...,Qn, , Q1, Q2,...,Qn - . , , , ( ). 15. : ( , ). (, ). ( , ). ( , ). ( , ). , , : : ( , X), (X, ). 19


, . , . , . , , () , . ( , ). ( , ). ( , ). , , X . . . , - , , , . - H: - P1, P2,..., Pn. ?: -Ë ?Ë, H , P1, P2,..., Pn . H ?P1? P2,...,?Pn. , . , - . 16. 15 , ?Ë: ( , ). (, ). ( , ). ( , ). ( , ). (X, Y): - (X, Z), (Z, Y). (X, Y): - (X, Z), (Z, Y). , , : : ( , ). - , , - . ?Ë. . , ?Ë : (x, y): - (x, y). (x, y): - (x, y). (x, y): - (x, z), (z, y). (x, y): - (x, z), (z, y). 20


, . , , , : ? Ë. - . 2.3 . , , , , . , , - , , . , . ? ; Ë. ?notË - P not(P). not(P) , () P. . , P, not(P), , . , P , . 2.4 . : , , , . . , ( ), . , , , , . . : . . : ? t YË. , , 21


. , , . . , . . 1. x y-, , . 2. x- , Y-, , Y x . 3. x y -, , () . ?=Ë, . ?=Ë . ?=Ë , . 17: X=Y, X Y - , , : X=5 Y=5, (); X=6 Y=5, (). X Y - , , ?=Ë . ?=Ë , . 2.5 . . () , , [1]: Q=Q1, Q2,..., Qn. . : , . 22


, . , . , , . , , , , . , . . , , . . : , . - . Q , ?Q. D - ( ). Q, , [D(?Q)] . : . 18: : 1. ( , ). 2. (, ). 3. ( , ). 4. ( , ). 5. ( , ). 6. (X, Y): - (X, Z), (Z, Y). 7. (X, Y): - (X, Z), (Z, Y). 8. (X, Y): - (X, Z), (Z, Y). 9. (X, Y): - (X, Z), (Z, Y). : Q1, Q2 = (X, Y), (, Y). Q1= (X,Y) : (,). 1={X=,Y=}. 1[Q2]= (, ). 1 , , : X= , Y= . 1[Q2]. , 23


, Q1, Q2 Q1 , . . 2={X=,Y=}. 1[Q2]= (, ). . : : (X, Y), (, Y). 2 : X=, Y= X=, Y=. , TestGoal Visual Prolog. : 1. -. 2. , -. 3. , , . 4. , . Visual Prolog ( project- ), , PDC Prolog, . 2.6 . , , . , . , : fail - ! - . fail ! , fail . , fail . , . , , , . 24


: 1. , - ? Ë. 2. - ? Ë. 19: ! fail. 16 ?-Ë, : 1._1(X,Y):-(X,Y), write(X," ",Y),nl. goal _1(X,Y). 3 : X=, Y= . X=, Y= . X=, Y= . 2._2:-(X,Y), write(X," ",Y),nl,fail. goal _2. 3 : X=, Y= . X=, Y= . X=, Y= . 3._3(X,Y):-(X,Y), write(X," ",Y),nl,!. goal _3(X,Y). 1 : X=, Y= . 2.7 . - . , . , ?Ë , . , : 1. . 2. . , , . 25


, case . 3. , . 4. go to. 5. . . 2.8 . , , : , , , . domains, constants, facts (database), predicates, clauses goal . : Ç domains , , , , ; Ç constants , , , ; Ç facts (database) , , ; Ç predicates , , , ; Ç clauses , , ; Ç goal . . , . Visual Prolog domains, facts, predicates, clauses , global. : 26




char integer byte word dword

-32768 32767 0 255 0 65535 0 +1E-307 +1E308 16 16 32 32 16 32

`a', 'b', `#', `B', `%' -63, 84, 2349



real short ushort long ulong unsigned

360, - 8324, 1.25E23, 5.15E-9



string symbol

?todayË, ?123Ë, ( 250) ?school_dayË 1. , , ; - . 2. , . DOS flower, school_day

?string and symbolË



ref file mail.txt, LAB.PRO

, domains. 20: domains number=integer name, person=symbol. symbol string - , . 27


Visual Prolog string symbol. , , string, - symbol: Symbol - , , , . String - , #0, . Visual Prolog , , . predicates. , . , ?goË ?repeatË. , predicates: 21: predicates mother (symbol, symbol) father (symbol, symbol). clauses, goal - . Visual Prolog goal . Test Goal. , , - . 2.9 , . , , . domains. , . , . . , . , : , . ( ). , 28


, , , . 22: , . personal_library, : personal_library= book (title, author, publisher, year), collection (collector, personal_library). book . , , 1990 , : domains collector, title, author, publisher = symbol year = integer personal_library = book (title, author, publisher, year) predicates collection (collector, personal_library) clauses collection (irina, book (?Using Turbo PrologË, ?Yin with SolomonË, ?Moscow, WorldË, 1993)). collection (petr, book (?The art of PrologË, ?Sterling with ShapiroË, ËMoscow, WorldË, 1990)). collection (anna, book (?Prolog: a relation language and its applicationsË, ?John MalpasË, ËMoscow, ScienceË, 1990)). goal collection (X, book( Y,_, _, 1990) Y . : collection (X, Z),Z=book( Y,_, _, 1990), Z book. . . . 23: , . domains person, title, author, artist, album, type = symbol thing = book (title, author); record (artist, album, type) predicates owns (person, thing) clauses owns (irina, book (?Using Turbo PrologË, ?Yin with SolomonË)). owns (petr, book (?The art of PrologË, ?Sterling with ShapiroË)). 29


owns (anna, book (?Prolog: a relation language and its applicationsË, ?John MalpasË)). owns (irina, record (?Elton JohnË, ?Ice FairË, ?popularË)). owns (petr, record (?Benny GoodmanË, ?The King of SwingË, ËjazzË)). owns (anna record (?MadonnaË, ?MadonnaË, ?popularËË)). goal owns (X, record(_, _, ?jazzË) Visual Prolog . , record (?Elton JohnË, ?Ice FairË, ?popularË) , : artist=art(family,name), family,name=symbol, : record (art("Elton","John"),?Ice FairË, ?popularË). 2.10 , . - . , , , , . : , , [4]. ([]), . , . . . , , . , : - ; - . , . - , , , ? Ë. . , , , , . ! (|): [Head | Tail]. 30


, - ! Head , Tail ( ). , , , X S [X|S]. , , . , , [a,b,c] :


a



b



c

[]

(*) . 24: , . domains list1=integer* list2=char* list3=string* list4=real* list5=symbol* personal_library = book (title, author, publisher, year) list6= personal_library* list7=list1* list8=list5* , personal_library, . 31


25: . [1, 2, 3, 4, 5] [6.9, 4.3] [cat, dog, horse] [`S', `K', `Y'] [?PIGË] [] 1 6.9 Cat `S' ?PIGË [2, 3, 4, 5] [4.3] [dog, horse] [`K', `Y'] []

Visual Prolog . - , . , , . 26: , , : domains llist=i(integer);c(char);s(string) list=llistl* 2.11 domains, , predicates, clauses goal. : , , , , , . 2.11.1 . . , , . . 27: . domains list=integer* predicates 32


member (integer, list) clauses member (Head, [Head |_ ]). member (Head, [_ | Tail ]):- member (Head, Tail). goal member (3, [1, 4, 3, 2]). , member. . , , . , , , . , , , , . , , . , , , , . find . , : member (Head, [Head |_ ]):- !. , . 2.11.2 , , . L1, - L2. L1 = [1, 2, 3], L2 = [4, 5]. append L2 L1 L3, L1 L2. : 1. L3 . 2. L1 L3, L3 [1, 2, 3]. 3. L2 L3, [1, 2, 3, 4, 5]. : 28: . 33


domains list=integer* predicates ppend (list, list, list) clauses append ( [], L2, L2). append ([H|T1], L2, [H|T3 ]):- append (T1, L2, T3). goal append ( [1, 2, 3], [4, 5], L3). append , append ([1, 2, 3], [4, 5], L3). : append (L1, [3, 4, 5], [1, 2, 3, 4, 5]) - L1=[1, 2], L2 = [3, 4, 5] L3 = [1, 2, 3, 4, 5]. append (L1, L2, [1, 2, 3, 4, 5]) L1 L2, L3 = [1, 2, 3, 4, 5]. 2.11.3 count_list1 count_list2. count_list1 , , byte , . count_list2 , , byte . 29. 29: . domains list=integer* predicates count_list1(list,byte,byte) count_list2(list,byte) clauses count_list1([],N,N). count_list1([_|T],N,M):- N1=N+1, count_list1(T,N1,M). count_list2([],0). count_list1([_|T],N):- count_list1(T,N1), N=N1+1. goal count_list1([0,-1,2,6,-9],0,N), count_list2([4,3,-8],K).

34


2.11.4 max_list min_list. max_list , , integer , - . min_list , , integer . 26. 30: . domains list=integer* predicates max_list(list, integer, integer) min_list(list, integer) clauses max_list ([],M,M). max_list ([H|T],N,M):- H>N, max_list(T,H,M). max_list ([H|T],N,M):- H<=N, max_list(T,N,M). min_list ([H|[]],H). min_list ([H|T],M):- min_list(T,M1), H>M1, M=H. min_list ([H|T],M):- min_list(T,M1), H<=M1, M=M1. goal L=[1,5,3,-6,8,-4],L=[H|T],max_list(T,H,Max), min_list([4,3,-8,6],Min). 2.11.5 . . : Ç , Ç , Ç . . , . , . . , , . 35


31: (). domains list=integer* predicates puz(list,list) perest(list,list) clauses puz(L1,L2):-perest(L1,L3),!,puz(L3,L2). puz(L1,L1). perest([X,Y|T],[Y,X|T]):-X>Y. perest([Z|T],[Z|T1]):-perest(T,T1). goal puz([1,3,4,5,2],L).] 32: . domains list=integer* predicates insert_sort (list, list) insert (integer, list, list) asc_order (integer, integer) clauses insert_sort ( [], []). insert_sort ([H1|T1], L2):- insert_sort (T1, T2), insert(H1, T2, L2). insert (X, [H1| T1], [H1| T2]) :- asc_order (X, H1), !, insert (X, T1, T2). insert (X, L1, [X| L1]). asc_order (X, Y):- X>Y. goal insert_sort ([4, 7, 3, 9], L). . , , insert_sort, H1 , . . , , insert, . H1 9, insert (9, [], [9]). , insert_sort. 3 asc_order, asc_order (3, 9):- 3>9. 36


, , , 3 9: insert (3, [9], [3, 9]). insert_sort, : insert_sort ([3, 9], [3, 9]). 7 asc_order asc_order (7, 3):- 7>3. , 3 insert , - [9]: insert (7, [9], _). asc_order (7, 9):- 7>9 , , insert, insert_sort. 7 3 9: insert (7, [3, 9], [3, 7, 9]). 4 asc_order asc_order (4, 3):4>3. , 3 insert , - [7, 9]: insert (4, [7, 9], _). asc_order (4, 7):- 4>7 , , insert, insert_sort. 4 3 7: insert (4, [3, 7, 9], [3, 4, 7, 9]). insert_sort [4, 7, 3, 9], [3, 4, 7, 9]). 2.11.6 . findall, . findall : Findall (Var_, Predicate_, List_), Var_ Predicate_, List_. 33: findall. domains d=integer predicates decimal (d) write_decimal clauses decimal (0) 37


decimal (1) decimal (2) decimal (3) decimal (4) decimal (5) decimal (6) decimal (7) decimal (8) decimal (9) write_decimal:- findall(C, decimal (C), L), write (L). goal write_decimal. 2.12 . , [3]. : Ç ; Ç ; Ç , ; Ç . . fail cut (!) , . , , , , . 2.12.1 , . , , . 34: . domains d=integer predicates decimal (d) write_decimal (d) clauses 38


decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). write_decimal (C):- decimal (C), write (C), nl. goal write_decimal (C). 2.12.2 . fail, , . 35: . domains d=integer predicates decimal (d) write_decimal clauses decimal (0) decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). write_decimal:- decimal (C), write (C), nl, fail. goal write_decimal. 10 , decimal (C). C , 0. , 39


decimal (C), , 0 . fail , , . 36: . domains d =integer predicates decimal (d) s (d, d) cikl clauses decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). decimal (8). decimal (9). s( X, Z):- Z=X*X. cikl:-decimal (I), s(I , S), write (S), nl, fail. goal not(cikl). 37: 5 . domains d=integer predicates decimal (d) write_decimal. make_cut (d) clauses decimal (0). decimal (1). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (7). 40


decimal (8). decimal (9). write_decimal:- decimal (C), write (C), nl, make_cut (C). make_cut (C):-C=5. goal write_decimal. ! , . make_cut fail, , 5. 38: , 5. domains d=integer predicates decimal (d) write_decimal clauses decimal (0). decimal (5). decimal (2). decimal (3). decimal (4). decimal (5). decimal (6). decimal (5). decimal (8). decimal (9). write_decimal:- decimal (C), =5, write (C), nl, !. goal write_decimal. !, 5, . 5. 2.12.3 , , : repeat. repeat:- repeat. repeat , repeat . , , repeat. repeat - , ( repeat). repeat repeat, , repeat repeat. 41


repeat . . , repeat , . repeat , . 39: . 0. domains d=integer predicates repeat write_decimal do_echo check (d) clauses repeat. repeat:- repeat. write_decimal:-nl, write( ?, , Ë), nl, write(? , 0Ë), nl. do_echo:- repeat, readln (D), write(D), nl, check (D), !. check (0):- nl, write (?-OK!Ë). check(_):- fail. goal write_decimal, do_echo. do_echo - , , repeat readln, write, check. check . , 0, ?OK!Ë , check . 0, check fail . repeat. Repeat , repeat . 2.13 - , . , , . 42


, ( 48), . factorial , . , , . , -. 40: . predicates factorial (byte, word) clauses factorial (0, 1). factorial (1, 1):-!. factorial (N, R):- N1=N-1, factorial (N1, R1), R=N*R1. goal f (7, Result). . , . . 41: , . predicates f (byte, word) clauses f (1, 1). f (2, 1). f(N, F):- N1=N-1, f (N1, F1), N2=N1-1, f (N2,F2), F=F1+F2. goal f (10, Fib). : Ç ; Ç ; Ç , (, , ). - . , , , , , , . 43


, . , , , , . , . . Visual Prolog . , : Ç ; Ç . . 42: . count(100). count(N):-write(N),nl,N1=N+1,count(N1). goal nl, count(0). , . 43: . count1(100). count1(N):-write(N),N1=N+1,count1(N1),nl. goal nl, count1(0). - nl . 44: . count2(100). count2(N):-write(N),nl,N1=N+1,count2(N1). count2(N):-N<0, write("N - "). goal nl, count2(0). ( ), , . , . 45: . count3(100). count3(N):-write(N),nl,N1=N+1,check(N1),count3(N1). 44


check(Z):-Z>=0. check(Z):-Z<0. goal nl, count3(0). ( check). 44 45 , 42, . , ? , , . 44 45 , . 46: 41 . count4(100). count4(N):-N>0,!,write(N),N1=N+1,count4(N1). count4(N):- write("N - "). goal nl, count4(0). 47: 42 . count5(100). count5(N):-write(N),N1=N+1 check(N1),!, count5(N1). check(Z):-Z>=0. check(Z):-Z<0. goal nl, count5(0). 2.14 . . () . facts Visual Prolog () . , , .