I have a small problem when we are talking about anonymous variables. For example when we make this:
?- [_,2]=[X|Y].
Y=[2].
but my question is about the variable X. Does it have the '_'?
No, X
does not "have the _
". It is bound to an anonymous variable, which is never bound to anything else. This binding of X
to an anonymous variable does not create any additional limitations on X
- for all practical purposes, it remains unbound.
The _
variable has been introduced to let Prolog coders express in code that they do not care about a value in a particular position. One could emulate this behavior by using variables that look like UNUSED1
, UNUSED2
, UNUSED3
and so on instead of the _
, and ignoring Prolog warnings about singleton variables:
[UNUSED123,2]=[X|Y].
Using the underscore _
is like telling Prolog that you know that the unused variable is singleton, and that it is indeed your intention.