Prolog - Exercise on facts and lists
-
14-04-2021 - |
Question
The problem is that I need to program a predicate capable of consulting the products in a database so that it returns a list filled with the names of the products its prices are reduced(that is indicated by the "state".
domains
state = reduced ; normal
element = string
list = element*
database
producte (string, integer, state)
predicates
nondeterm reduced(list)
clauses
% ---> producte( description , price , state )
producte("Enciam",2,reduced).
producte("Peix",5,normal).
producte("Llet",1,reduced).
producte("Formatge",5,normal).
%unique case
reduced([D]):-
producte(D,_,reduced).
%general case
reduced([D|L]) :-
producte(D,_, reduced),retract(producte(D,_,reduced)),reduced(L).
Goal
reduced(List).
I appreciate it. Now, it gives me three different solutions. How could I force the predicate to give me one solution? In fact, The last one?
Solution
Since I don't use visual-prolog, I'll just propose something I found in the doc.
reduced(List) :-
List = [ Price || producte(_, Price, reduced) ].
OTHER TIPS
What about when the first product in the list is NOT reduced -- you have no rule for that case.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow