What?  Set Theoretic Ordinals. You know! Silly! The representation of the integers as nested sets.

Like this:

0 == ( ) ( i.e. the empty set )
1 == ( 0 ) == ( ( ) )
2 == ( 0 , 1 ) == ( ( ) , ( ( ) ) )
3 == ( 0 , 1 , 2 ) == ( ( ) , ( ( ) ) , ( ( ) , ( ( ) ) ) )
etc.

Doesn't everyone wish they 'had a chunk of code that did that'? Well, wish no more! For here it is! Calling ordinal( 3 ) produces a nested arrayref of [ [] , [ [] ] , [ [] , [ [] ] ] ] or, perhaps more obviously,

[ [] , [ [] ] , [ [] , [ [] ] ] ]
Now, I ask you, is that not useful? Is it not the snippet you've been waiting for? Why, sure it is!

You're welcome!

</irony>

sub ordinal { my @list = defined($_[0]) ? @{ $_[1] } : (); if ( $_[0] == 0 ) { return \@list } if ( $_[0] == 1 ) { return [ @list , \@list ] } return ordinal( $_[0] - 1 , [ @list , \@list ] ); }

Replies are listed 'Best First'.
Re: Set Theoretic Ordinals
by larsen (Parson) on Sep 18, 2001 at 12:30 UTC