Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Singleton variables

189 views
Skip to first unread message

pineapp...@yahoo.com

unread,
Sep 30, 2008, 11:39:56 PM9/30/08
to
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

unread,
Oct 1, 2008, 10:13:06 AM10/1/08
to
In message
<c3906938-0430-424a...@x41g2000hsb.googlegroups.com>,
pineapp...@yahoo.com writes

>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 ni...@maproom.co.uk

0 new messages