How to create a global variable in the prolog

I have a list that I create as follows:

tab([(top,left),(top,middle),(top,right),(center,left),(center,middle),
     (center,right),(bottom,left),(bottom,middle),(bottom,right)]).

I want to create a global variable AllPosition, which is a tab. So I did the following:

tab(AllPos).

Is it correct?

Then I have to keep track of the problem: I have a function that gets one of a pair in a bookmark. I want to delete this. So I did this:

place(Line, Column, Tab) :-
AllPos \== [_,_] /*while AllPos isn't empty - not sur if this is done this way*/ -> (member((Line,Column), AllPos) -> (erase(AllPos, (Line,Column), AllPos)).

where it erase(List, Element, NewList)erases the Element from the list and creates a new NewList, equal to List, but without the element. Both functions memberalso erasework.

... , AllPos . , , , ( ), . ? AllPos ?

+5
3

SWI-Prolog : b_setval(name, value) b_getval(name, value). , , , , : nb_setval(name, value) nb_getval(name, value).

, , , , , :

recursive(100).
recursive(X) :- add, Y is X + 1, recursive(Y).

add :- nb_getval(counter, C), CNew is C + 1, nb_setval(counter, CNew).

testRecursion
:-
    % Set counter to zero
    nb_setval(counter, 0),

    % Run some code with 'add'
    recursive(0), !,

    % Print the results
    nb_getval(counter, CounterValue),
    write('Steps: '), writeln(CounterValue).

, Prolog, Prolog .

+4

Ian:

assert/retract, . (, swi-prolog lib)

, , , ; "" myvar(42)., :

foo:-
   myvar(Var),
   do_something(Var).

, , , - .

+1

: , . , - . , . , Prolog, . , AllTabs place/4 , Tabs. , assert retract.

:

tab(AllPos).

this declares a predicate with an unrelated variable in the head. This is more or less pointless (you can read it, "this is what tabis true for something, but we do not have information that this is true").

AllPos \== [_,_]

This usage AllPosis in a different area tab/1, therefore, in addition to sharing the same sequence of characters in the variable name, the two uses AllPosare not related at all.

0
source

All Articles