Very new to Prolog. Goal is to check if dynamic predicate contains a specific value. Doing it in the following way but does not work:
:- dynamic storage/1.
not(member(Z,storage)), // checking if Z is already a member of storage - this does not work.
assert(storage(Z)). // asserting Z if above is true
Could you please explain how this should be done?
Like this?
:- dynamic storage/1.
append_to_database_but_only_once(Z) :-
must_be(nonvar,Z), % Z must be bound variable (an actual value)
\+ storage(Z), % If "storage(Z)" is true already, fail!
assertz(storage(Z)). % ...otherwise append to the database
And so:
?- append_to_database_but_only_once(foo).
true.
?- storage(X).
X = foo.
?- append_to_database_but_only_once(foo).
false.
Note that you don't need to query any datastructure or list with member/2
. With assertz/1
, you are changing the Prolog database itself! So afterwards you just need to perform the corresponding query, storage(Z)
.