Reputation: 365
I am currently learning to program in Prolog, and am working on an assignment which involves a world of size n and has 3 blocks that are picked up and moved based on where the user wants to move it. I have a basic bare bone structure of the program, however, the issue I am having is getting the user's input, storing the integer of the world size and using variable throughout the program. I managed to get the user input, but when I use it elsewhere in the code, the value is not what it should be.
For example. If I enter 14 for n, and then in the position
clause I output the size, I get World Size: _6324
instead of 14. This seems to be causing other issues in my code, and I cannot figure out how to solve this issue. What am I doing wrong, and how would I solve this? Also, what does _6324
mean?
Here is the initial code which asks for the user input and outputs the current set of data, including the world size:
%Setting world: world of lenght n, block size of 3
:-assert(at(a,0)).
:-assert(at(b,2)).
:-assert(at(c,5)).
%Gets the initial size of the world from the user
start :- write('What the world length?: '), read(Length),
assert(world_length(Length)), nl, position.
%outputs the initial positions of each block
position:- write("Initial Positions: "),nl,
write('World Size: '), write(Length),nl,
at(a,X), write(a), write(' is at position '), write(X),nl,
at(b,Y), write(b), write(' is at position '), write(Y),nl,
at(c,Z), write(c), write(' is at position '), write(Z),nl,nl,
write('Instructions: To move a block, simply type in move(block, position).'), nl.
Upvotes: 2
Views: 966
Reputation: 71070
The logical variable Length
inside your position/0
predicate has no knowledge of what the logical variable Length
inside your start/0
predicate was.
In Prolog, there is no dynamically nested scopes.
You save this value in your dynamic database by calling assert(world_length(Length))
inside the start/0
predicate, and so you can query it inside the position/0
, with
write('World Size: '), world_length(Length), write(Length), nl, ...
Without the querying, what you have is a non-initialized, not-yet-set logical variable Length
, and _6432
(or _G12345
) is its name, that is printed by write
in such cases. Meaning, "no value yet".
Upvotes: 2