drbean has asked for the wisdom of the Perl Monks concerning the following question:
The code to construct the list, a list of the numbers from m to n, is:sub tail { my $list = shift; if ( ref( $list->[1] eq 'CODE' ) { return $s->[1]->() ); else { return $s->[1] } }
wheresub upto { my ($m, $n) = @_; return if $m > $n; node( $m, promise { upto($m+1, $n) } ); }
and:sub promise (&) { $_[0] }
According to MJD, the tail() function sees the promise and invokes the anonymous promise function, which in turn invokes upto($m+1, $n). When I try this code out on Fedora 9 with perl-5.10.0, I get the error:sub node { my ($head, $tail) = @_; return [$head, $tail]; }
It is apparently parsing the block argument to promise as an indirect method call on the non-existent object { upto($m+1, $n) } If I wrap the block argument to promise in parentheses, I get the errors:'Can't call method "promise" on an undefined value.'
If I then disambiguate the thing in braces as a block with { return upto($m+1, $n) }, undef is returned. And if I replace the braces in the original line with parentheses, likeOdd number of elements in anonymous hash at stream.pl line 19. Use of uninitialized value in anonymous hash ({}) at stream.pl line 19 +.
the function is eagerly evaluated, not lazily. So, what do I tell MJD? I think this probably worked before. Is this a problem with newer perls?node( $m, promise ( upto($m+1, $n) ) );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: lazy evaluation of sub arg
by Anonymous Monk on Dec 17, 2008 at 11:42 UTC | |
by drbean (Novice) on Dec 17, 2008 at 14:08 UTC | |
by chromatic (Archbishop) on Dec 17, 2008 at 17:47 UTC | |
|
Re: lazy evaluation of sub arg
by AnomalousMonk (Archbishop) on Dec 17, 2008 at 14:15 UTC |