Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归遍历列表时出现Prolog奇怪错误_Prolog - Fatal编程技术网

递归遍历列表时出现Prolog奇怪错误

递归遍历列表时出现Prolog奇怪错误,prolog,Prolog,我需要做一个谓词,检查成分的总成本是否等于。 以下是我的事实: 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(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,它也打印no

我的语法有问题吗?

问题是imho

total_cost(X,K) :- ingredients(X,Y),helpt(Y,B),K==B. 
如果你把它改成

total_cost(X,B) :- ingredients(X,Y),helpt(Y,B).
我得到了正确的结果


==/2
检查是否相等,同时您希望实现统一

为什么相等不起作用,我理解你的为什么起作用,但为什么不比较第二个参数helpt和total_cost work,因为
X==2
计算结果为假,因为
X
是一个变量,
2
是一个原子<但是,code>X=2将
X
与两个统一起来,并且同样有效。