1

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 '_'?

false
  • 10,264
  • 13
  • 101
  • 209
Miguel
  • 89
  • 2
  • 9

1 Answers1

2

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • My original problem was to unify this two lists [2] and [1,_,3] giving an answer of [1,2,3]. I though of using the operator `|` to compare if the member we were spliting would unify through the predicate `member` (pre-built) but since I can't identify the `_` i don't know how to do it. My code is: unifica([],[],_). unifica(Lista1, [Cabeca|Resto], Lista2) :-member(Lista1,Cabeca),!, unifica([X|_], Resto, [X|Lista2]). unifica(Lista1, [Cabeca|Resto], [Cabeca|Lista2]) :- unifica(Lista1, Resto, Lista2). – Miguel May 18 '14 at 13:13
  • @Miguel Your code in the comment is hard to read because of formatting. You may want to post the actual problem that you are trying to solve as a separate question - it should get more attention this way. – Sergey Kalinichenko May 18 '14 at 13:30
  • Ok, i'll open a new question for this, thank for the above information – Miguel May 18 '14 at 13:33