-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesercizio18.pl
46 lines (40 loc) · 909 Bytes
/
esercizio18.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
% ------------------
% ** ESERCIZIO 18 **
% ------------------
% Si supponga di avere nel Data Base PROLOG fatti del tipo a(X,Y), dove X e Y sono atomi (non necessariamente
% numerici). Si scriva un predicato PROLOG somme(S1,S2) che istanzi S1 ed S2 rispettivamente
% alla somma dei primi e dei secondi argomenti numerici dei fatti del tipo sopra citato
% contenuti nel Data Base.
%
% ?- somme(S1,S2).
% S1 = 5,
% S2 = 4.
:-dynamic totale/2.
totale(0,0).
a(a,b).
a(1,2).
a(a,1).
a(a,bcde).
a(1,1).
a(3,b).
somme(_,_) :-
a(X,Y),
valori(X,Y),
fail.
somme(S1,S2) :-
totale(S1,S2),
% azzero il conteggio
retract(totale(S1,S2)),
asserta(totale(0,0)).
valori(X,_) :-
totale(A,B),
integer(X),
Z is A + X,
retract(totale(A,B)),
asserta(totale(Z,B)).
valori(_,Y) :-
totale(A,B),
integer(Y),
S is B + Y,
retract(totale(A,B)),
asserta(totale(A,S)).