in reply to Re: passing subroutine references
in thread passing subroutine references
I would not call that a better fix. Adds unnecessary complexity and just confuses things. If you do insist that you do not want to have to pass the $move_disk around, it's cleaner to do it like this:
{ my $move_disk; sub hanoi { $move_disk = pop(@_); goto &_hanoi; # (almost) equivalent to _hanoi(@_) } sub _hanoi { my ( $n, $start, $end, $extra ) = @_; if ( $n == 1 ) { $move_disk->( 1, $start, $end ); } else { _hanoi( $n - 1, $start, $extra, $end ); $move_disk->( $n, $start, $end ); _hanoi( $n - 1, $extra, $end, $start ); } }; } hanoi( 3, 'A', 'B', 'C', sub {print "(@_)\n"} );
Jenda
Enoch was right!
Enjoy the last years of Rome.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: passing subroutine references
by ikegami (Patriarch) on Sep 19, 2009 at 13:37 UTC | |
by Jenda (Abbot) on Sep 19, 2009 at 15:43 UTC | |
by ikegami (Patriarch) on Sep 20, 2009 at 17:12 UTC | |
by Jenda (Abbot) on Sep 20, 2009 at 21:51 UTC | |
by ikegami (Patriarch) on Sep 21, 2009 at 03:02 UTC | |
by ikegami (Patriarch) on Sep 21, 2009 at 03:00 UTC | |
|
Re^3: passing subroutine references
by joe76 (Novice) on Sep 20, 2009 at 17:02 UTC | |
by ikegami (Patriarch) on Sep 20, 2009 at 17:27 UTC | |
by Jenda (Abbot) on Sep 20, 2009 at 21:42 UTC |