Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (14)
    As of 2014-07-29 18:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (225 votes), past polls