Prolog未定义程序

时间:2014-09-25 07:38:48

标签: prolog

我试图根据一系列信息获取包含相关信息的三个项目列表:

product(I):-
    I = [_,_,_,_], %Type,Brand,Category,Value
    cheaper(item(apple,_,_),item(_,kay,_,_),I),
    cheaper(item(bar,_,_,_),item(_,_,fruit,_),I),
    member(item(_,kay,_,2),I),
    member(item(apple,granny,_,_),I),
    member(item(bar,_,chocolate,_),I),
    /* Below not given */
    member(item(cracker,_,_,_),I),
    member(item(_,_,biscuit,_),I),
    member(item(_,_,_,4),I),
    member(item(_,_,_,5),I).

cheaper(X,Y,H) :- %Used to determine the item values
    item(X,_,_,A),
    item(Y,_,_,B),
    A<B.

当我尝试运行它时遇到错误:

?- product(I).
ERROR: cheaper/3: Undefined procedure: item/4
Exception: (8) item(item(apple, _G3604, _G3605), _G3651, _G3652, _G3653) ? 

我知道item不是一个程序,但是我可以用什么来检查apple的值和bar的值?

1 个答案:

答案 0 :(得分:1)

首先,显而易见的是,你曾经称之为更便宜的错误:

 cheaper(item(apple,_,_),item(_,kay,_,_),I),
         ↑
         Only three values, not four.

如果item不是程序,则您不得调用它,但请使用解构。 此外,您希望那些您更便宜的物品成为列表的一部分,对吗?如果是这样,你必须检查一下。您可以使用统一来提取所需的值:

cheaper(X,Y,I) :- 
  member(X,I),
  member(Y,I),
  [item(_,_,_,A),item(_,_,_,B)] = [X,Y],
  A<B.

现在,您将收到有关未实例化参数的错误。这是因为如果它们彼此之间的大于,则检查它们(尚未)。要避免这种情况,请将cheaper/3调用移至子句正文的末尾:

product(I):-
    I = [_,_,_,_], %Type,Brand,Category,Value
    member(item(_,kay,_,2),I),
    member(item(_,_,_,4),I),
    member(item(_,_,_,5),I),
    member(item(apple,granny,_,_),I),
    member(item(bar,_,chocolate,_),I),
    /* Below not given */
    member(item(cracker,_,_,_),I),
    member(item(_,_,biscuit,_),I),
    cheaper(item(apple,_,_,_),item(_,kay,_,_),I), % note the 4th argument
    cheaper(item(bar,_,_,_),item(_,_,fruit,_),I).

有了这个,您将获得一个解决方案,然后失败并出现错误。这是因为您只为价格槽提供三个值,而您有四个项目,而prolog将检查A > 2

对不起,在我的另一个回答中,我没有找到海报试图实现的目标,我认为这比完整的reedit要好。 (光荣的SO mods告诉我,如果我错了)

相关问题