I was trying to accomplish the following, if I have two lists, L1 and L2, I wanted that the result (R) to be the "subtraction" of L2 from L1.
Example:
L1 = [1,2,3]
L2 = [2,3,4,5]
R = [1]
I WAS able to accomplish this but I can't tell what is the difference between _
and [_]
.
If I do this:
diferencia([],_,[]).
diferencia([X|Tail],L2,R):-
member(X,L2),
diferencia(Tail,L2,R).
diferencia([X|Tail],L2,[X|R]):-
not(member(X,L2)),
diferencia(Tail,L2,R).
It works, if I do this, it gives me false:
diferencia([],[_],[]).
diferencia([X|Tail],L2,R):-
member(X,L2),
diferencia(Tail,L2,R).
diferencia([X|Tail],L2,[X|R]):-
not(member(X,L2)),
diferencia(Tail,L2,R).
I would assume a list containing anything [_]
should work since L2 will always be a list.
Actually, _
matches one variable and one variable only. Here, you'd want it to match 2, 3, 4, 5
(the four variables). It can't. It can only match [2, 3, 4, 5]
(the list). You'd have to write [_|_]
so that the head and the tail are matched ([2|[3, 4, 5]]
)
Or [_, _, _, _, _, _, ...]
with the number of _
being the exact number of items in your list so that every single element is properly matched with an anonymous variable.
The basic thing to remember is that _
is just a normal variable. If you have troubles remembering it, just explicit names, like _Head
or _Accumulator
, so that you do realize when you write your code that the thing you manipulate is actually a variable, only you do not care about it (variable starting with a _
won't produce a singleton variable warning, in swi-pl at least, so they're usable instead of _
for a better overall clarity).
Edit : one other way to say it is that in your title, you think _
is anything. But anything can be nothing, and anything can be many things. _
can only be one thing. That's why it doesn't work : ]
_
is anything... foo, [1,2], bar(42,foo[2,3,7]) etc
[_]
is a list that has exactly one element that could be anything
in your example if L2 has more than one elements (or is the empty list) then it won't match with [_]