0

I have a problem that requires me to add elements to a list that are spread across various predicates. Rather than doing via argument based lists I've opted to use a dynamic list predicate. I simple example can be seen below. When I initially used it it worked fine but now if I use any argument as X it keeps retrieving previous argument data from the list even after closing the program and recompilation. Does anybody know what's wrong with it?

//list declarations
:- dynamic listD/1.
listD([]).

//pushes X onto the list then retrieves the entire list for verification
sample(X):-
    assert(listD(X)),
    listD(Y),
    write(Y).


Example usage

sample([adam]).
//prints adam fine
sample([fred]).
//prints adam again
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
user2211776
  • 239
  • 1
  • 2
  • 11

1 Answers1

0

Use retractall to clean up the state when you start.

sample(X):-
    retractall(listD(_)),
    assert(listD(X)),
    listD(Y),
    write(Y).
Will Ness
  • 70,110
  • 9
  • 98
  • 181