SWI-Prolog将列表中的值与常量进行比较,但预期会出现错误字符

时间:2014-04-28 11:19:22

标签: prolog

我想写一个SWI-Prolog程序,它测试结果是否低于4.0。

因此,我传递了一个像[Name, Subject, Result]这样的列表,主题和返回值的名称。

third([Head|Tail], Z) :-
    Z = Tail.

second([Head|Tail], Z) :-
    Z = Head.

test([Head|Tail], Subject, Z) :-
    second(Tail, Subject2),
    Subject2==Subject,
    third(Tail,Result),
    Result=<4.0,
    Z=[Head|Tail].

现在,如果我使用以下参数执行:

test([smith, mu, 2.4], mu, R).

发生错误:

ERROR: =</2: Type error: `character' expected, found `2.4'

我找不到解决方法。

1 个答案:

答案 0 :(得分:1)

third(Tail,Result)替换为third(Tail,[Result])

test([Head|Tail], Subject, Z):-
    second(Tail, Subject2),
    Subject2==Subject,
    third(Tail,[Result]),
    Result =< 4.0,
    Z=[Head|Tail].