Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://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":



  • 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found