У меня есть следующий код:
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).
Я пытаюсь запустить следующие тестовые случаи, чтобы проверить мой «конвертировать» предикат, но в третьем тесте он застревает на бесконечном l oop.
test(convert) :- convert(foot, 2, inch, 24.0).
test(convert) :- convert(foot, 6, yard, 2.0).
test(convert) :- convert(chain, 2, foot, 132.0).
Как бы я go остановил возврат, чтобы этот последний тест не работал бесконечно?
Спасибо.