Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I came up with a similar, and arguably neater implementation:

sub sum{ ( shift()//return 0 ) + &sum } print sum( 1,2,3);; 6 print sum( 1 .. 100 );; Deep recursion on subroutine "main::sum" at 5050

but rejected it because 'sum' is a (glob) variable.

In theory, it is possible to avoid the naming of the sub, thereby achieving "anonymous recursion", by using the Y-combinator. And the Y-combinator has been achieved in Perl by a former regular here.

Putting it together you get:

print Y( sub{my$rec=shift; sub{(shift()//return 0) + &$rec }})->(1 .. +100);; 5050

But whilst that achieves the Y-combinators goal of recursion without adding the sub to the permanent namespace, it still requires the naming of the pesky closure $rec.

And of course, requires you to add the Y-combinator to the permanent namespace first:

sub Y { my ( $curried_rec ) = @_; sub { my ( $f1 ) = @_; $curried_rec->( sub { $f1->( $f1 )->( @_ ) } ) }->( sub { my ( $f2 ) = @_; $curried_rec->( sub { $f2->( $f2 )->( @_ ) } ) } ) }

And that's already more obfuscation as I want to wrap my brain around, even in an obfuscation section post!

My final thought is that the simplest mechanism that fits the OPs breif is just:

C:\test>perl -Mstrict -wE"say eval join'+',()" C:\test>perl -Mstrict -wE"say eval join'+',0" 0 C:\test>perl -Mstrict -wE"say eval join'+',-1" -1 C:\test>perl -Mstrict -wE"say eval join'+',-1..+3" 5

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^2: (almost) foldl by BrowserUk
in thread (almost) foldl by dk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-12-07 09:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found