Search code examples
listprologelementwin-prolog

Prolog. lists predicate to remove an element


Im trying to remove 'b' which is '(b, 10)'. The code i have is:

    remove(C, L1, L2).

    remove(C, [C|N], N).

    remove(C, [C|L1], [C|L2]) :- remove(C, L1, L2).

'C' represents a chest. 'L' represents a location. 'N' represents a number.

Im not sure if im heading in the right direction or if im just missing something little.


Solution

  • you need some correction:

    remove(_, [], []).  % drop this if must fail when no element found
    remove(C, [(C,_)|N], N) :- !.
    remove(C, [A|L1], [A|L2]) :-
        remove(C, L1, L2).
    

    you must pass a matching argument

    ?- remove(c, [(a,1),(b,2),(c,3),(d,4)], L).
    L = [(a,1),(b,2),(d,4)]