### Comment on

 Need Help??
tall_man,
The only thing that is recursive is merge() which could also be iterative. I believe it does what you want.
```#!/usr/bin/perl
use strict;
use warnings;

use constant TAIL => -1;

# Usage my \$next = genHam( take => #, list => [<factors>] );
# If take is not specified, it assumes infinity

my \$next = genHam( take => 23, list => [2, 3, 5] );
while ( my \$n = \$next->() ) {
print "\$n\n";
}

sub genHam {
die "Incorrect number of arguments" if @_ % 2;
my %arg = @_;
return () if ! exists \$arg{list} || ref \$arg{list} ne 'ARRAY';

\$arg{take} ||= -1;
my \$n = 0;

my (@pool, @stream);
push @stream, [] for 0 .. \$#{ \$arg{list} };
\$stream[TAIL] = [ \$arg{list}->[TAIL] ];

return sub {
return () if ! \$arg{take}--;

# Return obligatory 1
return ++\$n if ! \$n;

# No need to generate as we already have some in the pool
return shift @pool if @pool;

# Increase highest factor
\$stream[TAIL] = [ \$arg{list}->[TAIL] * \$n ];
++\$n;

# Generate streams of multiples >= highest factor
for my \$i ( 0 .. \$#stream - 1 ) {
\$stream[ \$i ] = [];

my \$multiple = \$arg{list}->[ \$i ];

# Determine what multiple each factor should start at
my \$start = \$n > 2 ? (\$arg{list}->[TAIL] * (\$n - 2)) / \$mu
+ltiple + 1 : 1;

# Add multiples to the stream less than or equal to the hi
+ghest factor
for ( \$start .. \$stream[TAIL]->[HEAD] / \$multiple ) {
push @{ \$stream[ \$i ] }, \$multiple * \$_;
}
}

# Merge streams into new pool
for ( 1 .. \$#stream ) {
@pool = merge(\@pool, \$stream[ \$_ ]);
}

# Return first member of pool
return shift @pool;
}
}

sub merge {
my (\$s1, \$s2) = @_;
return @\$s2 if ! @\$s1;
return @\$s1 if ! @\$s2;

shift @\$s1;
return shift @\$s2, merge( @_ );
}

Cheers - L~R

Update: This is wrong. I was so focused on lazy evaluation that I misunderstood "how do you generate the series of numbers composed of a given list of prime factors, where each can be used an unlimited number of times?" To me, that meant any positive multiple of any factor was valid.

In reply to Re: Hamming Sequences and Lazy Lists by Limbic~Region
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!
• 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.
• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-06-23 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?