Você está na página 1de 119

. .

TURBO PROLOG

2005


....................................................................................................................................2
1. -.................................................................................4
1.1 ...............................................................................................................................4
1.2 - ............................................................................................6
1.2.1 - .......................................................................8
1.2.2 , -.....................................9
1.2.3 " -!".........................................................9
1.2.4 .............................................................................10
1.2.5 .................................................................................11
1.2.6 ...................................................11
1.2.7 ...............................................................................................................12
1.4 .........................................................................................................................12
2. - ....................................................................12
2.1 ...............................................................................................................................12
2.2 ...........................................................................13
2.2.1 .....................................................................................................14
2.2.2 ......................................................................................16
2.3 ...................................................................................................18
2.3.1 .........................................................................................................19
2.3.2 ...........................................................................................................19
2.3.3 ..................................................................................21
2.3.4 ..........................................................................................21
2.3.5 ...........................................................................................................22
2.4 -............................................23
2.4.1 ...........................................24
2.4.2 .................................................25
2.4.3 ............................................................................................................................28
2.5 .........................................................................................................................34
3. - .........................................................35
3.1 .............................................................................................................................35
3.2 -...............................................................................35
3.2.1 ..............................................................................36
3.2.2 - ..........................................................40
3.3 ................................................................................................40
3.3.1 .........................................................42
3.3.2 ...........................................................44
3.3.3 ............................................................................48
3.3.4 ................................................................................................51
3.3.5 ...................................................................................................................54
3.3.6 .........................................................................56
3.3.7 ................................................................61
3.4 -..........................................................................................66
3.5 .........................................................................................................................68
4. ................................................................................................69
4.1 .............................................................................................................................69
4.2 ..............................................................69
4.3 ...........................................................................................................70
4.4 ...........................................................................................................72
4.4.1 .....................................................................................72
4.4.2 () ................................................................................78
4.4.3 (), ..............................................82

4.5 ........................................................................................86
4.5.1 ........................................................................................................86
4.5.2 () ........................................................88
4.6 ..................................................................................................95
5. ................................................................................................96
5.1 .............................................................................................................................96
5.2 -....................................................................................................96
5.2.1 ........................................................................................................97
5.2.2 .......................................................................98
5.3 ...................................................................................99
5.4 ..............................102
5.5. ...............................................................................104
5.5.1 ..........................................................................................105
5.5.2 .......................................................................................................107
5.5.3 .............................................................................................109
5.5.4 .................................................................................................111
5.6 .........................................................................................115
5.7. ......................................................................................................................118

1. -
1.1
- (Turbo Prolog) (). Borland International , -
.
- ?
- - .
.
- , , - . . - ,
-,
.
-
, , .
,
.
, ,
,
.
. - .
-
: Arity Prolog, Prolog II, Wisdom Prolog Micro Prolog.
- , . IBM PC,
(CGA) (EGA).
-.
, .
- , ,
, .
4

. . -
( IBM PC).
. ,
-
. , - -
.
?
"
" (Prolog PROgramming in LOGic). 1973 .
" " () . . , ().
C&M Prolog "
" .
, ,
.
- .
C&M, , . (
) - . . , - " ", ,
. . , - "" ,
,
.
-
, ,
. , , , , .
.
, , .
,
DO WHILE IF ... THEN.
5

. (
. 2)
.
- .
, . , ,
, , ,
.
?
, -,
,
. (, , ), (,
). ,
-. .
- ,
. " -" , , . -
.
- ,
, . , , .
- - . . -
,
. , -
,
- . , - ,
, ,
, , .
- IBM PC .
1.2 -
-
, 200 .
6

PROGRAM DISK, - LIBRARY&SAMPLE


PROGRAMS. .1 ,
.
1.1. 1.1
___________________________________________________________
1. PROGRAM DISK:
PROLOG.EXE -.
PROLOG.OVL ,
, .EXE- .
PROLOG.SYS , ,
,
.
PROLOG.ERR
PROLOG.HLP (
F1).
GEOBASE.PRO

GeoBASE.
GEOBASE.INC
GEOBASE.DBA GeoBase.
GEOBASE.HLP GeoBase.
README.COM
README.
README
,
, -.
2. LIBRARY&SAMPLE PROGRAMS:
PROLOG.LIB , -

.
INIT.OBJ
EXAMPLES
, , .
ANSWERS
,
.
PROGRAMS
,
.
________________________________________________________

1.2.1 -

- 7 () .
;
, , . ,
.
7 -, :
1. (Run).
2. (Compile).
3. (Edit).
4. (Options).
5. (Files).
6. (Setup).
7. (Quit).
.
. , . ,
Edit E. ( , , . . Shift .)
Esc. ; Enter.
. - ( Editor ),
- (Dialog), (Message),
- (Trace). ,
, - - .
. Line 1 Col 1 ,
. . Indent , , Insert - ,
. WORK.PRO
; .PRO
, -. -
, WORK.PRO.

1.2.2 , -

, ,
. WELCOME.PRO. , , , , ,
.
Edit Enter ( E).
Editor - . -
.
WELCOME.PRO:
predicates
hello
goal
hello.
clauses
hello :write("Welcome to Turbo Prolog!"), nl.
,
Enter .
BackSpace. , ,
.
1.2.3 " -!"

-.
,
, Esc.
, Edit. Run
Message Dialog.
,
WELCOME.PRO. Run, . . Compile.
-
, . Run .
-, . , ,
9

-. , , , -
, - . hello.
*
1.1. WELCOME ,
.
-,
, . - , , .
Y. Quit , Esc.
1.2.4

, , (
), Esc, Files Save ( S,
Enter).
,
(, , WORK.PRO), ,
. , . WELCOME.PRO,
Enter.
( - ), .BAK, . , . .

. , "" . . ""
,
.

10

1.2.5

- ,
Files Directory
. , .PRO. , , Enter.
(File mask). *.PRO.
, . , Enter, , .
1.2.6

- , , . -
:
, . -
WordStar. WordStar, , , , -, -.
,
Files Load
Files. Enter,

.PRO. , , Enter. ,
, ,
.PRO. -, ; F1.
Help. , Help.
.
Shift-F10, ; Shift-F10 (
).
.
, - , 11

. , Files, Zap file in editor. . Y, .


, . , , Enter. , . Enter , Files. Edit
Enter, .
*
1.2. -.
WELCOME
nl,write(Have a nice day.)

write("Welcome to Turbo Prolog!")
.
1.2.7

Print Files
, . ,
Files, -
Print. Print, .
1.4
, , -,
.

-. ,
.
, , ,
, , ,
.
2. -
2.1
- . , ,
- -

12

. , ,
- , .

. , - . ""
.
2.2
-,
. " " , .. .
,
, ,
,
, .
, () .
,
, .
, . , ,
.
. ,
, , , ,
.
,
. , , ,
.
. 60- . , ,
, .
, , .
( ) . ,
, .. "" , . , 13

, ,
. , . .
, , - .
, , , , .
,
. , , .
, , , . , , , ,
. , , - - , .
, ,
-, .
, ,
- .
2.2.1

, - - , , . , -
, .
. " ". ( ) .
,

, , , , . ,
:
,
, ,
. , -

14

,
.
, :

, ( )
, .. ,
. ,
:
,
, , , .
,
. " " "
, ( )" . . "" - , "" - .
. , .
, ,
:

, , , . :

, ,
.
:
, ( ) ,
"" "":
,
, "".
, "".
"" . , , :

, , "" , . "" -

15

, , "" .
, "",
. -
,
, , "".
, , ,


"" - . , ""
, "" ,
, .
"" "",
:

(), ()
: .
.
, .
2.2.2

" ",
. , , .
, , . , " " " "
,
, . ,
, . -, , .
:

"" "" "" , . ""


:

16


(1,2)

(, )
-.
, ,
. , . ( ,
;
). , -,
:
(, ).
, . :
" ".
, " ,
" "",
. ,
"" ,
. , ,
- "" . - .
, "" -, .
"", ,
.
-
:
(1,2) (1,2)
(,
X)

(,
X)
. ,
. , , -
:
(, ).
(, X) (, X).
" " " , ". , , X ( ""), ,
. "" "", X "".
, - ,
"" X . , ,
, ,
17

, , . (
) ,
.
-
, " X ,
". -
, ,
. , - , -,
. , ,
.
, -, , , , - , -, , - .

-, , , .
- . , ,
, , .
"
".
2.3
, - , . , .
, , . -
. ,
"(, )", - True ()
False (); , -
, , ,
No solutions ( ).
. .
, , .

18

2.3.1

- , .
"" . - , ,
, .
, .
, ,
.
, - , , .
.
. , ( ) "". "" . ,
, "",
. :
(,).
, ,
, .
2.3.2

-
likes(mary, apples).
/* */

likes(beth, X) if likes(mary,X). /* , */
,
.
likes(beth,apples).
/* */
- ,
, .
. - likes.

likes(mary,apples).
, , likes, . likes , - beth mary.
- ,
. :
likes(beth, apples).
likes(mary, apples).
beth mary . , . - 19


, . -

likes(beth,X) if likes(mary,X).
likes(beth,X) .
, . -
, , if ( ).
- likes(beth, apples)
likes(beth, X) :
likes(beth, apples).
likes(beth, X).

. , apples X .
, , , X
"". , X,
.
"" .
, - , , ,
. X
, apples , - apples
X. X apples - .
, ,

.
X apples, likes(beth, X)
"" likes(beth, apples). - , apples X.
- if likes(mary,
X). X aplles, - " " , " ". , , , . "", , , , .

likes(mary, aplles).
- , likes(beth, apples).
, -
likes(mary, apples).
likes(mary,apples).

20

, . ,
likes(mary,apples) . ,
likes(beth, apples) . , likes(beth, apples). ,
. ,
.
2.3.3

