'Prolog: Can't increase value of variable inside complex term
So I'm working on a prolog problem where a state is defined in a complex term, When I try to increase the value of x inside this complex term nothing happens for example
CurrentState(left, x, y).
test = CurrentState(left, x,y),
newX = x + 1,
write(newX).
Is there a workaround for this?
Solution 1:[1]
Facts and predicates need to start with a lowercase letter.
Variables need to start with an uppercase letter.
=
is not assignment, and Prolog predicates aren't function calls, and you never use test
anyway.
Even then, what do you want x + 1
to do when you haven't given any value for x? It makes no more sense than left + 1
written like that.
It would all be more like:
currentState(left, 5, 2). % fact
test(NewX) :-
currentState(Direction, X, Y), % search for the fact, fill the variables.
NewX is X + 1, % new variable for new value.
write(NewX).
Then
?- test(NewX).
6
NewX = 6
After that you still don't want to be storing the state as a fact in the Prolog database and removing/asserting it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TessellatingHeckler |