0
votes

I want to apply a list of effects in a current state in other words, have a list of effects generated by an action if the current state has a condition that corresponds to the denial of a effect that condition will be removed.

If i have the current state:

[clear(b),on(b,a),on(a,mesa),clear(d),on(d,c),on(c,desk)]

And the list of effects :

[-clear(d), -on(d.c), on(c,d)]

The result would be :

[clear(b),on(b,a),on(a,mesa), on(c,d), on(c,desk)]

This is what i got right now, any help would be appreciated!

applyEffects(currentState,Effects,result) 
insert(Element, List,result) 
remove(Element,List, result)

    applyEffects([],L,L).
    applyEffects(L,[],L).
    applyEffects([X|XTail], [-X|YTail], A) :-
       insert(X, A, B),
       applyEffects([X|XTail],YTail, B).

    insert(E, L1, [E|L1]).

    remove(X, [X|L1], L1).
    remove(X, [Y|L1], A):- remove(X,L1,[L1|Y]).
1

1 Answers

1
votes

Your insert and remove should both be select (which is often built-in).

You might want to distinguish, if you have a "negative" argument or not:

applyEffects(L,[],L).
applyEffects(L,[-X|R],A):-
    !,
    applyEffects(L,R,Y),
    select(X,Y,A).
applyEffects(L,[X|R],[X|Y]):-
    applyEffects(L,R,Y).

The cut used in the second clause is a red cut, to make it green, add a \+ X = - _ line to the third clause.

if you want to allow non-existing negatives, change the second clause to this:

applyEffects(L,[-X|R],A):-
    !,
    applyEffects(L,R,Y),
    (select(X,Y,A),!;
     Y = A).

Now applyEffects([],[-on(something)],X) doesn't fail.