SWI-Prolog compare value from list with constant, but getting error character expected

StackOverflow https://stackoverflow.com/questions/23339724

  •  10-07-2023
  •  | 
  •  

문제

I want to write a SWI-Prolog program, which tests if the result is lower than 4.0.

Therefore I pass a list like this [Name, Subject, Result], the subject and the name for the return value.

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].

Now if I execute with the following parameters:

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

There is an error occuring:

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

I just can't find a way to fix it.

도움이 되었습니까?

해결책

Replace third(Tail,Result) with third(Tail,[Result]):

test([Head|Tail], Subject, Z):-
    second(Tail, Subject2),
    Subject2==Subject,
    third(Tail,[Result]),
    Result =< 4.0,
    Z=[Head|Tail].
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top