Discussion:
Singleton variables
(too old to reply)
p***@yahoo.com
2008-10-01 03:39:56 UTC
Permalink
Can a guru please explain singleton variables (I get many warnings in
my code)? Are they good? Are they bad? Provide an example? And how
can one eliminate them - indeed, should one eliminate them?

Thanks
Nick Wedd
2008-10-01 14:13:06 UTC
Permalink
In message
Post by p***@yahoo.com
Can a guru please explain singleton variables (I get many warnings in
my code)? Are they good? Are they bad? Provide an example? And how
can one eliminate them - indeed, should one eliminate them?
They are bad. At best (first example below) they are pointless, and
while they do no harm, it will make your code more readable if you
replace them by the anonymous variable _.

But usually, they indicate a typo (second example below) or other error,
and your code will not work until you fix them.

Example of deliberate use:
min( A, B, A ) :- A<B, !.
min( A, B, B).
Here the second clause has a singleton A. I would have written it
min( _, B, B).

Example of singleton caused by typo:
append( [], Any, Any ).
append( [Head|Tail], Any, [Head|Rest] ) :- append( Tial, Any, Rest ).
Here the second clause will generate two singleton-variable warnings,
and you will read the warnings, spot the typo, and fix it.

Nick
--
Nick Wedd ***@maproom.co.uk
Continue reading on narkive:
Loading...