Prolog奇怪的错误递归迭代列表

时间:2016-03-12 00:34:02

标签: prolog

我需要制作一个谓词来检查成分的总成本是否使X等于Y. 以下是我的事实:

cost(carne_asada,3).
cost(lengua,2).
cost(birria,2).
cost(carnitas,2).
cost(adobado,2).
cost(al_pastor,2).
cost(guacamole,1).
cost(rice,1).
cost(beans,1).
cost(salsa,1).
cost(cheese,1).
cost(sour_cream,1).
cost(taco,1).
cost(tortilla,1).
cost(sopa,1).


ingredients(carnitas_taco, [taco,carnitas, salsa, guacamole]).
ingredients(birria_taco, [taco,birria, salsa, guacamole]).
ingredients(al_pastor_taco, [taco,al_pastor, salsa, guacamole, cheese]).
ingredients(guacamole_taco, [taco,guacamole, salsa,sour_cream]).
ingredients(al_pastor_burrito, [tortilla,al_pastor, salsa]).
ingredients(carne_asada_burrito, [tortilla,carne_asada, guacamole, rice, beans]).
ingredients(adobado_burrito, [tortilla,adobado, guacamole, rice, beans]).
ingredients(carnitas_sopa, [sopa,carnitas, guacamole, salsa,sour_cream]).
ingredients(lengua_sopa, [sopa,lengua,beans,sour_cream]).
ingredients(combo_plate, [al_pastor, carne_asada,rice, tortilla, beans, salsa, guacamole, cheese]).
ingredients(adobado_plate, [adobado, guacamole, rice, tortilla, beans, cheese]).

这是我的代码:

helpt([],0).
helpt([H|T],V):-helpt(T,VT),cost(H,Y),V is VT+Y.
total_cost(X,K) :- ingredients(X,Y),helpt(Y,B),K==B. 

当我做这两件事时,这是正常的:

?- total_cost(carnitas_taco,3).
false

?- total_cost(X,5).
X = carnitas_taco ;
X = birria_taco ;
X = lengua_sopa ;
false

但是当我这样做时:

?- total_cost(carnitas_taco,X).

即使它应该是5,也不打印。

我的语法有问题吗?

1 个答案:

答案 0 :(得分:1)

问题是imho total_cost(X,K): - 成分(X,Y),helpt(Y,B),K == B. 如果你改成它 total_cost(X,B): - 成分(X,Y),helpt(Y,B)。 我得到了正确的结果。 == / 2检查是否相等,而你想要统一。