Question

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

No correct solution

OTHER TIPS

Use retractall to clean up the state when you start.

sample(X):-
    retractall(listD(_)),
    assert(listD(X)),
    listD(Y),
    write(Y).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top