Prolog - Little exercise on facts

2019-08-23 11:10发布

问题:

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). 

回答1:

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.



回答2:

The findall part :

calculate(Price) :-
    List = [ Price || producte(_, Price, _) ],
    sum_list(List, 0, Sum).

The sum_list part :

sum_list([], Acc, Acc).
sum_list([Head|Tail], Acc, Sum) :-
    NewAcc is Acc + Head,
    sum_list(Tail, NewAcc, Sum).

I guess something along these lines should work according to visual-prolog doc but I kinda don't wanna install visual-prolog to test it...