1

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.

user1854914
  • 137
  • 8
  • 2
    Could you add some examples to demonstrate how you'd use the predicate and what you'd expect the answers to be in each base. Is the location `L` actually a list of things the location contains? What does the number `N` represent? The first clause, `remove(C, L1, L2)` is rather meaningless, and the others don't make much sense without more context. For instance, `N` can't be a number in the second clause as you're attempting to unify with the tail end of a list. The third clause just says that both lists have to be of equal length and consist solely of the value 'C'. – Keith Gaughan Nov 27 '12 at 22:52
  • Ack! I meant 'case', not 'base'. Also, what's the difference between `L1` and `L2`? Are these two different locations? – Keith Gaughan Nov 27 '12 at 22:58
  • An example would be: remove(b,[(a,3), (b, 7), (c,4)], L2). and the result should be L2 = [(a,3), (c,4)]. – user1854914 Nov 28 '12 at 14:25

1 Answers1

1

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)]
Keith Gaughan
  • 21,367
  • 3
  • 32
  • 30
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • out of curiousity is there a way where you can just enter ` remove(3, [(1,a),(2,b),(3,c),(4,d)], L).` and then just get the answer ` L2 = [ (1, a), (2, b), (4, d)].` – user1854914 Nov 28 '12 at 14:57
  • the correction by @Keith Gaughan it's working, but remove/3 it's losing generality (it's less useful with the specialized schema). I would prefer adding a refinement that accept the specialization. I.e. with the original definition, add `remove(C,L,R) :- remove((C,_),L,R), !.`. – CapelliC Nov 28 '12 at 15:22