Search code examples
databaseprologfactvisual-prolog

Prolog - Little exercise on facts


Ok. That's my problem. I need to implement a predicate that sums up all the prices of the products in the list. But, for now, I'm not getting any further with it. What am I doing wrong? Thanks in advance.

domains
    state =  reduced ; normal

database
    producte (string, integer, state)
predicates
    nondeterm calculate(integer)

clauses 
   % ---> producte( description , price , state )
    producte("Enciam",2,normal). 
    producte("Llet",1,reduced). 
    producte("Formatge",5,normal). 
    calculate(Import):-
        producte(_,Import,_).
    calculate(Import):-
        producte(_,Import,_),
        calculate(Import2),
        Import=Import2+Import,!.

Goal
    calculate(I). 

Solution

  • Disclaimer: I'm a bit daft when it comes to prolog. Also, I don't have access to a prolog interpreter right now.

    The canonical example, sum of a list:

    sum([], 0).
    sum([Head | Tail], Total) :- sum(Tail, Temp), Total is Head + Temp.
    

    making a list with findall/3:

    findall(Val, producte(_, Val, _), Vals).
    

    Vals has your list you want to sum.

    Update: per your comment, I'm a bit out of my depth without access to an interpreter.

    calculate(I) :- sum(Vals, I), findall(Val, producte(_, Val, _), Vals).
    

    What I think this does:

    uses your single goal I, which receives the result of summing your Vals list, which is generated by findall. But it's been so long since I've used prolog that I may not even have the syntax right for doing what I want. However, a small variation should accomplish what you want with a single goal.