如何在这个Prolog谓词中获得句号?
我有以下代码:如何在这个Prolog谓词中获得句号?,prolog,failure-slice,Prolog,Failure Slice,我有以下代码: factor_in_inches(Unit, Scale) :- recursiveScale(Unit, Scale, inch). %Part3 scale_factor(Unit1, Unit2, Factor) :- recursiveScale(Unit2, Factor, Unit1). %Part 1 - wrote alternate scale function so I could keep part instead of deleting for par
factor_in_inches(Unit, Scale) :- recursiveScale(Unit, Scale, inch).
%Part3
scale_factor(Unit1, Unit2, Factor) :- recursiveScale(Unit2, Factor, Unit1).
%Part 1 - wrote alternate scale function so I could keep part instead of deleting for part 2
findScale(Unit1, Scale, Unit2) :-
scale(Unit1, Scale, Unit2);
scale(Unit2, Scale1, Unit1),
Scale is float(1/Scale1).
%need to flip scales because not all units have direct conversions to eachother
%have to find reciprocal conversion and then flip it
%Part2
%have to use findScale instead of regular scale because otherwise
%the recursive fucntion did not work for direct conversions to inches
%had to use findScale to get base conversions for inches
recursiveScale(Big, Scale, Smallest) :-
findScale(Big, Scale, Smallest);
recursiveScale(Smaller, MoreScale, Smallest),
findScale(Big, ScaleAgain, Smaller),
Scale is float(ScaleAgain * MoreScale).
%Part 4
convert(Unit1, Quantity1, Unit2, Quantity2) :-
factor_in_inches(Unit1, Factor1),
factor_in_inches(Unit2, Factor2),
Factor1 is round((Factor2 * Quantity2)/ Quantity1).
这是基于以下事实:
scale(foot, 12, inch).
scale(yard, 3, foot).
scale(rod, 198, inch).
scale(chain, 22, yard).
scale(furlong, 40, rod).
scale(mile, 8, furlong).
scale(league, 3, mile).
我试图运行以下测试用例来测试我的convert谓词,但在第三个测试中,它被卡在一个无限循环中
test(convert) :- convert(foot, 2, inch, 24.0).
test(convert) :- convert(foot, 6, yard, 2.0).
test(convert) :- convert(chain, 2, foot, 132.0).
我如何停止回溯,使最后的测试不会无限期地运行
谢谢。以下是您的程序的责任部分,说明您的任何查询为何没有终止。这不仅仅是最后一个!要看到这一点,可以询问Query,而不是false recursiveScale(Big, Scale, Smallest) :- ( false,
要解决此问题,必须修改可见部分中的某些内容。看来你误解了析取。总是放;在前面,或者简单地看一下清单/1的建议。有关更多信息,请参阅标签。以下是程序中负责的部分,说明您的任何查询为何没有终止。这不仅仅是最后一个!要看到这一点,可以询问Query,而不是false recursiveScale(Big, Scale, Smallest) :- ( false,
要解决此问题,必须修改可见部分中的某些内容。看来你误解了析取。总是放;在前面,或者简单地看一下清单/1的建议。有关更多信息,请参阅标签。此问题中没有任何原因可以无限期运行 您只需将某些原始面额的值缩小到英寸。 然后将英寸值放大到目标面额 findScale/3谓词看起来已经可疑了。您希望缩小比例,如中所示 findScaleBig,Scale,Minimer。但是,第三行尝试查找scaleUnit2、Scale1、Unit1,其中Unit2的比例大于Unit1。数据库中不存在这样的事实
findScale(Unit1, Scale, Unit2) :-
scale(Unit1, Scale, Unit2);
scale(Unit2, Scale1, Unit1),
Scale is float(1/Scale1).
你想要的是一个谓词
conversionFactor(U1,U2,F) :- larger(U1,U2),
stepDown(U1,Ux,Fx),
conversionFactor(Ux,U2,Fxx),
F is Fx*Fxx.
conversionFactor(U1,U2,F) :- larger(U2,U1),
conversionFactor(U2,U1,Fx),
F is 1.0/Fx.
conversionFactor(U1,U2,F) :- \+ larger(U1,U2), \+ larger(U2,U1), % incomparable
conversionFactor(U1,inch,Fa),
conversionFactor(inch,U2,Fb),
F is Fa*Fb.
conversionFactor(U,U,1.0).
没有回溯,因为真的没有选择。
这只是一个递归调用 在这个问题中没有理由让任何东西无限期运行 您只需将某些原始面额的值缩小到英寸。 然后将英寸值放大到目标面额 findScale/3谓词看起来已经可疑了。您希望缩小比例,如中所示 findScaleBig,Scale,Minimer。但是,第三行尝试查找scaleUnit2、Scale1、Unit1,其中Unit2的比例大于Unit1。数据库中不存在这样的事实
findScale(Unit1, Scale, Unit2) :-
scale(Unit1, Scale, Unit2);
scale(Unit2, Scale1, Unit1),
Scale is float(1/Scale1).
你想要的是一个谓词
conversionFactor(U1,U2,F) :- larger(U1,U2),
stepDown(U1,Ux,Fx),
conversionFactor(Ux,U2,Fxx),
F is Fx*Fxx.
conversionFactor(U1,U2,F) :- larger(U2,U1),
conversionFactor(U2,U1,Fx),
F is 1.0/Fx.
conversionFactor(U1,U2,F) :- \+ larger(U1,U2), \+ larger(U2,U1), % incomparable
conversionFactor(U1,inch,Fa),
conversionFactor(inch,U2,Fb),
F is Fa*Fb.
conversionFactor(U,U,1.0).
没有回溯,因为真的没有选择。
这只是一个递归调用 似乎可以简单得多: 系数英寸,1:-!。 系数单位为英寸,比例:- scaleUnit,Scale1,向下, 因子单位为英寸,比例2, 比例为1*2。 编辑 削减是为了提高效率,将其删除,即第一条成为 系数在英寸中,1。 您可以列举所有因素: ?-美国仁切斯的系数。 U=英寸, S=1; U=英尺, S=12; U=码, S=36; U=杆, S=198; U=链, S=792; U=弗隆, S=7920; U=英里, S=63360; U=联盟, S=190080; 错误的
似乎可以简单得多: 系数英寸,1:-!。 系数单位为英寸,比例:- scaleUnit,Scale1,向下, 因子单位为英寸,比例2, 比例为1*2。 编辑 削减是为了提高效率,将其删除,即第一条成为 系数在英寸中,1。 您可以列举所有因素: ?-美国仁切斯的系数。 U=英寸, S=1; U=英尺, S=12; U=码, S=36; U=杆, S=198; U=链, S=792; U=弗隆, S=7920; U=英里, S=63360; U=联盟, S=190080; 错误的