操作员在表达后预期

时间:2014-03-20 23:29:40

标签: prolog

我有那些事实

   relationship(nikos,anna,husband).
    type_of_service(nikos,hospital).
    insurance_cover(nikos,partial).
    type_of_insurance(nikos,family).
    refund_request(nikos,5000).

我写了下面的

refund(Insured , insuranceCover, hospital, requestedRefund, Poso):-
hospital_refund(Insured, insuranceCover, requestedRefund, Poso).

hospital_refund(Insured, partial, requestedRefund, Poso):-
requestedRefund <= 5000,Poso = requestedRefund. %% <--- Error

hospital_refund(Insured, full, requestedRefund, Poso):-
Poso = requestedRefund.

apozemiose(Insured, Poso):-
refund(Insured, insurance_cover(Insured ,X),
    type_of_service(Insured),
    refund_request(Insured , X)
).

如果某人有医院保险,则需要退款,金额取决于保险的类型,如果是部分或全部保险。

我想要的是能够询问一个人可以获得多少退款。 具体来说是?- apozemiose (nikos, Poso).

我在表达式之后得到一个错误的运算符,我确定存在某种语法错误,在哪里进行比较。

2 个答案:

答案 0 :(得分:3)

您不在Prolog中使用<=。正确的语法是=<

答案 1 :(得分:1)

除了语法错误之外,您还需要更改最后一个子句

apozemiose(Insured, Poso):-
  insurance_cover(Insured, I),
  type_of_service(Insured, T),
  refund_request(Insured, R),
  refund(Insured, I,T,R, Poso).

编辑这里有一组修正,主要是引入变量,你使用 atoms ,即文字常量

refund(Insured, InsuranceCover, hospital, RequestedRefund, Poso):-
    hospital_refund(Insured, InsuranceCover, RequestedRefund, Poso).

hospital_refund(_Insured, partial, RequestedRefund, Poso):-
    RequestedRefund =< 5000, Poso = RequestedRefund.
hospital_refund(_Insured, full, RequestedRefund, Poso):-
    Poso = RequestedRefund.

作为一种风格选择,我会避免在规则体中进行分配,即:

hospital_refund(_Insured, partial, RequestedRefund, RequestedRefund):-
    RequestedRefund =< 5000.
hospital_refund(_Insured, full, RequestedRefund, RequestedRefund).

但这当然取决于规则如此简单......

评论后

完整代码

relationship(nikos,anna,husband).
type_of_service(nikos,hospital).
insurance_cover(nikos,partial).
type_of_insurance(nikos,family).
refund_request(nikos,5000).

refund(Insured, InsuranceCover, hospital, RequestedRefund, Poso):-
    hospital_refund(Insured, InsuranceCover, RequestedRefund, Poso).

hospital_refund(_Insured, partial, RequestedRefund, Poso):-
    RequestedRefund =< 5000, Poso = RequestedRefund.
hospital_refund(_Insured, full, RequestedRefund, Poso):-
    Poso = RequestedRefund.

apozemiose(Insured, Poso):-
    insurance_cover(Insured, I),
    type_of_service(Insured, T),
    refund_request(Insured, R),
    refund(Insured, I,T,R, Poso).

试验:

?- apozemiose(nikos, Poso).
Poso = 5000 ;
false.

HTH