You write Perl code like this:
sub gron { my ($f, $total, $width) = @_; my $veet; $veet = sub { my ($partial, $subtotal, $n) = @_; my $rem = $total - $subtotal; if ($n+1 == $width) { $f->($rem, @$partial); } else { $veet->([$_, @$partial], $subtotal+$_, $n+1) for 0..$rem; } }; $veet->([], 0, 0); }I'm fairly sure that the idea of using a recursive closure in Perl has never crossed my mind before. Notice the disguised conses as well :-)
gron(sub {print($_ ? $_ : " ") for @_; print "\n"}, 3, 27);
B::Utils
for a doubly-recursive closure. That broke my brane.
my $x; $x = sub {
...you've just defined a circular data structure. So once you're finished with the whole excursion of calling $x, you have to undef $x or the sub-object doesn't get destroyed -- at least last I checked.
Re:Recursive closures
koschei on 2002-05-25T21:08:53
Do you happen to have an example of the use of one of these fiendish Y operators in Perl? (If you don't, don't feel compelled to write one if you don't want to, I'm mostly only curious for curiosity's sake.)Y Operator in Perl
TorgoX on 2002-05-25T23:26:00
http://www.speech.cs.cmu.edu/user/sburke/pub/y_operator.plRe: Y Operator in Perl
robin on 2002-05-26T12:32:46
That reminds me of a staggering bit of obfuscated Haskell programming.