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).
推荐答案
免责声明:关于序言,我有点傻.另外,我现在无法访问序言解释器.
Disclaimer: I'm a bit daft when it comes to prolog. Also, I don't have access to a prolog interpreter right now.
典型示例,列表总和:
sum([], 0).
sum([Head | Tail], Total) :- sum(Tail, Temp), Total is Head + Temp.
使用findall/3列出列表:
making a list with findall/3:
findall(Val, producte(_, Val, _), Vals).
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).
我认为这是做什么的
使用您的单个目标I
,该目标接收由findall生成的Vals列表求和的结果.但是自从我使用序言以来已经有很长时间了,以至于我什至都没有正确的语法来做自己想做的事情.但是,只需进行少量改动就可以实现一个目标.
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.
这篇关于Prolog-关于事实的小练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!