Allow me to add the (inefficient) recursive solution that you were looking for.

Writing recursion is 2 tricks together. The first is to take a problem and break it into one or more simpler problems. The other is to figure out the simple problems that you will come to and make them special cases.

So in this case what do we want? We want a function that takes a number (m) and an array of n things, and returns all of the possible choices of m things from the list. Well we can make this simpler by deciding whether or not to choose the first element. That will reduce n, and possibly m as well, which has to be simpler.

Now what simple problems can we come down to? Well we could get down to needing no elements. In which case there is one list of no elements. Or we could get down to a situation where our list is shorter than the number we want, in which case we have no answers.

So we wind up with this:

# Takes a number and a list of elements. Returns all possible ways to # select that number out of the list. sub choose_m { my $m = shift; # Special cases first if (0 == $m) { return []; } elsif (@_ < $m) { return (); } else { # This is our recursion step my $first = shift; return ( # Things without the first element choose_m($m, @_), # Things with the first element map {[$first, @$_]} choose_m($m - 1, @_) ); } }
Now a warning on recursion. Recursive problems where you make a *pair* of recursive calls (like this one) open you up to processing an exponential amount of data, or even returning that. (Certainly the case here!) This should be a danger flag, and when you reach for this you should either be willing to pay a very steep price, or you should know why in your situation it won't be a problem.

Try to write an iterative and a recursive solution to calculating Fibonacci numbers, see which is faster. Likewise Perl's RE engine uses recursion, if you look around you will find some discussions of the possible problems that result. And in this case, well try to list 30 choose 15 things, see what happens to your memory... :-)


In reply to Re (tilly) 1: nCr arrays of size r by tilly
in thread nCr arrays of size r by PsychoSpunk

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.
  • 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, details, 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, summary, 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.