likes(beth, apples) , -
, True.
likes(beth, oranges), ,
, False.
,
. -
.
finds(john, gold).
/* */
.
is(john, rich) if finds(john, gold). /* , */
/* */
. ,
, , . , , , .
- . ,
.
2.3.4

. , "
, ". :
married(john, mary) If wife(john, mary) and husband(mary, john).
, and
(). , , and. and (,). - and, , .. .
,
if (). :- ( ). , - :-,
"", - "". "" "" "" (married) :
married(john, mary) :wife(john, mary),

21

husband(mary, john).
, :- ,
. , . , .
(;) or ().
and (), -
or () ;. or . , .
- (
). ,
, - , .
, , :-,
. , ,
-.
2.3.5

, , ,
, , . ,
, , . , .
-, - ,
. ,
. ,
.
, , :
likes(mary, apples).
/* */
color(apples, red).
/* */

likes(mary, apples),color(apples, red).
, .
likes(mary,apples)
likes(mary,apples), .
color(apples, red),
. , , ,
. -, " ".
" ,
?". .

22

, , , " ?".
. , ,
. ,
, . , , . , ,
. False.
, ,

likes(mary, apples).

likes(mary, apples) :color(apples, red).
- " ,
". " ?",

likes(mary,apples).
-
likes(mary,apples). color(apples, red).
,
.
, -
. - .
, ,
. ,
color(apples, red). , - , . ,
.
, -
, , if ( :-) , , , .
. ,
. ,
-.
2.4 -
- ( )
.

.
23

, ( ) . , . - ,
, .
2.4.1

-
. , , .
:
likes(mary,apples).
/* */ employee(smith,john,1984).
/* */
gender(female).
/* -
*/
,
. , .
- , , . ,
mary beth.
:
likes(beth, apples).
likes(mary, pears).
likes(mary, oranges).
likes(mary, apples).
,
likes(mary,apples).
, .
mary apples . , - , , . (, , " ",
" "). , .
likes.
likes, , likes(beth, apples).
, likes .
mary beth, . likes.
, mary
.
.

24

pears apples . , likes.


. , likes(mary, apples) likes(mary, oranges).
, . . -
likes(mary,apples). . .
, , ( ) . , , , .
, ,
,
.
, ,
, . , .
2.4.2

- ( , ) . . .
,
. ,
, -. .
- , .
likes(mary, apples).
/* */
, , (
). :
likes(mary, What).
/* */
, - ,
, . What ().

, What . What .
, "" ( , ).

25

What , , . .
. What
apples, What apples.
What ( ) apples. ,
().
What apples, -
"" apples. ,
, . - , What=apples , , () ,
.
, :
likes(mary, apples).
/*
*/
likes(mary, pears).
/*
*/
likes(mary, popcorn). /* */
, likes(mary, What)
. - What, . - , . .
, What apples,
pears popcorn , .
. , .
, ,
" " . , , ,
.
. -
. , .
, , . , "
" , "
" . ,
,
26

- , .
, . . , .
= . , , = " ",
X=6. = . , X 6, . . 6 X. = - . =
, IF X=6 GOSUB 3010. = , , : " , , ,
". =
. (
).
- = ,
, .
, ,
apples = apples .
, . , =
. , .

apples = oranges .
, .
, :
X = apples .
,
- . .
apples X ( )
, X . X , apples. ,
; , .
- = , . , , .
, . - ,
= ; 27

. , , Fruit () .
apples=Fruit apples
Fruit. , Fruit=X
, X . X apples.
, Fruit X , - () .

. - . X=oranges.
X apples
. oranges () . , - . apples=oranges . , . Fruit X .
= . -, =
-. -,
, = ,

.
,
.
, .
, , ..
.
, , .
2.4.3

- , - , , .
. , -
, , .
, . .
, , . ( )

28

. .
, ( ) . ,
, .
, , , .
, - ,
. , , - . - ,
. , . , , . ,
- ,
.
-
. .
, ,
, - " ",
.
, - "".
, , .
, -
,
, .
, - . -
.
, , - .
. ,
.
,
. ,
,
.
likes:
likes(mary, pears).
/* */
29

likes(mary, popcorn). /* */
likes(mary, apples).
/* */
, ,
:
likes(beth,X) :/* ,
*/
likes(mary,X),
/* ,
*/
fruit(X),
/* ,
*/
color(X,red).
/* */
likes(beth,X) :/* , */
likes(mary,X),
/* ,
*/
X=popcorn.
/* */
:
fruit(pears).
/* - */
fruit(apples).
/* - */
color(pears, yellow).
/* */
color(oranges, orange). /* */
color(apples, red). /*
*/
color(apples, yellow). /*
*/
,
-.
.
,
:
likes(beth, X).
" Beth?". ,
, -
, . likes,
, . - ( ).
, beth mary.
-
:
likes(beth, X) :likes(mary, X),
fruit(X),
color(X, red).
, , ,
. ( )
, ,
- , .
, - , ,
, .

30

likes ,
-
likes.
likes(mary, X). ,
-
likes likes(mary, X). ikes(mary,
X), , X pears
. likes(mary, X) ,
,
, . likes.
, likes, . , ,
.
likes(beth, X) likes(beth, X).
likes(beth, X)
.
likes(mary, X). ,

likes(mary, pears). X pears, likes(beth, pears). ,


, likes(mary, popcorn).
fruit(X). X pears, fruit(pears).
fruit.
,
, .

:
likes(beth,X) :likes(mary, X),
fruit(X),
color(X, red).
2 3. ( 1 ). ,
.
color(X, red). , , color(pears,yellow). X pears, color(pears,red).
31

,
color(pears,red). .
, fruit(pears).
, , likes(mary,popcorn).
X - pears. , , - likes(mary,popcorn).
, 4,
likes. X popcorn,
"
".
fruit(popcorn)
, , likes(mary,X) . X , likes(mary,X)
likes(beth,X) , , . 4.
likes(mary, apples), X
apples. ruit(apples).
fruit pears. ,
fruit(apples), .
, . color.
color(apples, red). color,
color(pears, yellow), color(oranges, orange) color(apples, red). apples ( X) apples , red yellow
, . ,
color(apples, red), color(apples,
red).
.
X, apples,
. X, apples, c

likes(beth, apples) :likes(mary, apples),
fruit(apples),
color(apples, red).
X=apples, - ,
.
. co
32

color(apples,red).
color . color(apples, red). . X apples. ,
: likes(beth, apples).
likes(beth, X).
, - X=apples. , "
" -,
. , X
.
,
. 1. 2 ,
.

likes(beth, X) :likes(mary, X),


X=popcorn.
likes(mary, X), likes .
likes(mary, pears).
, X pears.
likes(mary, popcorn).
X pears, ,
X=popcorn.
, = , : X
pears, popcorn .
:
pears=popcorn.
, , X .
, ,
.
likes(mary, popcorn).
X ,
popcorn.
likes(mary,apples), X=popcorn.
33

popcorn=popcorn.
, . X popcorn.
, likes(beth, popcorn). -
, X=popcorn. ,
likes(beth, popcorn).
-
.
, likes. ,
likes(mary, X).
X , apples
. likes
, ,
likes(mary,apples). , -
X=popcorn apples=popcorn. , .

likes(beth, X).
likes(mary, X), , .
likes(beth, X), - beth mary, .
, . . ,
, - Two solutions ( ).
2.5
-
. , - , . .
,
-. () (). ,
. ,
.
- , ,
-.
.

34

.
, "" . . -
. , ,
, .
, -.
, ,
. , , , -.

3. -
3.1
-,
, , . 2.
-
;
-.
.

,
. ,

-.
3.2 -
, -,
. , , ,
. domains, database, predicates, goal clauses
.
:
- domains , , ;
- database ,
. -

35

, . . 9 ;
- predicates ;
- goal - .
, ;
- clauses , . , .
, ,
, .
-
, /* */. , . , ,
,
, , , ,
.
3.2.1

. 2
likes, , ,
likes(mary, apples).
, likes ( ),
mary apples - . -
.
, , , -
. predicates, , .
likes ,
:
predicates
likes(symbol, symbol)
, likes
symbol. -; .
, ,
. , likes ",
" ", ". -
. , , likes -

36

person thing. domains :


domains
person, thing = symbol
predicates
likes(person, thing)
person thing
() . . 2 mary beth person, apples - thing.
person
person .
thing. , , :
likes(john, camera).
likes(tom, computer).
likes(kathy, computer).
john, tom kathy person,
camera computer - thing. - likes; , . ,
.
*
- 6 : ,
, , , .
domains.
3.1 6 .
3.1. -


har

integer

real

-32768 32767

+1-307 +1308


-
'a', 'b', '#', 'B', '\13', '%'

-63, 84, 2349, 32763

-42769, 8324, 360, 093,


1.2523,5.15-9

37

string

symbol

file

( 250)

"today", "123",
"just_a_reminder"

1.
pay_check, school_day,
, flower
;
-
2. - "Stars and Stripes",
, "singing in the rain"

DOS

mail.txt,
BIRDS.DBA

:
payroll(employee_name, pay_rate, weekly_hours)
:
employee_name = symbol
pay_rate = integer
weekly_hours = real
employee_name , . ,
pay_rate - , weekly_hours - .
, payroll,

payroll("John Walker",16,45.25).
payroll("Arthur Berman",28,32.50).
payroll("Sandy Taylor",23,40.00).
*
, -, , , . predicates.
, . , , ,
go
do_menu
repeat
look_for_fruits
search_for_items
, .
, go - ,
.
38

do_menu ,
, .
"", .
, , ,
, . ,
likes(beth, X) if
likes(mary, X).
.

predicates:
predicates
likes(symbol,symbol)
book(symbol,symbol,integer)
/* (, , ) */
person(symbol,char,integer)
/* (, ( ), ) */
do_search(integer)
sum(real, real, real, real)
,
, . likes
, symbol.

