Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This solution is extremely similar to the Haskell one in both structure and technique -- as much of a direct translation as I could manage. I had to roll a couple of tools, but they are elegantly in keeping with functional programming style.

I was really pleased with how this all came together.

use strict; use warnings; # A stream is an array ref, in which the first element is an ordinary # value, and the second element is a continuation: a sub that yields # a stream. In other words, a lazy list. # Takes an ordinary list and turns it into a stream sub stream_up { my ($x, @rest) = @_; return [ $x, sub { @rest ? stream_up(@rest) : [] } ]; } # Yields the first N elements from a stream sub take { my ($N, $x_xs) = @_; $N > 0 or return (); if (!@$x_xs) { return () } my ($x, $xs) = @$x_xs; return($x, take($N-1, $xs->())); } # merge takes two streams, and returns one sub merge { my ($x_xs, $y_ys) = @_; if (!@$x_xs) { return $y_ys } if (!@$y_ys) { return $x_xs } my ($x, $xs) = @$x_xs; my ($y, $ys) = @$y_ys; if ($x < $y) { return [$x, sub { merge($xs->(), $y_ys) } ] } if ($x > $y) { return [$y, sub { merge($x_xs , $ys->())} ] } if ($x == $y) { return [$x, sub { merge($xs->(), $ys->())} ] } } # Like map, but applies coderef to stream, returning a stream sub stream_map { my ($coderef, $x_xs) = @_; if (!@$x_xs) { return $x_xs } my ($x, $xs) = @$x_xs; local $_ = $x; return [$coderef->($x), sub { stream_map($coderef, $xs->()) }]; } # genHam takes a stream and returns a stream sub genHam { my ($x_xs) = @_; if (!@$x_xs) { return $x_xs } my ($x, $xs) = @$x_xs; my $out; $out = merge([1, sub {stream_map(sub{$_ * $x}, $out)}], genHam($xs +->())); } print "$_\n" for take 20, genHam stream_up(2,3,5);

Caution: Contents may have been coded under pressure.

In reply to Re: Hamming Sequences and Lazy Lists by Roy Johnson
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
  • 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?
    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 avoiding work at the Monastery: (11)
    As of 2015-07-31 11:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (276 votes), past polls