05 1024x768

05 1024x768



comc locciher in lisi pcoccsaring in PROLOG. The lisi element* thcmselvcs are codoMd b>- brackets [ | and are separuted by commas. Examplcs of PROLOG liats are:

[tom. dek. Harry, frodl

U


The tirsi element* of a list may be separaled from ihc taił of the lisi by the bar operator. I The Uil of a lisi is the list with iis first element remoyed. For instancc. when the list ił (tom.dick.harry.fred). the tirsi element i* tom and the taił is the list (dlck. Harry, fred) Using the vcrtic;»l bar operator and uniAcation. we can break a list into its componcMc

lf [tom. dick. Harry, frod] «s matchcd to (XIY). ihcn X = tom and Y - (dlck. Harry, fradj

If [tom.dick.harry.frod] is matchcd to patiem (X. Y IZ), then X ■ tom . Y ■ dick. andZ*

(Harry, frod]

li ftom, dlck. harry. frod] is matchcd to (X. Y. ZI W), then X ■ tom. Y ■ dlck. Z « harry. md

W = (frod)

lf (tom. dick. horry, frod) is matchcd to (W. X. Y. ZIV). then W ■ tom. X ■ dlck. Y ■ tony.

Z * frod. and V • ( ) .

[tom. dick. horry. frod) will not match (V. W. X, Y. ZI U] .

[tom, dick. horry. frod) will match (tom. XI (harry. frodQ. to gńc X = dick.

Besides “lcaring lists aport** to get at particular element*. unitication can be nd o “build" the list stmcturc. For cxample. if X * tom. Y = (dick). and L unifics with (XIYJ. then L will be bound to [tom. dick). Thus terms separated by commas bcfocc the I areał element* of the list. and the structure after the I is always a list. the taił of the list.

Ld's takc a simplc cxample of recursivc Processing of lisls: the mombor chcck. We deftne a prcdicate to determine whether an i tern. rcprcscntcd by X. is in a bt Tho prcdicatc member takes two arguments. an element and a list, and is tnie if the element h a member of the lisi. For cxamplc:

?- momborfa. [a. b. c. d. o]).

?- memberfa. [1.2. 3.4]).

?- member[X. [a, b. c]). X = a

X ■ b

X ■ c no

610


PART VI / LAN6UAGES FOR Al PROBLEM SOLVIN G

To dcłlnc member rccursńcly. we first test if Xis the first item in the list;

mombor(X. [XITJ).

This test* whether X and the firn element of the list are idcniical. lf they are not. then it is natur.il to chcck whether X is an element of the rcsl (T) of the list. This i% defincd by:

membor(X.[Y|TJ)membor(X. T).

The two lines of PROLOG for chccking list membership are then:

member(X. [XI TJ).

membor(X. [YlTD:- member(X. T).

This examplc illustratcs the importance of PROLOG** built-in order of scarch with the lerminaling condition placed before the rccursńe cali. to be tested bcforc the algorithm recurs. lf the order of the prcdicatcs is rcverscd. the terminating condition may ncvcr be checkcd. We now trące momborfc.(a.b.cj). with numbering:

1: membor(X. [XI TJ).

2: member(X. [Y [ TJ)member(X. T).

?• mombor(c, [a, b. ej), cali 1. fali, sińce c * o

cali 2. X = c. Y = a. T ■ Ib, ej. member(c. [b.cj)? cali 1. fali. sińce c * b cali 2. X - c. Y - b. T - jej. mamborfe. (ej)?

cali 1. success. c = c yes (to second cali 2.) yes (to first cali 2.)

y—

Good PROLOG style suggests the use of anoaymous wriabłrs. These serve as an iadicaiion to the programmer and interpreter that certain \anablcs are used solcly for pataenwnatching purposes. with the \-ariable btndmg itself not part of the computatioa proces*. Thus. when we test whether the element X is the same as the first item in the list w ustialiy say: momber(X, fX| J). The use of the _ indicatcs that even tbough the taił of ibc list płays a cnie tal part in the unification of a qocry. the content of the taił of the list is oatmportant. In the member chcck the anonymous \ariablc should be used in the recursńe totemem as well. wherc the valuc of the head of the Ust is unimportami

momber(X, (XI _J).

mombor(X, LI T|) mombor(X. T).

Writing out a list one element to a linę is a nicc cxcrcisc for understanding both lisa ud recursive conirol. Supposc we wish to writc out the list (aAcxfl. We could drfinc the rccursńre command:

611


CHAPTBR 14 / AN INTROOUCTION TO PROLOG


Wyszukiwarka

Podobne podstrony:
scan0021 (5) 350 HANDBOOK OF ECOTOXICOLOGY In water the toxicity cif individual PAHs to plants and a
IMG63 [1024x768] Wykorzystanie kultur in vitro do otrzymywania mieszańców międzygatunko wydr lub&nb
SBD k2 05 g3* SBD TEST 2 (ORACLE ) f, f r t Ą /■ 1.For sacb grade find the mumber oi peopfe wto eam
Screen shot 13 02 08 at 05 03 cxplosion$, in short, of impaticnces, of cverything which denies ripe
10 1024x768 143 A Production System Example in PROLOG la the section \c write a production system so
13 1024x768 Wc n»n prcsent a »hcll structurc for dcpth-flrst scarch in PROLOG, kccping track of hoUi
C360 12 12 05 42 05 incredibfe storiesDiscussion Work In smali groups. Dlscuss these questions: &nb
55216 jow06 05 Th© big ftnture In thla ezorniac lloa in HOT <hr you oan bond. Your nrma mat r.ovc
201312275844 (1024x768) 11 nagli in~ ■« ••wib HMlnMUMł dmrai w nimranWM ałuteio* kT^1 —"■>—

więcej podobnych podstron