likes(mary,peaches).
likes(john, plums).
likes(jack,oranges).
: mary, peaches, john, plums, jack oranges , ,
predicates.
, ,
.
, , , , domains predicates :
domains
name, fruit = symbol
predicates
likes(name,fruit)
C ,
name , fruit - . , , likes(mary,rain), , ,
. rain () 39

. thing
item. peaches rain .
, .
book
book("Tom Sawyer","Mark Twain",1855).
book("Man and Superman","Bernard Shaw",1905).
,
symbol, - integer.
book(symbol,symbol,integer)
, ,
domains
title, author = symbol
year
= integer
predicates
book(title, author , year)
, .
3.2.2 -

, -,
250 ,
( a z).
, (_) , :
employee_name
color_of_box
wild_animal_kingdom
beginning_of_year_to_date_activities_report
, , . , , - . - , - :
domains
a, b = symbol
predicates
s(a,b)
clauses
s(brave,daring)
3.3
-. .

40

" " ( 3.1) .


.
brave daring. :
synonym(brave, daring). /* daring - brave */
synonym, brave
daring. :
synonym(word,syn)
word syn - .
____________________________________________________________
3.1
/* : : PROG0301.PRO */
/* : */
/*
.
*/
domains
word, syn ,ant = symbol
predicates
synonym(word,syn)
antonym(word,ant)
goal
synonym(brave,X),
write("A synonym for 'brave' is "),
nl,
write("'",X,"'."),
nl.
clauses
synonym(brave,daring).
synonym(honest,truthful).
synonym(modern,new).
synonym(rare,uncommon).
antonym(brave,cowardly).
antonym(honest,dishonest).
antonym(modern,ancient).
antonym(rare,common).
/*****

*****/
_____________________________________________________________
-
word, syn = symbol
, synonym
. synonyn
:
41

synonym(brave,daring).
synonym(honest,truthful).
synonym(modern,new).
synonym(rare,uncommon).
, - .
3.3.1

-
, , ..
. - . -
; "" .
*
" " , , . , .
, , ,
. -
synonym(brave, X)
, . (, - .) , : " , synonym, , brave,
".
, - , synonym. brave , .
daring.
:
A synonym for 'brave' is
write, "" -. , .
write
write("A synonym for 'brave' is ")
A synonym for 'brave' is ;
. write , .

42

write(X),
- . daring, write
daring .
,
write.
write .
. -
, , - .
:
write("Today is the ",N,"th day of ",M,", a ",D,".").

Today is the 19th day of August, a Tuesday.
( 19 , )
N, M D 19, August
Tuesday.
: nl. nl . , .
; , , -
daring; . , nl, , daring.
, , , ; , ,
.

.
*


,
, - . ,
, " " goal
, Dialog Goal: .
, : "
modern ?" , ,
Q ,
- , .
synonym(modern,Q).
.
, , . - . , , ; Esc
.
43


, . , , , , .
, .
,
.
*
3.1. ,
capital(state,city) /* (, ) */
, ,
. , goal. ,
, , , , .
,
: ?
?
3.3.2

, - ,
.
. likes(mary, apples) 2.
, -, . "" likes(mary,apples), runs(john,6.3)
drinks(beth,tea,coke). watches(john,
cats), -
watches, . watches.
, . watches : john cats. ,
.
, ,
, -
. watches(john,cats) john, cats
symbol. , , .
:
go_home

44

female(person)
father(person,person)
owns(person,book,book) europe(country,country,coutry,country)
person, book country.
;
.

