I have encountered a lot of problems with a literal that starts with a _
such as _Peter
. So what exactly would be the difference between:
good(_,_).
and
good(_,_Peter).
Thank you for answering my question!
A single underscore ( _ ) denotes an anonymous variable and means "any term". Unlike other variables, the underscore does not represent the same value everywhere it occurs within a predicate definition. A compound term is composed of an atom called a "functor" and a number of "arguments", which are again terms.
The = "operator" in Prolog is actually a predicate (with infix notation) =/2 that succeeds when the two terms are unified. Thus X = 2 or 2 = X amount to the same thing, a goal to unify X with 2. The == "operator" differs in that it succeeds only if the two terms are already identical without further unification.
Variables. A variable is a string of upper-case letters, lower-case letters, digits and underscore characters that starts either with an upper-case letter or with an underscore. For example, X , Y , Variable , _tag , X_526 , List , List24 , _head , Tail , _input and Output are all Prolog variables.
_
alone is an anonymous variable. Multiple occurrences in the same clause (or the same read-term) represent different variables.
A variable starting with _
but containing further characters is not an anonymous variable. Several occurrences represent the same variable.
By convention, many Prolog systems require that variables occurring only once need to start with a _
. But this is a convention leading in most implementations to warnings only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With