[英]Member predicate issues, change for a dollar
I'm a complete noob in Prolog. 我是Prolog中的菜鸟。 I'm working on an assignment where I create a change counter that can take a total
S
(in cents), 0 <= S <= 100. So I need to show the number of half dollars, quarters, dimes, nickels. 我正在做一个作业,我在其中创建一个可以加总
S
(以美分为单位)的零钱计数器,0 <= S <=100。因此,我需要显示半美元,四分之一,一角硬币,五分之一的数字。
Here's my code: 这是我的代码:
change(S,H,Q,D,N,P) :-
member(H,[0,1,2]), /* Half-dollars */
member(Q,[0,1,2,3,4]), /* quarters */
member(D,[0,1,2,3,4,5,6,7,8,9,10]) , /* dimes */
member(N,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),
/* ^^ nickels ^^ */
S is 50*H + 25*Q +10*D + 5*N,
S =< 100,
P is 100-S.
so my issue is when i try and calculate something like ?- change(87,0,3,D,1,P).
所以我的问题是当我尝试计算
?- change(87,0,3,D,1,P).
where the amount i am inputing is 87 cents and i need 3 quarters and 1 nickel..... i get an error stating uncaught exception: error(existence_error(procedure,change/6),top_level/0)
我输入的金额是87美分,我需要3个季度和1个镍.....我收到一条错误消息,指出
uncaught exception: error(existence_error(procedure,change/6),top_level/0)
With my last 3 lines of code I thought I was handling the amount given correctly. 在我的最后三行代码中,我认为我正在正确地处理给出的数量。 Do I need to make an additional rule regarding the amount given?
我是否需要针对所给的金额制定其他规则?
You miss the cents! 你想念仙! And I suggest to use between/3 to express the ranges:
我建议使用between / 3来表示范围:
...,
between(0,100,Cents),
...,
...,
S is 50*H + 25*Q +10*D + 5*N + Cents,
...,
test: 测试:
?- change(87,H,Q,D,N,Cents,P).
H = Q, Q = D, D = N, N = 0,
Cents = 87,
P = 13 ;
H = Q, Q = D, D = 0,
N = 1,
Cents = 82,
P = 13 ;
WRT error(existence_error...
I think you need to compile your script... WRT
error(existence_error...
我认为您需要编译脚本...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.