如何在此Prolog谓词中获得句号?

问题描述 投票:0回答:1

我有以下代码:

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).

我正在尝试运行以下测试用例来测试我的“转换”谓词,但是在第三个测试中,它陷入了无限循环。

test(convert) :- convert(foot, 2,  inch,  24.0).
  test(convert) :- convert(foot, 6,  yard,   2.0).
  test(convert) :- convert(chain, 2, foot, 132.0).

我将如何停止回溯,以便最后一次测试不会无限运行?

谢谢。

prolog
1个回答
0
投票

这里是程序的负责部分,为什么您的任何查询都不会终止。它不仅是最后一个!要查看此内容,请改为询问Query, false

factor_in_inches(单位,比例):-recursiveScale(Unit,Scale,inch),false。recursiveScale(大,小,小):-(falsefindScale(Big,Scale,Smallest); recursiveScale(Smaller,MoreScale,Smallest),falsefindScale(Big,ScaleAgain,Smaller)刻度是浮点的(ScaleAgain * MoreScale))。转换(单位1,数量1,单位2,数量2):-factor_in_inches(Unit1,Factor1),falsefactor_in_inches(Unit2,Factor2)因子1是圆形的((因子2 *数量2)/数量1)。?-convert(链,2,脚,132.0)。

此片段称为与您的程序密切相关。因为,如果此片段循环,那么您的程序也将循环。我仅通过将目标false添加到您的程序中就获得了此片段。有趣的是convert/4的所有参数都被有效地忽略了。因此,all查询将循环。甚至那些您认为它们会终止的地方。

要解决此问题,您必须修改可见部分的内容。您似乎误解了析取。始终将;放在前面,或者只是看listing/1的建议。有关更多信息,请参见标签

© www.soinside.com 2019 - 2024. All rights reserved.