K thut lp trình Prolog 167
displaylist( [X | L ] ) :-
write( X ),
tab( 1),
displaylist( L), nl.
Yes
7 b"E"I@9-N_N1
7' `,","
Gi s mt bn ghi c s d liu, mt s, kin dng cu trúc m t ca
Prolog, có ni dung nh sau :
family( individual(tom, smith, date(7, may, 1960),
work(microsoft, 30000)),
individual( ann, smith, date(9, avril, 1962),
inactive),
[ individual( roza, smith, date(16, june, 1991),
inactive),
individual( eric, smith, date(23, march, 1993),
inactive) ] ).
Ta cn in ra ni dung bn ghi s dng v t' write(F) theo quy cách nh
sau :
parents
tom smith, birth day may 7,1960, work microsoft, salary
30000
ann smith, birth day avril 9, 1962, out of work
children
roza smith, birth day june 16, 1991, out of work
eric smith, birth day march 23, 1993, out of work
Ta xây d,ng th tc writefamily( F) nh sau :
writefamily(family(Husband, Wife, Children)) :-
nl, write(parents),nl, nl,
writeindividual(Husband) ,nl,
writeindividual(Wife), nl, nl,
write(children), nl, nl,
writeindividual(Children).
writeindividual(individual(Firstname, Name, date(D, M,
Y), Work)) :-
tab(4), write(Firstname),
tab(1), write(Name),
write(', birth day '), write(M), tab(1),
write(D), tab(1), write(', '), write(Y), write(',
168 Lp trình lägich trong Prolog
'),
writework(Work).
writeindividual([ ]).
writeindividual([ P | L] ):-
writeindividual( P), nl,
writeindividual( L).
writework(inactive):-
write('out of work').
writework(work(Soc, Sal)):-
write(' work '), write(Soc),
write(', salaire '), write(Sal).
Th,c hin íchX = ..., writefamily(X), ta nhn c k!t qu nh sau
?- X = family(individual( tom, smith, date(7, may,
1960), work(microsoft, 30000) ),individual( ann, smith,
date(9, avril, 1962), inactive),[individual( roza,
smith, date(16, june, 1991), inactive),individual( eric,
smith, date(23, march, 1993), inactive) ] ),
writefamily(X).
parents
tom smith, birth day may 7 , 1960, work microsoft, salaire
30000
ann smith, birth day avril 9 , 1962, out of work
children
roza smith, birth day june 16 , 1991, out of work
eric smith, birth day march 23 , 1993, out of work
X = family(individual(tom, smith, date(7, may, 1960),
work(microsoft, 30000)), individual(ann, smith, date(9,
avril, 1962), inactive), [individual(roza, smith,
date(16, june, 1991), inactive), individual(eric, smith,
date(23, march, 1993), inactive)])
Yes
74 GE"N1,"
) c d liu trên tp, ngi ta s dng dãy ích sau :
..., see( F), fileprocess, see( user), ...
Th tc fileprocess c và x lý ln lt t'ng hng ca F cho !n khi c
h!t tp. Mô hình th tc nh sau :
filetreat :-
read( Term),
treat( Term).
treat( end_of_file) :- !. % K!t thúc tp
treat( Term) :-
K thut lp trình Prolog 169
treatment( Term), % X hng hin hành
filetreat. % X phn còn li ca tp
Trong th tc trên, treatment( Terme) th hin mi thao tác th c
ng lên hng. Ch ng hn th tc di ây lit kê t'ng hng ca tp k t' dòng
th N tr i cho !n h!t tp, kèm theo th t, có mt ca hng ó trong tp :
viewfile( N) :-
read( Term),
viewterm( Term, N).
viewterm( end_of_file, _ ) :- !.
viewterm( Term, N) :-
write( N), tab( 2),
write( Term), nl,
N1 is N + 1,
viewfile( N1).
?- see('exp.txt'), viewfile(1), see( user), seen.
1 parent(pam, bob)
2 parent(tom, bob)
3 parent(tom, liz)
4 parent(bob, ann)
5 parent(bob, pat)
Yes
Sau ây là mt mô hình khác  x tp. Gi s file1tp d liu ngun
cha các hng có dng :
object( NoObject, Description, Price, FurnisherName).
Mi hng mô t mt phn t ca danh sách c i tng. Gi s r$ng tp cn
xây d,ng file2 cha các i tng do cùng mt nhà cung cp cp hàng. Trong
tp này, tên nhà cung cp c vi!t mt ln u tp, không xut hin trong
các i tng, dng object( No, Desc, Price). Th tc to tp nh
sau :
createfile(Furnisher) :-
write(Furnisher), write( ‘.‘), nl,
creatremaining(Furnisher).
creatremaining( Fournisseur) :-
read( Objet),
treat( Objet, Furnisher).
treat( end_of_file) :- !.
treat(object( No, Desc, Price, Furn), Furn) :-
write( object( No, Desc, Price) ),
write( ‘.‘), nl,
creatremaining(Furn).
170 Lp trình lägich trong Prolog
treat( _ , Furnisher) :-
creatremaining(Furnisher).
Gi s file1tp
see(' file1.txt'),tell(' file2.txt'), createfile(suzuki),
seen, see(user), told, tell(user).
Ví d III.10 :
Sao chép ni dung mt tp lên mt tp khác :
copie :-
repeat,
read(X),
mywrite(X),
X == end_of_file, !.
mywrite( end_of_file).
mywrite( X) :-
write( X), write( '.'), nl.
)ích sau cho phép côpy t' tp ngun f1.txt vào tp ích f2.txt :
?- tell('f2.txt'), see('f1.txt'), copie, seen, told.
Yes
Trong th tc copie s dng v t' repeat. V t' repeat luôn luôn
thành công, to ra mt vòng lp vô hn. V t' repeat c nh ngha nh sau :
repeat.
repeat :- repeat.
77 Ja!&=+
Mt s v t' xký t, ca Prolog nh sau :
Tên v t Ý ngha
put(Char) )a Char ra dòng ra hin hành, Char hoc là mt giá
tr nguyên trong khong 0..255, hoc mt ký t,
put(File, Char)
)a Char ra tp File
get_char(Char) )c t' tp File và hp nht Char vi ký t, ti!p theo.
get_char(File,
Char) Hp nht Char vi ký t, ti!p theo trong tp File.
get0(Char) )c ký t, ti!p theo
get0(File,
Char) )c ký t, ti!p theo trong tp File.
get(-Char) )c ký t, khác khong trng t' dòng vào và hp nht
vi Char.
get(File, Char)
)c ký t, khác khong trng ti!p theo trong tp File.
skip(Char) )c vào và b+ qua các ký t, c c cho !n khi gp
úng ký t, khp c vi Char.
K thut lp trình Prolog 171
skip(File,
Char)
)c vào t' tp File và b+ qua các ký t, c c cho
!n khi gp úng ký t, khp c vi Char.
Ví d III.11 :
% )a ra liên ti!p các ký t, A, B C có mã ASCII ln lt 65, 66, 67
?- put( 65), put( 66), put( 67).
ABC
yes
% )c và ghi các ký t,
?- get0(X).
|: a % o mt t, ri Enter (), không gõ du chm
X = 97
Yes.
?- get0(X).
^D
X = -1.
Yes.
Ví d III.12 :
Sau ây ta xây d,ng th tc del_space c vào mt câu gm nhiu t' cách
nhau bi các khong trng tr v úng câu ó sau khi ã loi b+ các khong
trng th'a, ch" gi li mt khong trng gia các t' mà thôi.
Th tc hot ng tng t, các th tc x tp, b$ng cách c ln lt t'ng
ký t, ri a ra màn hình. Th tc s dng k1 thut nhát ct  x tình hung
t, c vào hoc mt khong trng, hoc mt ch cái, hoc mt du
chm k!t thúc. Sau ây là th tc del_space :
del_space :-
get0( C),
put( C),
follow( C).
follow( 46) :- !. % 46 mã ASCII ca du chm
follow( 32) :- !, % 32 là mã ASCII ca du khong
trng
get( C), % B qua các du khong trng tip theo
put( C),
follow( C).
follow( Letter) :-
del_space.
Chy th nh sau :
?- del_space.
|: The robot try to cast the balls
to the basket.