문제

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

올바른 솔루션이 없습니다

다른 팁

Use retractall to clean up the state when you start.

sample(X):-
    retractall(listD(_)),
    assert(listD(X)),
    listD(Y),
    write(Y).
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top