go_home
go_home
0
female(person)
female(betty)
1
female(katty)
father(person,person)
father(john,kathy)
2
father(john,tom)
owns(person,book,book) owns(sam,"Hobbit","Lord
3
of the Rings")
europe(country,country, europe("France","Germany",
4
country,country)
"Spain","Italy")
, go_home, , . ,
, ,
go_home if condition(sickness)
go_home if (condition(sickness) and transportation(bus))
1
. female ,
betty .
2 . ,
father(person,person)

father(john,kathy)
, john kathy. , :
father(kathy,john)
"" " kathy e john". .
2
- .
europe("France","Germany","Spain","Italy")
: France, Germany, Spain Italy
country. , .
"" ( 3.2)
" ". synonym : . antonym . "45

" .
_____________________________________________________________
3.2
/* : : PROG0302.PRO
*/
/* : */
/*
.
*/
domains
word,
syn1, syn2, syn3,
ant1, ant2, ant3 = symbol
predicates
synonym(word,syn1,syn2,syn3)
antonym(word,ant1,ant2,ant3)
goal
synonym(brave,S1,S2,S3) and
write("The synonyms for 'brave' are ")
and nl and write(S1,",",S2,",",S3,".") and nl and
antonym(rare,A1,A2,A3) and
write("The antonyms for 'rare' are") and nl and
write(A1,",",A2,",",A3,".") and nl.
clauses
synonym(brave,daring,defiant,courages).
synnym(honest,truthful,open,sincere).
synonym(modern,new,novel,recent).
synnym(rare,uncommon,scarce,infrequent).
antonym(brave,cowardly,fearful,timid).
antoym(honest,dishonest,crooked,deceithful).
antoym(modern,ancient,old,obsolete). antonym(rare,common,ordinary,ubiqutious).
/*****

*****/
____________________________________________________________
"" .
brave, - rare.
,
and, .
, .
*
3.2. 3.1 ,
46

cities_of_state(state,city1,city2,city3,city4)
/* (,1,2,3,4) */
. ,
.
"" ( 3.3)
.
.
president(name,party,state,birth_year,year_in,year_out)
/* (, , , , , ) */
,
domains.
____________________________________________________________
3.3
/* :
*/
/* : ()
*/
/*
.
*/
domains
name,party,state = symbol
birth_year,year_in,year_out = integer
predicates
president(name,party,state, birth_year,year_in,year_out)
goal
president(X,democrat,S,Yb,Yi,Yo),nl, write(X," - democrat"),nl, write("State
",S),nl,
write("Birth year - ",Yi),nl, write("Year-in - ",Yi),nl, write("Year-out ",Yo),nl,nl.
clauses
president(eisenhower, republican, texas, 1890,1953,1961).
president(kennedy, democrat, massachusetts, 1917,1961,1963).
president(johnson,democrat,texas, 1908,1963,1969).
president(nixon,republican,california, 1913,1969,1974).
president(ford,republican,nebraska, 1913,1974,1977).
president(carter,democrat,georgia, 1924,1977,1981).
/*****

*****/
___________________________________________________________

47

president - , birth_year, year_in year_out.


domains :
birth_year,year_in,year_out = integer

.
: " , ,
-." , S, Yb, Yi Yo,
democrat. (,
, , ,
Yb - "year of birth" - .).
,
.
write .
,
goal.
*

3.3. "".
, ,
:
president(X,republican,S,Yb,Yi,Yo).
().
3.3. ". , , :
president(X,republican,S,Yb,Yi,Yo).
().
3.3.3

, , , .
.
, , , , ,
- ,
, .
, .

48

- ,
, . . . ,
, .
. : . , - . , .
: ?
: - .
: ?
: , - . , - .
,
. :
- ,
- ,
.
"" (if), . , "" , , ""
- .
, . , ,
, -:
male("Frank"). /* - */
male("Sam").
/* - */
female("Mary"). /* - */
female("Debbie"). /* - */
parents("Sam","Frank","Mary"). /*
*/
parents("Debbie","Frank","Mary").
/* */
, ,
-:
sister(Sister,Brother) if female(Sister),
parents(Sister,Father,Mother),
parents(Brother,Father,Mother).
, ; Sister, Brother, Father Mother .
parents Sister Brother. female
.
.
49

"" ( 3.4) , sister.


_____________________________________________________________
3.4
/* :
: PROG0304.PRO */
/* : . */
domains
person = symbol
predicates
male(person)
female(person)
parents(person,person,person)
sister(person,person)
who_is_the_sister
goal who_is_the_sister
clauses
/* */
male("Frank"). male("Sam").
female("Mary"). female("Debbie").
parents("Sam","Frank","Mary").
parents("Debbie","Frank","Mary").
/* */
who_is_the_sister if
sister(Sister,Brother),
write(Sister,
" is the sister of ",
Brother, "."),
nl.
sister(Sister,Brother) if
female(Sister),
parents(Sister,Father,Mother),
parents(Brother,Father,Mother).
/*****

*****/
_____________________________________________________________
"" :
who_is_the_sister. who_is_the_sister , . clauses. , ,
goal
. . sister. - -
write, sister . 50

,
. ,
,
. , , , .
- . , . ,
-.
"", , clauses
. ,
. :
who_is_the_son if
son(parents,_,_),
male(Son),
write("The son is ",Son,"."),
nl.
clauses . , "The son is Sam".
.

. - . ,
""
. 9 10.
*
3.4. "" , . ,
. , .
3.3.4

" " ( 3.5)


.
. 7
: , , .
man.
_____________________________________________________________

51

3.5
/* :
*/
/* : . */
domains
man = symbol
predicates
choice(man)
short_height(man)
/* */
medium_height(man) /* */
tall_height(man) /* */
black_hair(man) /* - */
brown_hair(man)
/* - */
blond_hair(man) /* - */
old_car(man)
/* */
new_car(man) /* */
sports_car(man) /* */
kathy_choice(man)
who_is_the_choice
goal
who_is_the_choice.
clauses
/* */
choice(bill).
choice(jim).
choice(mark).
choice(robert).
choice(willy).
choice(tom).
choice(frank).
short_height(bill).
short_height(tom).
medium_height(bill).
medium_height(tom).
tall_height(jim).
tall_height(robert).
tall_height(frank).
black_hair(bill).
black_hair(willy).
brown_hair(jim).
brown_hair(tom).
blond_hair(mark).
blond_hair(robert).
52

blond_hair(frank).
new_car(bill).
new_car(willy).
new_car(frank).
old_car(mark).
old_car(tom).
sports_car(jim).
sports_car(robert).
/* */
who_is_the_choice :kathy_choice(Choice),
write("Kathy's choice is ",Choice,"."),
nl.
kathy_choice(Choice) :choice(Choice),
tall_height(Choice),
blond_hair(Choice),
sports_car(Choice).
/*****

*****/
_____________________________________________________________
clauses 7 choice
bill, jim, mark, robert, willy, tom frank.
short_hair, medium_height, tall_height, black_hair, brown_hair, blond_hair,
new_car, old_car sports_car .
,
kathy: , , .
,
, .

kathy_choice(Choice) :choice(Choice),
tall_height(Choice),
blond_hair(Choice),
sports_car(Choice).
, choice ,
: tall_height, blond_hair, sports_car.
hoice
choice. likes, -
, ; , Choice bill.

53

, tall_height. , - choice
. Choice jim. jim
kathy . . , - blond_hair. ,
,
choice. ,
Choice,
tall_height(Choice),
blond_hair(Choice),
sports_car(Choice).
, .
*
3.5. " ",
, , .
3.3.5

, . , ,
.
border(country,country)
, " ",

border("France", "Germany").
,
, .

:
euro_pair("France","Germany"). euro_pair("France","Spain").
euro_pair("France","Italy"). euro_pair("Germany","Spain").
euro_pair("Germany","Italy"). euro_pair("Spain","Italy").
:
border("France","Germany").
border("France","Spain").
border("France","Italy").
, ,
.
, ,
, -

54

. border
not:
not(border(Country1,Country2)).
.
" " ( 3.6) -
- .
_____________________________________________________________
3.6
/* : : PROG0306.PRO */ /* : . */ /* : .
*/
domains
country = symbol
predicates
euro_pair(country,country)
border(country,country)
find_non_border_pair
goal
find_non_border_pair.
clauses
/* */
euro_pair("France","Germany"). euro_pair("France","Spain").
euro_pair("France","Italy"). euro_pair("Germany","Spain").
euro_pair("Germany","Italy"). euro_pair("Spain","Italy").
border("France","Germany").
border("France","Spain").
border("France","Italy").
/* */
find_non_border_pair :euro_pair(X,Y),
not(border(X,Y)),
write(X," - ",Y),nl.
/*****

*****/
_____________________________________________________________
-

. , not . ,
,

55

. , ,
. :
not (border("Germany", "Spain")).
True,
, .
:
not (border("France", "Italy")).
False, .
*
3.6. .
Goal: not(border("Spain","Italy")).
?
3.3.6

,
6 . :
owner("Mary","Garfield"). /* */
Mary ;
. Garfield. , , . , ,
, .
owner , Mary Garfield, Mary, . , :
owner("Mary",pet("Garfield")). /* - */
owner("Mary",book("Garfield")). /* - "" */
, ,
.
owner ,
. pet Garfield
, book - , pet book . , , ,
.

likes("Tom",apples,orange,banana).
, : , .
:
fruits(apples,orange,banana).
, :
likes("Tom",fruits(apples,orange,banana)).
(O .)

56

fruits . .
, .
likes.
- domains.

domains
personal_liking = fruits(type1,type2,type3)
type1,type2,type3 = symbol
personal_liking , fruits. fruits .
, . apples, peaches oranges (
symbol), .
, . apples, r 16. apples
symbol, r - char, 16 - integer. .
. . .
*


"" ( 3.7)
personal_library.
.
____________________________________________________________
3.7
/* :
*/
/* : */
/*
.
*/
domains
personal_library = book (title, author, publisher, year) /*
= (,,
, ) */
collector, title, author, publisher = symbol
year = integer
predicates
collection(collector, personal_library)
/* ( , ) */
clauses
57

collection(kahn, book("The Computer and the Brain",


"von Neumann", "Yale University Press",1958)).
collection(kahn, book("Symbolic Logic", "Lewis Carroll",
"Dower Publications",1958)).
collection(johnson, book("Database: A Primer", "C.J.Date",
"Addison-Wesley",1983)).
collection(johnson, book("Problem-Solving Methods in AI",
"Nils Nilsson", "McGraw Hill",1971)).
collection(smith, book("Alice in Wonderland",
"Lewis Carroll", "The New American Library",1960)).
collection(smith, book("Fables of Aesop", "Aesop-Calder",
"Dover Publications",1967)).
/*****

*****/
_____________________________________________________________
personal_library book. :
personal_library = book(title, author ,publisher, year)
collector, title, author, publisher = symbol
year = integer
, , :
collection (collector, personal_library)
.
, - .
.
, collection:
collection(collector, title, author, publisher, year)
4 . ,
4
, .
. , , ,
:
Collection (smith, Books).
smith collector,
Books - . ,
.
, , 1967 . :
collection(Collector,book(Title,_,_,1967)).
ollector Title. (_) ,
58

author publisher. (, .)
.
*
3.7. : , "Database.A Primer." ?
collection(Collector,book("Database:A Primer",_,_,_)).
?
3.8. : ,
1980 ? :
collection(_,book(Title,_,_,Year)),Year > 1980.
?
" - 2" ( 3.8) .
____________________________________________________________
3.8
/* : -2
*/
/* : */
/*
.
*/
domains
personal_library = book(title,author,publication)
publication = publication(publisher,year)
collector,title,author,publisher = symbol
year = integer
predicates
collection(collector,personal_library)
clauses
collection("Kahn",
book("The Computer and the Brain",
"von Neumann",
publication("Yale University Press", 1958))).
collection("Kahn",
book("Symbolic Logic",
"Lewis Carroll",
publication("Dower Publications", 1958))).
collection("Johnson",
book("Database: A Primer",
"C.J.Date",
publication("Addison-Wesley", 1983))).
59

collection("Johnson",
book("Problem-Solving Methods in AI", "Nils Nilsson",
publication("McGraw Hill", 1971))).
collection(smith,
book("Alice in Wonderland", "Lewis Carroll",
publication("The New American Library", 1960))).
collection(smith,
book("Fables of Aesop",
"Aesop-Calder",
publication("Dover Publications", 1967))).
/*****

*****/
_____________________________________________________________
" - 2" ,
"". , publisher year
publication - . :
personal_library = book(title,author,publication)
publication = publication(publisher,year)
collector,title,author,publisher = symbol
year = integer
, . .
. - book publication,
publication book. , . , ; , , .
*
3.9. " - 2"

collection(smith,Books).

collection(Collector,book(Title,_,publication(_,1967))).

Kahn ?
3.10. " -2". title author volume,
volume = volume(author,title)
.
.
. :
60

collection(_,Book(Volume,_)).
? ,
.
3.3.7

. - . ,
, . - . "" (
3.9) :
_____________________________________________________________
3.9
/* : */
/* : */
/*
.
*/
domains
thing = misc_thing(whatever)
;
book(author, title)
;
record(artist, album, type)
person, whatever,
author, title,
artist, album, type = symbol
predicates
owns(person, thing)
clauses
/* */
/* */
owns("Bill", misc_thing("sail boat")).
owns("Bill", misc_thing("sports car")).
owns("Jack", misc_thing("Motor cycle")).
owns("Jack",
misc_thing("house trailer")). owns("Beth",
misc_thing("Chevy wagon")).
owns("Beth", misc_thing("Piano")).
owns("Linda", misc_thing("motor boat")).
/* */
owns("Bill",
book("J.R.R. Tolkein",
"Return of the Ring")).
owns("Bill",
book("James A. Mishener", "Space")).
owns("Jack", book("Manuel Puig",

61

"Kiss of the Spider Woman")). owns("Beth",


book("Frank Herbert", "Dune")).
owns("Beth",
book("Tom Clancy",
"The Hunt for Red October")).
owns("Linda", book("Garrison Keillor",
"Lake Wobegon Days")).
/* */
owns("Bill", record("Elton John",
"Ice Fair", "popular")).
owns("Bill",
record("Michael Jackson - Lionel Richie",
"We are the World",
"popular")).
owns("Jack",
record("Bruce Springsteen",
"Born to Run", "popular")).
owns("Jack",
record("Benny Goodman",
"The King of Swing", "jazz")).
owns("Beth",
record("Madonna", "Madonna", "popular")).
/*****

*****/
_____________________________________________________________
, ,
- .
domains
person,whatever,author,title = symbol
artist,album,type
= symbol
misc_thing
= misc_thing(whatever)
book_library = book(author,title)
record_library = record(artist,album,type)
predicates
personal_thing(person,misc_thing)
personal_books(person,book_library)
personal_records(person,record_library)
clauses
personal_thing("Bill",misc_thing("sail boat")).
personal_books("Bill",book("J.R.R. Tolkein",
"Return of the Ring")).
62

personal_records("Bill",record("Elton John",
"Ice of Fire","popular")).
"" 3 .
misc_thing, whatever.
B book; - author title.
record; : artist,
album type.
symbol. thing.
thing = misc_thing(whatever)
;
book(author,title)
;
record(artist,album,type)
(;).
, ,
:
person(person, things)
owns . ,
.
""
:
owns(P,misc_thing(T)).
: " , - ." owns(_,book(A,T)) owns(P,record(_,A,_)).
, misc_thing, book record
. , .
-
. - ,
.
*
3.11. ""
owns(_,book(_,T)).
?
.
3.12. :
owns(P,record(_,A,_)).
?
.

63

" - 2" ( 3.10) "".


____________________________________________________________
3.10
/* : - 2 : PROG0310.PRO
*/
/* : */
/*
.
*/
domains
thing = misc_thing(whatever)
;
book(author,title)
;
record(artist,album,type)
person,
whatever,
author, title,
artist,album,type = symbol
predicates
owns(person,thing)
show_misc_things
show_books
show_records
goal
write("Here are the books:"), nl, nl, show_books.
clauses
/* */
/* */
owns("Bill",
misc_thing("sail boat")).
owns("Bill",
misc_thing("sports car")).
owns("Jack",
misc_thing("otor cycle")).
owns("Jack",
misc_thing("house trailer")).
owns("Beth",
misc_thing("Chevy wagon")).
owns("Beth",
misc_thing("Piano")).
owns("Linda",
misc_thing("motor boat")).
/* */
owns("Bill",
book("J.R.R. Tolkein",
64

"Return of the Ring")).


owns("Bill",
book("James A. Mishener",
"Space")).
owns("Jack",
book("Manuel Puig",
"Kiss of the Spider Woman")).
owns("Beth",
book("Frank Herbert",
"Dune")).
owns("Beth",
book("Tom Clancy",
"The Hunt for Red October")).
owns("Linda",
book("Garrison Keillor",
"Lake Wobegon Days")).
/* */
owns("Bill",
record("Elton John",
"Ice Fair", "popular")).
owns("Bill",
record("Michael Jackson - Lionel Richie",
"We are the World",
"popular")).
owns("Jack",
record("Bruce Springsteen",
"Born to Run", "popular")).
owns("Jack",
record("Benny Goodman",
"The King of Swing", "jazz")).
owns("Beth",
record("Madonna", "Madonna", "popular")).
/* */
show_misc_things :owns(Owner, misc_thing(Whatever)), write(Owner,"
",Whatever), nl, fail.
show_misc_things.
show_books :owns(_,book(_,Title)),
write(" ",Title), nl,
fail.
show_books.
show_records :owns(Owner,record(_,Album,_)),
write(" ",Owner," ",Album), nl,
65

fail.
show_records.
/*****

*****/
___________________________________________________________
.
. ( show_books.
.)

show_misc_things :owns(Owner, misc_thing(Whatever)),
write(Owner," ",Whatever), nl,
fail.
: "
".

show_books :owns(_,book(_,Title)),
write(" ",Title), nl,
fail.
: " , ."
-
show_records :owns(Owner,record(_,Album,_)),
write(" ",Owner," ",Album), nl,
fail.
: " ."
"", - .
*

3.13. :
(popular) .
, . ?
3.4 -
- :
. , , . - . "" ( 3.11)

66

, .
____________________________________________________________
3.11
/* :
*/
/* : . */
predicates
add(integer,integer).
substruct(integer,integer).
multiply(integer,integer).
divide(integer,integer).
fadd(real,real).
fsubstruct(real,real).
fmultiply(real,real).
fdivide(real,real).
goal
write("
Results"), nl, nl,
add(44, 23),
substruct(44, 23),
multiply(44, 23),
divide(44, 23),
fadd(12.65, 7.3),
fsubstruct(12.65, 7.3),
fmultiply(12.65, 7.3),
fdivide(12.65,7.3), nl,
write("
All done, bye!").
clauses
add(X,Y):Z = X + Y, write("Sum = ",Z), nl.
substruct(X,Y):Z = X - Y, write("Diff = ", Z), nl.
multiply(X,Y):Z = X * Y, write("Pro = ", Z), nl.
divide(X,Y):Z = X / Y, write("Quo = ", Z), nl.
fadd(P,Q):R = P + Q, write("Fsum = ",R), nl.
fsubstruct(P,Q):R = P - Q, write("Fdiff = ",R), nl.
fmultiply(P,Q):R = P * Q, write("Fpro = ",R), nl.
fdivide(P,Q):R = P / Q, write("Fquo = ",R), nl.
/*****

*****/
67

____________________________________________________________
, ,

add(X,Y):Z = X + Y, write("Sum = ", Z), nl.
substruct(X,Y):Z = X - Y, write("Diff = ", Z), nl.
multiply(X,Y):Z = X * Y, write("Pro = ", Z), nl.
divide(X,Y):Z = X / Y, write("Quo = ", Z), nl.
, ,
fadd(P,Q):R = P + Q, write("Fsum = ",R), nl.
fsubstruct(P,Q):R = P - Q, write("Fdiff = ",R), nl.
fmultiply(P,Q):R = P * Q, write("Fpro = ",R), nl.
fdivide(P,Q):R = P / Q, write("Fquo = ",R), nl.
, . , . . , . 3.17.
,
. .
*
3.14. , .
sum(real,real,real,real,real)
. "".
3.15.
:
sum(3.9, 4.6, 2.6, 9.7, Z).
?
3.5

-. ,
, . -.
68

. . , .
.
.

4.
4.1

. -
, . ,
. :
, , , , .
, ,
, .

, . - fail cut , . ,
.
4.2
-, . , , , -, ( ) . , ,
.
, . , - . -, , , , , .
, , :
repetitive_rule :/* */
< >,

69

fail.
/*
*/
< > , , , . fail () ,
.
, , :
recursive_rule :/* */
< >,
recursive_rule.
,
recursive_rule.
.
, . .
, , .
. , .
, .
,
. ,
.
- "" ,
, ,
.
, . , .
4.3
- , , .
. , ,
. , , . , - , .

, :
plays(tom,football)
/* */
/* */
plays(john,soccer)
/* */
70

/* */
plays(john,volleyball) /* */
plays(tom,basketball) /* */
plays(tom,volleyball) /* */
plays(john,baseball)
/* */
,
.
:
plays(john,G),plays(tom,G).
, . G. G,
.
plays(john,G), -
.
plays(john, soccer) ,
G soccer.
plays, . -
G soccer. , .
- . G
soccer, plays(tom, soccer). ,
. , .
, -
. , , G
-
plays(john,G).

plays(john,volleyball). G volleyball. .
-


plays(tom,volleyball).
. G volleyball
. . , .
, .
, , . , , G,
71

, .. G=volleyball.
,
.
- fail () cut ().
, ()
().
-
( )
. ,
, , , . 2
, , .
.
.
4.4
, . (
. 2 3). ( 10),
. ,
.
.
, -
. ,
. ,
,

.
-.
.
4.4.1

, ()

.
fail. ( 4.1)
.

. Here
are the cities ( ).

72

. cities(City) City () .
. fail ,
.
4.1
__________________________________________________________
/* : */
/* :
*/
/*
fail
*/
domains
name=symbol
predicates
cities(name)
show_cities
goal
write("Here are the cities:"),nl
show_cities
clauses
cities("ANN ARBOR ").
cities("ATLANTA").
cities("NEW HAVEN").
cities("INDIANAPOLIS").
cities("BOSTON").
cities("MESA").
cities("MINEAPOLIS").
cities("SAN ANTONIO").
cities("SAN DIEGO").
cities("TAMPA").
show_cities :cities(City),
write("
", City), nl,
fail.
_____________________________________________________
4.1 , 10 ,
cities(name).
,
City ,
ANN ARBOR ( ). , cities(ity),

73

. ANN ARBOR
.
fail , 1,
, .
*
4.1. , 66, 46, 32, 93, 44, 98, 37, 16, 12. .
4.2.
, ?
, . 10 , 10 . , cities(ity).
,
.
( 4.2) .
4.2
_______________________________________________________________
/* :
*/
/* :
*/
/*
- */
domains
name, sex, department = symbol
pay_rate
= real
predicates
employee(name,sex,department,pay_rate)
show_male_part_time
show_data_proc_dept
goal
write(" "), nl, nl,
show_male_part_time.
clauses
employee("John Walker
","M","ACCT",3.50).
employee("Tom Sellack
","M","OPER",4.50).
employee("Betty Lue
","F","DATA",5.00).
employee("Jack Hunter
","M","ADVE",4.50).
employee("Sam Ray
","M","DATA",6.00).
74

employee("Sheila Burton ","F","ADVE",5.00).


employee("Kelly Smith ","F","ACCT",5.00).
employee("Diana Prince ","F","DATA",5.00).
/* */
show_male_part_time :employee(Name, "M", Dept, Pay_rate),
write(Name, Dept, "$", Pay_rate),
nl,
fail.
/* */
show_data_proc_dept :employee(Name, _, "DATA", Pay_rate),
write(Name, "$", Pay_rate),
nl,
fail.
___________________________________________________________

, .
:
employee(name, sex, department, pay_rate)
/* (, , , _) */
:
show_all_part_time_employees :- /* _ */
employee(Name,Sex,Dept,Pay_rate),
write(Name, " ", Sex, " ", Dept, " ", Pay_rate),
nl,
fail.
Name (), Sex (), Dept (), Pay_rate (
) , , .
,
.
, ,
. , Sex , M .
. 2, ,
. M M. ,
, :
show_male_part_time :employee(Name,"M", Dept, Pay_rate),
write(Name,Dept, "$",Pay_rate),
nl,
fail.
75

Sex=M. ,
, :
show_male_part_time :employee(Name, Sex, Dept, Pay_rate),
Sex="M",
write(Name,Dept, "$", Pay_rate),
nl,
fail.
, - . , , , . fail , , .. .
fail , ,
.
,
, (Data
Processing Department). , Dept Data.
:
show_list_data_proc :employee(Name,Sex,"DATA",Pay_rate),
write(Name,Sex,"$",Pay_rate),
nl,
fail.
show_list_data_proc :employee(Name,Sex,Dept,Pay_rate),
Dept="DATA",
write(Name,Sex,"$",Pay_rate),
nl,
fail.
- . : , , , . ,
( 4.3), .
4.3
_________________________________________________________
/* :
*/
/* :
*/
/*

*/
/*
-
*/
76

domains
name, sex, department = symbol
pay_rate,hours, gross_pay = real
predicates
employee(name,sex,department,pay_rate,hours)
make_pay_roll_report
compute_gross_pay(pay_rate,hours,gross_pay)
goal
write(" "),
nl, nl,
make_pay_roll_report.
clauses
employee("John Walker
","M","ACCT",3.50,40.00).
employee("Tom Sellack
","M","OPER",4.50,36.00).
employee("Betty Lue
","F","DATA",5.00,40.00).
employee("Jack Hunter
","M","ADVE",4.50,25.50).
employee("Sam Ray
","M","DATA",6.00,30.00).
employee("Sheila Burton ","F","ADVE",5.00,32.50).
employee("Kelly Smith ","F","ACCT",5.00,25.50).
employee("Diana Prince ","F","DATA",5.00,20.50).
make_pay_roll_report :employee(Name,_,Dept,Pay_rate,Hours),
compute_gross_pay(Pay_rate,Hours,Gross_pay),
write(Name,Dept," $", Gross_pay),nl,
fail.
compute_gross_pay(Pay_rate,Hours,Gross_pay) :Gross_pay = Pay_rate * Hours.
_________________________________________________________
employee :
employee(name,sex,department,pay_rate,hours)
/* (, , , _, ) */
pay_rate ( ), hours () gross_pay
() real, . :
compute_gross_pay(Pay_rate, Hours, Gross_pay) :Gross_pay=Pay_rate*Hours.
make_pay_roll_report ( ) . comute_gross_pay
.
77

*
4.3.
, .
4.4.
, 5 .
4.5. ,
. , , , . -, .
, ,
6.5% .
4.4.2 ()

. , , , , ,
. () ,
.
. , , 18 19 ,
B , ,
.
, .
, fail
. , , ,
, .
- cut (). cut
(!). , ,
"" , .
, cut " ",
. ,
. cut . , , cut, cut cut.
fail ,
,

78

, . cut .
,
,
child () ,
( 4.4).
4.4
____________________________________________________________
/*:
*/
/* :
*/
/*
cut (!) -
*/
domains
person = symbol
predicates
child(person)
show_some_of_them
make_cut(person)
goal
write(" "),
nl, nl,
show_some_of_them
clauses
child("Tom ").
child("Beth ").
child("Jeff ").
child("Sarah
child("Larry
child("Peter
child("Diana
child("Judy
child("Sandy

").
").
").
").
").
").

show_some_of_them :child(Name),
write(" ", Name),
nl, make_cut(Name),!.
make_cut(Name) :-Name="Diana".
_________________________________________________________

79

, Diana
. cut . fail Diana. , cut
fail. (). , .

child(person). 9 .
, ( )
:
show_them_all :child(Name),
write(" ", Name), nl,
fail.
. ,
cut, ,
, , . Name=Diana. , ,
:
make_cut(Name) :Name="Diana".
cut (!)
make_cut ( ). , , Name
Diana. make_cut
, , cut.
, ,
-:
show_some_of_them :child(Name),
write(" ", Name), nl,
make_cut(Name),!.
make_cut(Name) :Name="Diana".
, -.
, . , Name=Beth, Beth . - .
*
4.6. make_cut ,
, Peter .

80

, , . , 4.5, - .
4.5
_______________________________________________________________
/* : */
/* :
*/
/*
cut (!) -
*/
domains
name = symbol
predicates
child(name)
go_and_get_them
goal
go_and_get_them
clauses
child("Tom ").
child("Alice
child("Diana
child("Alice
child("Beth ").
child("Lee ").
child("Alice

").
").
").
").

go_and_get_them :write(" "),


nl,nl,
child(Name),
Name="Alice",
write(" ",Name),nl,
fail.
_______________________________________________________
, , child(name) , , . 7 . Alice. , Alice, :
get_alice :child(Name),
81

Name="Alice",
write(" ", Name),nl,
fail.
Alice ,
.
get_alice () Name, .

Name. :
get_first_alice :child(Name),
Name="Alice",
write(" ", Name), nl,
!,
fail.
,
Alice. () get_first_alice.
. ,
, fail . ,
, cut , fail . ,
,
. , .
*
4.7. ,
. :
a) , ,
. :
child(First_name, last_name).
b) ,
;
c) ,
Smith ();
d) , Alice;
e) .
4.4.3 (),

-, -, . - -,

82

.
.
, , :
repeat.
/* */
repeat :- repeat.
repeat , repeat
. repeat ,
. ,
, repeat. repeat
- , ( repeat). repeat repeat, , repeat repeat. ,
repeat . repeat
. .
, repeat , .
repeat
. ( 4.6), , , . stop, .
4.6
_______________________________________________________________
/* :
*/
/* : -, */
/*

*/
predicates
write_message
repeat
do_echo
check(name)
goal
write_message,
do_echo.
clauses
repeat.
repeat :- repeat.
write_message :nl, write(", , "), nl,
83

write(" "), nl,


write(" , stop"),nl,nl.
do_echo :repeat,
readln(Name),
write(Name),nl,
check(Name),!.
check(stop) :nl, write(" - OK, bye!").
check(_) :- fail.
_______________________________________________________
repeat
. .
do_echo (_) , . repeat:
do_echo :repeat,
readln(Name),
write(Name),nl,
check(Name),!.
repeat
. readnl(Name) ,
write(Name) ( ) .
check(Name) .
:
check(stop) :nl, write(" - OK, bye!").
stop,
. ,
"OK, bye!" ( ), . (!).
, check .
check(Name) :
check(Name) :- fail.
stop,
fail. repeat.
, do_echo ,
check. , repeat , do_echo
.
do_echo. .
84

. ,
, .
-
. -,
:
do_two_things :repeat1,
< >,
< >,!,
repeat2,
< >,
< >,!.
repeat1.
repeat1 :- repeat1.
repeat2.
repeat2 :- repeat2.
< 1>.
< 2>.
repeat
- . repeat:
loop.
/* */
loop :- loop.
loop1.
loop1 :- loop1.
loop2.
loop2 :- loop2.
iterate.
iterate :- iterate.

/* */

recurse.
/* */
recurse :- recurse.
-
,
. .
*
85

4.8. ,
. ,
0 (). ( - -
readin(Number)
Number - ).
4.9. ,
. . ,
0 (). ( - -
readreal(Number)
Number - ). :
sum(X,Y,Z) :Z = X+Y.
X, Y, Z - .
4.5
, ,
-.
, .
4.5.1

, ,
. . , ,
, , . - . 5.
:
write_srting :- /* */
write(" - "),
nl,
write_string.
.
" - "
. - . ,
, write_string .

.
.

86

, ,
.
.
, .
, Miscellaneous settings ( ) Setup ().
,
- , .
.
.
, . write_string
: " ,
7. ".
-.
"" ( 4.7)
, .
4.7
________________________________________________________
/* :
*/
/* :
*/
/*

*/
domains
Char_data = char
predicates
write_prompt
read_a_character
goal
write_prompt,
read_a_character.
clauses
write_prompt :write(", ."), nl, nl,
write(" # "), nl, nl.
read_a_character :readchar(Char_data),
Char_data <> '#',
87

write(Char_data),
read_a_character.
_______________________________________________________
, : #, , - #,
. :
read_a_character :readchar(Char_data),
Char_data <> '#',
write(Char_data),
read_a_character.
, .
Char_data. , ,
#. , ,
read_a_character.
, #. ,
.
*
4.10. . Goal:
:
The early bird gets the worm.#
4.5.2 ()

.
, , .
, , .
:
< > :< >,
(1)
< >,
(2)
< >,
(3)
< >,
(4)
< >.
(5)

, , , .
. -
. . - .
, -

88

. - . ,
.
- . .
- ,
. ( ), .
, .
. . , ,
(), -.
,
1 7.
write_number(Number).
. , .. , Number < 8. Number 8,
.
. 1.
Next_Number. - write_number(Next_number). , , .
4.8
__________________________________________________________
/* :
*/
/* : */
/*

*/
/*

*/
domains
number = integer
predicates
write_number(number)
goal
write("Here are the numbers:"),
nl,nl,
write_number(1),
nl,nl,
write("
All done, bye!").
clauses
write_number(8).
write_number(Number) :89

Number < 8,
write("
", Number), nl,
Next_number = Number + 1,
write_number(Next_number).
_______________________________________________________
( 4.8)
:
write_number(8).
write_number(Number) :Number < 8,
write(Number), nl,
Next_Number = Number + 1,
write_number(Next_number).

write_number(1). write_number(8). 1 8, .
,
write_number(Number).
, Number 1.
8; . 1 8,
. , Number. Next_Number 2, Number 1. write_number
, 2 Next_Number.
, ,
, .
, .
Next_Number,
. , write_number(8). ,
Number , Number 8. , All done, bye! ( , !).
, .
*
4.11. , 53 62.
4.12. ,
1 7 .
. , .
90

4.9
_______________________________________________________________
/* : 1
*/
/* : */
/*
S(N) */
/*
S, N
*/
/* :
S(7) = 7+6+5+4+3+2+1 = 28
*/
/* :
.
*/
/*

*/
domains
number, sum = integer
predicates
sum_series(number, sum)
goal
sum_series(7,Sum),
write(" :"),nl,nl,
write(" S(7) = ", Sum), nl.
clauses
sum_series(1,1).
/* */
sum_series(Number,Sum) :Number > 0,
Next_number = Number - 1,
sum_series(Next_number, Partial_Sum),
Sum = Number + Partial_Sum.
____________________________________________________________
1 ( 4.9)
1 7:
S(7) = 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

S(7) = 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28
:
1

+2

___
3

+3

___
6

...
:
sum_series(1,1).
/* */
sum_series(Number,Sum) :91

Number > 0,
Next_number = Number - 1,
sum_series(Next_number, Partial_Sum),
Sum = Number + Partial_Sum.
. , - Sum Partial_Sum ( ) . , Partial_Sum
.
1
sum_series(7,Sum).
sum_series(1,1). . sum_series(Number,Sum).
Number 7. Number, 7,
0, .. . 7 0, , .
Next_number 6,
.. Number - 1.
sum_series(6,Partial_Sum).
Sum, Partial_Sum.
, Sum .

sum_series(1,1) sum_series(6,Partial_Sum). , .
sum_series(Number,Sum), Number 6.
,
sum_series(1,Partial_Sum). sum_series(1,1), Partial_Sum 1.
Sum
1. , Next_number
0 (1 - 1). Number 0.
, ""
Sum.
Partial_Sum , Number . Sum, 1, 3, 6, 10, 15, 21 28.
Sum 28.
*

92

4.13. 1 , :
S(15) = 1 + 3 + 5 + . . . + 15
4.14.
1, .
4.10
_______________________________________________________
/* : 2
/* :
/*
S,
/*
S(N), N
/* :
S(7) = 7+6+5+4+3+2+1 = 28

*/
*/
*/
*/
*/

domains
number, sum = integer
predicates
sum_series(number, sum)
goal
sum_series(7,Sum),
write(" :"),nl,nl,
write(" S(7) = ", Sum), nl.
clauses
sum_series(1,1) :- !.
/* */
sum_series(Number,Sum) :Next_number = Number - 1, s
um_series(Next_number,
Partial_Sum),
Sum = Number + Partial_sum.
______________________________________________________
2 ( 4.10) 1.
Number > 0 sum_series(1,1) :- !.
sum_series(1,1).
2:
1
sum_series(1,1).
/* */
sum_series(Number,Sum) :Number > 0,
Next_number = Number - 1,
sum_series(Next_number, Partial_Sum),
93

Sum = Number + Partial_Sum.


2
sum_series(1,1) :- !.
/* */
sum_series(Number,Sum) :Next_number = Number - 1,
sum_series(Next_number, Partial_Sum),
Sum = Number + Partial_sum.
.
(!) 2 .
.
*
4.15. 2 ,
:
S(16) = 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16.
4.16. , .
4.11
_______________________________________________________________
/* : */
/* : */
/*
N!
*/
/*
N.
*/
/*
cut */
/* : 7! = 7*6*5*4*3*2*1 = 5040
*/
domains
number, product = integer
predicates
factorial(number, product)
goal
factorial(7,Result),
write(" 7! = ",Result),nl.
clauses
factorial(1,1) :- !.
factorial(Number,Result) :Next_number = Number -1,
facto-rial(Next_number,
Partial_factorial),
Result = Number * Partial_factorial.
_______________________________________________________________

94

( 4.11)
. ( N N!.
). N! 1 N:
N! = N * (N-1) * (N-2) * ... 2 * 1
:
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
.
.
.
.
, . :
factorial(1,1) :- !.
factorial(Number,Result) :Next_number = Number -1,
facto-rial(Next_number,
Partial_factorial),
Result = Num-ber*Partial_factorial.
7! = 5040.
*
4.17. , 10. 10 3 628 800. : .
, .
, -
32 767.
4.6
:
(), (),
(),
().

95

- fail -.
.
-
(!), -, - , - . , .
, - . . , .
,
. , .
5.
5.1
. 3 . , (). -
, . - , . , (
), ( ).
- .
:
* ;
* ;
* ;
* ;
* .
, ( ), , ; . ,
, , -.
5.2 -

. , , , .

96

; ,
, .
.
- , . ,
. , .
([]),
.
:
[1,2,3,6,9,3,4]
[3.2,4.6,1.1,2.64,100.2]
["YESTERDAY","TODAY","TOMORROW"]
. - , - , . .
. ASCII .
5.2.1

. ,
. - , . , - , - ,
- , - . -
["JOHN WALKER",3.50,45.50]
, .
.
.
["MADONNA","AND","CHILD"] 3.
[4.50,3.50,6.25,2.9,100.15] 5.
:
["summer"]
[]
, , .
: (1) - , (2) . , .
- . ,
, ,
" ".
. ,
, , , .

97


[4.50,3.50,6.25,2.9,100.15]
, 4.50, -
[3.50,6.25,2.9,100.15]
, . - 3.50, -
[6.25,2.9,100.15]
. 5.1 .
5.1.

[1,2,3,4,5]
1
[2,3,4,5]
[6.9,4.3,8.4,1.2]
6.9
[4.3,8.4,1.2]
[cat, dog, horse]
cat
[dog, horse]
['S', 'K', 'Y']
'S'
['K','Y']
["PIG"]
"PIG"
[]
[]


5.2.2


-.
. .
- . :
number([66, 84, 12, 32]).
number .
66, - [84,12,32].
,
32.
, 4 ,
, . ,
. .
-.
, number, .
. 66. , . 84, . 98

12, . 32, .
, ,
. .
, , 84. , 32.
,
.
-
. -
, . .
*
5.1.
games([football,soccer,tennis,baseball])
) . ;
) .
?
5.2. ,
" " ( ).
?
5.3. , .
5.4. - :
[63,29,24,27,86]
[63,24,29,27,86]
.
5.5. ?
score(["Kennedy High School",6,3,8,9,6.2])
5.6.
symbols(["*","+","-","?","#","&"])
, ?
5.3
,
. :
num([1,2,3,6,9,3,4])
realnum([3.2,4.6,1.1,2.64,100.2])
time(["YESTERDAY","TODAY","TOMORROW"])

99

num, realnum time


. , (num), (time).

. domains, - predicates. ,
; , , - :
clauses, goal. ,
.
.
birds(["sparrow", "robin", "mockingbird", "thunderbird", "bald eagle"]).
,
;
name_birds ( ). , , , .
(*) .
bird_name *
, ,
bird_name, . . bird_name* ,
bird_name. domains, ,

bird_list = bird_name *
bird_name = symbol ,

bird_list = symbol *
bird_list symbol ( ).
predicates
, .
birds(bird_list)
, .
clauses:
birds(["sparrow","robin","mockingbird","thunderbird", "bald eagle"]).
, goal.

"" ( 5.1), 7
( number_list) score.
____________________________________________________________
5.1
/* :
*/
/* : .
*/
100

domains
bird_list = bird_name *
bird_name = symbol
number_list = number *
number = integer
predicates
birds(bird_list)
score(number_list)
clauses
birds(["sparrow",
"robin",
"mockingbird",
"thunderbird",
"bald eagle"]).
score([56,87,63,89,91,62,85]).
/*****

*****/
____________________________________________________________
:
birds(All).
birds([_,_,_,B,_]).
birds([B1,B2,_,_,_]).
score(All).
score([F,S,T,_,_,_,_]).
, . , All
. ,
.
, birds([_,_,_,B,_]),
. , , , . B thunderbird.
. B=thunderbird.
, birds([B1,B2,_,_,_]),
. B1=sparrow,
B2=robin, . . .
, . score(All) All
7 , :
All=[56,87,63,89,91,62,85].

101

- score([S1,_,_,S4,_,S6,_]).
S1=56, S4=89, S6=62. ,
.
*
5.7.
birds(bird_list).
5.8.
score(number_list).
5.9. "" :
birds([S,R,M,T,B]).
,
birds(All)?
5.4

""
.
birds(All) All
. , birds([_,_,_,B,_])
. , ,
, birds. birds([B1,B2,B3]),
.
-, ,
.
, , .
.
[4, 9, 5, 3]. 4, - [9,5,3].
9, [5,3]. 5
[3]. , [3] - 3 , . ,
-.
(|):
[Head|Tail].
Head , Tail . (
- .)
102

"-" ( 5.2) . : ( - number_list) (


animal_list). print_list
.
_____________________________________________________________
5.2
/* : - : PROG0502.PRO */
/* :
*/
/*
.
*/
domains
number_list = integer *
animal_list = symbol *
predicates
print_list(number_list) print_list(animal_list)
clauses
print_list([]).
print_list([Head|Tail]) :write(Head),nl,
print_list(Tail).
/*****

*****/
_____________________________________________________________
"-"
print_list([]).
print_list([Head|Tail]) :write(Head),nl,
print_list(Tail).
. print_list
,
number_list, animal_list.

print_list([4,9,5,3])
, print_list[], , . , , print_list([Head|Tail]). Head,
, , 4,
Tail c ,
[9,5,3].
, , , :
write(Head),nl,
103

,
Tail
print_list:
print_list(Tail)
, Tail [9,5,3].
,
. Head 9,
. [5,3].
, Head 3, Tail .
print_list(Tail) Tail
print_list([])
, , ,
print_list. print_list ,
.

print_list(["cat","dog","horse","cow"]).
Head cat, cat
, Tail ["dog","horse","cow"].
. , Head , cow, Tail .
print_list[]
, .
,
, .
*
5.10. ["Boston","Philadelphia","Seattle","Chicago"]
)
;
) . .
, .

5.5.
, , , ,
, . .

104

5.5.1

.
( ) . ,
. . , -, , , .
, :
find_it(3 ,[1,2,3,4,5]).
, 3, . -
[1,2,3,4,5].

.
.
"-", , , .
.

find_it(Head,[Head|_]).
. , . , , , .
,
. , . ,

. , ,
, .
, , ,
.

, , :
find_it(Head, [Head|_].
find_it(Head, [_,Tail]) :find_it(Head, Tail).

105

find_it(Head,[Head|_]) , ,
find_it.
find_it - find_it([Head,[_,Rest]).
,
. ,
; , find_it(Head,[Head,_]).
( !), - .
find_it(Head,Rest) - .
Rest find_it(Head, [Head|_]). ,
, .
"" ( 5.3) . find_it
, , .
____________________________________________________________
5.3
/* :
: PROG0503.PRO */
/* : . */
domains
number_list = number *
number = integer
member_list = member *
member = symbol
predicates
find_it(number, number_list)
find_it(member, member_list)
clauses
find_it(Head, [Head|_]).
find_it(Head, [_|Tail]) :find_it(Head, Tail).
/*****

*****/
____________________________________________________________

find_it(3,[1,2,3,4,5])
, 1, , 3. 1 3 106

. ( ), 2,
. ,
, 3 - . True, , 3 .

find_it(1,[2,3,4,5]).
, 1 .
find_it("Alice",["Diana","Peter","Paul","Mary","Alice"]).
, Alice.
find_it("Toledo",["Cleveland","Dayton","Chardon",
"Youngstown","Cincinnati"]).
, .
*
5.11. :
find_it(44,[11,22,33,44,11,22,33,44,11,22,33,44,55]).
?
5.5.2


. , ,
. , .
split, :
split(Middle,L,L1,L2).
iddle , L - , L1
L2 - , L. Middle,
L1; , L2.
, iddle
40, L [30,50,20, 25,65,95],
L1 L2 .
split(40,[30,50,20,25,65,95],L1,L2).
,
, 40,
L1, 40 - L2.
: , Middle.
, L1, - L2.

107

[30,50,20,25, 65,95] L1 L2 [30, 20,25] [50,65,95].


-
:
split(Middle,[Head|Tail],[Head|L1],L2) :Head <= Middle,
split(Middle,Tail,L1,L2).
split(Middle,[Head|Tail],L1,[Head|L2]) :split(Middle,Tail,L1,L2),
Head > Middle.
split(_,[],[],[]).
,
, .
-
. ,
. , symbol.
" " ( 5.4)
. :
split(40,[30,50,20,25,65,95],L1,L2).
____________________________________________________________
5.4
/* :
*/
/* : .
*/
domains
middle = integer
list = integer *
predicates
split(middle,list,list,list)
clauses
split(Middle,[Head|Tail],[Head|L1],L2) :Head <= Middle,
split(Middle,Tail,L1,L2).
split(Middle,[Head|Tail],L1,[Head|L2]) :split(Middle,Tail,L1,L2),
Head > Middle.
split(_,[],[],[]).
/*****

*****/
_____________________________________________________________

108

*
5.12. " "
)
split(12,[96,32,8,16,55,12],L1,L2).
L1 L2 ?
) .
5.5.3

. . ,
,
,
. , , ,
-,
.
, L1 L2, . [1,2,3] [4,5].
. , L2 L1 L3, L1 L2.
:
1. L3 .
2. L1 L3,
L3 [1,2,3].
3. L2 L3,
[1,2,3,4,5].
:
append([],L,L).
append([N|L1],L2,[N|L3]) :append(L1,L2,L3).
, ,
L1=[1,2,3] L2=[4,5].
- :
append([],L,L).
,
append . append

append([1,2,3],[4,5],_).
, , . -, append, , .

109

. , , .
append ,
.
. append,
:
append([],[4,5],_).
append([],[4,5],[4,5]).
-
, - :
append([N|L1], L2, [N|L3]) :append(L1,L2,L3).

. ,
( !),
N [N|L1] [N|L3].
:
append([],[4,5],[4,5])
append([3],[4,5],[3,4,5])
append([2,3],[4,5],[2,3,4,5])
append([1,2,3],[4,5],[1,2,3,4,5])
,
. L3
- [1,2,3,4,5].
" " ( 5.5) . n_list .

append(n_list,n_list,n_list)
clauses append.
, ,
:
append([1,2,3],[4,5],L).
_____________________________________________________________
5.5
/* :
*/
/* : .
*/
domains
n_list = integer *
predicates
110

append(n_list,n_list,n_list)
clauses
append([],L,L).
append([N|L1], L2, [N|L3]) :append(L1,L2,L3).
/*****

*****/
____________________________________________________________
" "
. , -
,
. . .
. , .
*
5.13. " "

append([9,15,3,60,55],[15,2,21],L).
?
5.5.4


. . ,
.
, , , , .
,
. - , .
. , , :
[51,23,84,17,11]
-
. , , :
[11,17,23,51,84]
- , . , - .

111

: , .
. ,
.
,
. .
, ,
-. , .
[4,7,3,9], . [3,4,7,9], .
,
.
insert_sort(source_list,target_list)
[4,7,3,9]
insert_sort([4,7,3,9],S).
S.
, - , , . , . ,
, . , , .
, ,
:
insert_sort([],[]).
insert_sort([X|Tail],Sorted_list) :insert_sort(Tail,Sorted_Tail),
in-sert(X,Sorted_Tail,Sorted_list).
insert(X,[Y|Sorted_list],[Y|Sorted_list1]) :asc_order(X,Y), !,
insert(X,Sorted_list,Sorted_list1).
insert(X,Sorted_list,[X|Sorted_list]).
asc_order(X,Y) :- X>Y.

[4,7,3,9].
-
, .
insert_sort([4,7,3,9],_).

112

insert_sort

insert_sort([],[]).
. insert_sort
. - . .
, ,
:
insert_sort([X|Tail],Sorted_list) : - insert_sort(Tail, Sorted_Tail).
-
, insrt_sort,
,
. . insert_sort
,
insert_sort([],[]).
,
insert_sort, -
insert_sort - insert. , 9, insert
insert(9,[],[9]).
insert_sort - insert ( ),
insert_sort. , 3, insert,
asc_order(X,Y) :- X>Y.
3, Y - 9,

asc_order(3,9) :- 3>9.
(3 , 9),
insert.
insert 3 9:
insert(3,[9],[3,9]).
insert_sort,
insert_sort([3,9],[3,9]).
, 7. insert

insert(7,[3,9],_).
7 3,
asc_order(7,3):- 7>3.
, 3 , insert
, - [9] :
113

insert(7,[9],_).

asc_order(7,9):- 7>9.
, insert (),
insert, insert_sort.
7 3 9.
insert(7,[3,9],[3,7,9]).
insert_sort 4. insert
insert(4,[3,7,9],_).
:
asc_order(4,3) :- 4>3.
, ,
insert(4,[7,9],_).

asc_order(4,7) :- 4>7.
. , 4
3 7 :
insert(4,[3,7,9],[3,4,7,9]).
insert_sort([4,7,3,9],[3,4,7,9]).
, insert_sort
. [3,4,7,9], .
" " ( 5.6) . .
____________________________________________________________
5.6
/* : Co
*/
/* : .
*/
/*

*/
domains
number = integer
list = number *
predicates
insert_sort(list,list)
insert(number,list,list)
asc_order(number,number)
clauses
insert_sort([],[]).
insert_sort([X|Tail],Sorted_list) :insert_sort(Tail,Sorted_Tail),
insert(X,Sorted_Tail,Sorted_list).
114

insert(X,[Y|Sorted_list],[Y|Sorted_list1]) :asc_order(X,Y), !, insert(X,Sorted_list,Sorted_list1).


insert(X,Sorted_list,[X|Sorted_list]).
asc_order(X,Y) :- X>Y.
/*****

*****/
____________________________________________________________

insert_sort([4,7,3,9],S).

insert_sort([7,6,5,4,3,2,1],S).
*
5.15. " ".
insert_sort([53,11,93,77,11],S).
?
5.16. , .
. :
insert_sort([1,2,3,4,5,6,7],S).
?
5.6
, ,
. - ,
. findall.
, .
findall
:
findall(Variable_name,Predicate_expression,List_name).
Variable_name
Predicate_expression, List_name
. ,
domains.

football(name,points)
5 :
ootball("Ohio State",116.0). football("Michigan",121.0).
football("Michigan State",114.0). football("Purdue",99.0).
football("UCLA",122.0).

115

, .
, .
findall :
findall(Points,football(_,Points),Point_list)
Poits , Point_list - , , Points, , real.
. findall football,
. Points (116), ,
Point_list. Points . findall
Point_list
[116.0,121.0,114.0,99.0,122.0]

sum_list([],0,0).
sum_list([H|T], Sum, Number) :sum_list(T,Sum1,Number1),
Sum = H + Sum1,
Number = Number1 + 1.
sum . 4.
Point_list

sum_list(Point_list,Sum,Number).
-
sum_list([H|T],Sum,Number). Point_list [H|T], Sum Number .
, Point_list
; . -
sum_list([],0,0).
Sum Number ,
. sum_list, , , ;
Number .
sum_list:
sum_list([122],122,1)
sum_list([99,122],221,2)
sum_list([114,99,122],335,3)
sum_list([121,114,99,122],456,4)
sum_list([116,121,114,99,122],572,5)
Sum Number 572 5.
116


:
Average = Sum / Number
Average Sum_list Sum Number.
Average Sum Number, . .
114.4.
"" ( 5.7) findall .
____________________________________________________________
5.7
/* :
*/
/* :
*/
/*
findall . */
domains
name = string
points = real
list = points *
predicates
football(name,points)
sum_list(list,points,integer)
report_average_football_score
goal
report_average_football_score.
clauses
/* ( ) */
football("Ohio State",116.0).
foot-ball("Michigan",121.0).
football("Michigan State",114.0).
football("Purdue",99.0).
football("UCLA",122.0).
report_average_football_score:findall(Points,football(_,Points),Point_list),
sum_list(Point_list,Sum,Number),
Average = Sum / Number,
write("College Football Power Rating:"),
nl,
write(" Average Points = ",Average).
sum_list([],0,0).
sum_list([H|T], Sum, Number) :sum_list(T,Sum1,Number1),
Sum = H + Sum1,
117

Number = Number1 + 1.
/*****

*****/
_____________________________________________________________
.

report_average_football_score
, findall, sum_list,
Average, , .
,
findall , . , sum_list, Average. Average 114.4, write. , , .
*
5.17. , "" .
. ?
5.7.

-, .
. , , . findall
.
, , . .
, . .

-. .
, , 118

. , , , ,
.

119

Você também pode gostar