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