I want to write change_state(+L1, +L2, -L3).
L1 contains states e.g: [milled, burned] L2 contains predicates e.g: [del(milled), add(shifted)] And L3 is the return list.
Example:
L1 = [milled, burned]
L2 = [del(burned), add(shifted)]
L3 = [milled, shifted]
So L1 is my start list and depending on the predicates in L2 I change the state of L1.
This is my solution with recursion:
change_state([], [], []).
change_state(X, [], []).
change_state(State, [H|T], NewState) :-
functor(H, N, 1),
arg(1, H, A),
( N = del
-> remove_from_list(A, State, NewState)
; arg(1, H, A),
add_to_list(A, State, NewState) ),
print(NewState),
change_state(NewState, T, X),
print(NewState).
I also put some prints into the function for debugging.
Now the problem:
If I call the function with
change_state([milled, burned], [del(burned), add(lol), add(hi)], L).
I get as an result
L = [milled].
So only the first predicate in L2 worked.
But the prints show me:
[milled][lol,milled][hi,lol,milled][hi,lol,milled][lol,milled][milled].
So it actually worked, but because of the recursion it goes back to the first state.
Any idea how to fix this?
change_state(State, [H|T], NewState) :- ...shouldn't that bechange_state(State, [H|T], X) :- ...considering your almost-tail-recursive callchange_state(NewState, T, X)?change_state(X, [], []).must be replaced withchange_state(X, [], X).