0

I am currently trying to find all elements of Lists L1 and L2 that are different from each other and append them into List L.

So far, I tried the following:

 difference(L1, L2, L) :-
     findall(H, (member(H,L1),\+member(H, L2)), L).

However, I only get an empty list when I query the following:

 |?- difference([1,2,3],[1,2,3,4,5,6], L).

I also tried querying the following:

 |?- difference([1,2,3,4,5,6],[1,2,3], L).

Interestingly, for this one I get L = [4,5,6]. How can I rewrite my fact to for it to work also with the first query?

qwerty
  • 321
  • 1
  • 5
  • 13

1 Answers1

0

Your definition reads "all elements of L1 that are not in L2".
What you want is "all elements of L1 or L2 that are not in L1 and L2"

difference(L1, L2, L) :-
     findall(H, ((member(H,L1);member(H,L2)),\+(member(H,L1),member(H,L2))), L).
Cephalopod
  • 14,632
  • 7
  • 51
  • 70