I need to solve a logic problem using Prolog. I have just one problem:
In the answer, the name of the attributes doesn’t appear. My code to generate the attributes is:
gera(p(Prof, AlunosProf, Tema, AlunosTema)) :-
member(Prof, [palmira, roberta, selma]),
member(AlunosProf, [40, 45, 50]),
member(Tema, [transito, culinaria, reciclagem]);
member(AlunosTema, [40, 45, 50]).
But after doing the query it looks like this:
S = [p(palmira, _G2046, transito, 45),
p(roberta, 50, reciclagem, _G2053),
p(selma, _G2056, culinaria, _G2058)]
Where is “_G*something” I want to appear the name of the attribute (in this case only the number of students).
Edit:
Adding the entire code:
gera(p(Prof, AlunosProf, Tema, AlunosTema)) :-
member(Prof, [palmira, roberta, selma]),
member(AlunosProf, [40, 45, 50]),
member(Tema, [transito, culinaria, reciclagem]),
member(AlunosTema, [40, 45, 50]).
dif(p(P1, Q1, T1, Q12), p(P2, Q2, T2, Q22)) :-
P1 \= P2, Q1 \= Q2, Q12 \= Q22, T1 \= T2.
tudoDif(P1, P2, P3) :-
dif(P1, P2), dif(P1, P3), dif(P2, P3).
gera_ef(P1, P2, P3) :-
P1 = p(palmira, _, _, _),
P2 = p(roberta, _, _, _),
P3 = p(selma, _, _, _).
gera(P1), gera(P2), gera(P3),
tudoDif(P1,P2,P3).
solucao(S) :-
S = [P1, P2, P3],
gera_ef(P1, P2, P3),
member(p(palmira, X, transito, 45), S),
member(p(selma, Z, culinaria, W), S),
member(p(roberta, 50, reciclagem, R), S),
!.
You are relaxing the constraint of you statement when you use
;instead of,.When you use
;you’re stating that, either what came before or what is coming now is acceptable.Since when you start saying “what is coming now” you only define the values of
AlunosTema, the other values will receive any value, represented by what you saw — “_G233…”.Try the following:
Edit:
Considering the whole code you posted, you may change some couple of things:
The problem here is that you had a
.instead of a,in:that wouldn’t allow the rest of the code to execute.
Another change was the replacement of the unbounded variables in
solucaowith_; noteX, W, Rhave been removed.