Jakob Auer
Jakob Auer

Reputation: 131

Prolog returns variable name instead of value

I have a method which checks where to place the next brick while building a tower.

freePosition(POS, LEVEL) :- levelWithSpace(LEVEL),
                            freePositionOnLevel(POS, LEVEL).

This method checks for a level with space and asks for the free positions on this level. In my case it should return two results:

However it returns the following output:

?- freePosition(POS, LEVEL).
POS = 1,
LEVEL = 2 ;
POS = LEVEL, LEVEL = 2 ;

How can I change the behavior that it returns POS=2 instead of POS=LEVEL?

Upvotes: 1

Views: 265

Answers (1)

repeat
repeat

Reputation: 18726

You can use the builtin format/2 to emit a formatted string:

?- freePosition(POS, LEVEL),
   format('Pos = ~d, Level = ~d~n', [POS,LEVEL]).

This will make it easy to get a uniform output style no matter what Prolog implementation you use.

Note that the concrete style of answers given by the Prolog interpreter top-level differs quite a bit between different Prolog implementations.

Upvotes: 3

Related Questions