I've just tried to implement absolute function in Prolog and I've got some strange behavior. My code was:
absval(X, RESULT) :- X >= 0, RESULT is X.
absval(X, RESULT) :- X < 0, RESULT is -X.
And when I try in SWI-Prolog absval(-2,X).
I get
X = 2
yes
as expected. But otherwise when I invoke absval(2,X)
, I get X = 2 ?
and I should insert another input. After pressing enter I get also yes
.
What does mean the second one result? What's wrong with my solution?