Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

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

The challenge would be to do this in perl. I believe perl6 has or will soon be getting Haskell-like lazy infinite lists. Can it be done in perl5?

To address this question more directly, I think it'd take very little to make this possible in Perl5. All that is required is a special function to mimic the ':' operator in Haskell. The crucial requirement for this operator is that its last operand not be evaluated unless it is specifically requested.

In what I posted, I had to do this "by hand"; i.e. what should have been simply

ll_new( $x, < any perl expression > )
had to be recast into
ll_new( $x, memoize( sub { < any perl expression > } ) )
The business with memoize is in some sense not essential; this would have, in principle, worked too:
ll_new( $x, sub { < any perl expression > } )

Wrapping the second argument with an anonymous sub effectively delays its evaluation. But without the memoization, this whole scheme becomes hopelessly bogged down with all the recursive calls.

If perl did not evaluate the second argument of ll_new, then the whole implementation would look a lot cleaner. For example, the definition of $fibs above would go from:

$fibs = ll_new( 0, memoize ( sub { ll_new( 1, memoize ( sub { ll_add( tail( $fibs ), $fibs ); } ) ); } ) );
to the relatively pithy:
$fibs = ll_new( 0, ll_new( 1, ll_add( tail( $fibs ), $fibs ) ) );
which holds its own against the Haskell rendition of the same:
fibs = 0:1:[a+b| (a,b) <- zip fibs (tail fibs) ]

the lowliest monk

In reply to Re: Hamming Sequences and Lazy Lists by tlm
in thread Hamming Sequences and Lazy Lists by tall_man

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Discipulus]: it seems choroba? you doubt?
    [choroba]: It works about 2.5 times faster than the original sort
    [Corion]: choroba: Well, it should be faster since it exploits that it only has to sort much smaller lists :)
    [Corion]: Also, it outputs (partial) results much earlier, which is nice too :)
    [choroba]: I hope it's ok , there's just no reaction from the OP

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2018-02-22 08:51 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (289 votes). Check out past polls.