next up previous
Next: Exercises: Up: Remarks on Variables Previous: Naming variables

The anonymous variable

If you have tried putting any of the above examples into a file which you then consult into SICStus Prolog, you will have discovered that it complains. For example, loading the rule for second/2 produces the following:
{Warning: [First,Rest] - singleton variables in second/2 in lines 1-1}
This is because each of the variables First and Rest occurs only once in the definition. Because the most common use of variables in Prolog is to convey information from one part of a program to another, the occurrence of any singleton variable triggers a warning, to alert the programmer to the possibility of an error. What to do?
  1. Ignore the warning. It doesn't affect the running of the program in any way; but there may be a genuine mistake hidden in the list of singleton variables which becomes hard to spot
  2. Use the underscore symbol instead of your singleton variables. This would change the definition of second/2 to this second([_, Second_], Second). Although we have two occurrences of the underscore in this definition, Prolog treats them as unrelated (and identical in interpretation to the original definition), as `anonymous'.

next up previous
Next: Exercises: Up: Remarks on Variables Previous: Naming variables
Steve Harlow 2001-11-26