我看到了这个XKCD 漫画中提到的问题的 ECLiPSe 解决方案。我试图将其转换为纯 Prolog。
go:-
Total = 1505,
Prices = [215, 275, 335, 355, 420, 580],
length(Prices, N),
length(Amounts, N),
totalCost(Prices, Amounts, 0, Total),
writeln(Total).
totalCost([], [], TotalSoFar, TotalSoFar).
totalCost([P|Prices], [A|Amounts], TotalSoFar, EndTotal):-
between(0, 10, A),
Cost is P*A,
TotalSoFar1 is TotalSoFar + Cost,
totalCost(Prices, Amounts, TotalSoFar1, EndTotal).
我不认为这是人们能想到的最好/最具声明性的解决方案。有没有人有任何改进的建议?提前致谢!