Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a list with anything ([_]) and anything (_)

Tags:

list

prolog

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.

like image 240
Trufa Avatar asked Dec 18 '11 22:12

Trufa


People also ask

What is the meaning of anything?

Anything "Anything" (one word) is a pronoun that means a thing of any kind (i.e., a thing, no matter what it is).

What is the difference between 'any' and 'any one thing'?

("Any thing" (two words) can often be substituted with "any one thing.") You may not take pictures of people, but you can take a picture of any thing. In the term "any thing," "any" is an indefinite adjective modifying the noun "thing."

What is the difference between ‘everything’ and ‘all things’?

They closely resemble each other in spelling and sound yet they are two different words that have different meanings and uses. ‘Everything’ refers to all things that exist. All objects, seen or unseen, are part of everything.

What part of speech is the word anything?

Similar to the word something, the word anything is generally used as a pronoun. My definition of the word anything is whatever; I don’t care what it is. The word anything is generally used in negative sentences: Here are some examples to help clear this up: I didn’t get anything for my birthday.


2 Answers

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 : ]

like image 53
m09 Avatar answered Nov 09 '22 14:11

m09


_ 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 [_]

like image 36
Thanos Tintinidis Avatar answered Nov 09 '22 14:11

Thanos Tintinidis