You need a name for the routine, say sumTo. And the args: the target and the list. So, you can write the skeleton:

sub sumTo { my $target = shift; ... }
  1. First consider the case where the list has one element that matches the target:
    sub sumTo { my $target = shift; my @list = @_; if( $list[ 0 ] ) == $target ) return 1; } return 0; }
  2. Now, consider that any element of the list might exactly match the target. So you need to iterate the list and check each in turn. No need to copy the list, it's already in @_ once you've shifted off $target:
    sub sumTo { my $target = shift; for( @_ ) { if( $_ ) == $target ) return 1; } } return 0; }

    If the target is in the list, it returns 1, otherwise it returns 0. And if the list is empty, it also returns 0;

  3. Now how to use the recursion? For each number in the list, we can subtract it from the target and remove it from the list and we then end up with a similar problem to that with which we started. So we can passed the reduced target and shortened list to the same routine and let it deal with it. If it succeeds, then we've succeeded and can return true, but we must continue iterating if otherwise.

    In order to pick out the individual values from the list, we need to use indexes rather than the values directly. We can then slice @_ to reduce the list:

    sub sumTo { my $target = shift; for( 0 .. $#_ ) { if( $_[ $_ ] == $target ) return 1; } else { if( sumTo( $target - $_[$_], @_[0 .. $_-1, $_+1 .. $#_] ) +) { return 1; } } } return 0; }

    And that's it. The essence of recursion is to reduce the problem at each step to a similar, but simpler problem that will eventually converge. In this case, we pick out each number in the list and subtract it from the target. We then have a smaller target and a smaller list and we recurse to solve that problem. Eventually, the list will be reduced to 1 element, and if it matches the remaining target, we've solved the problem.

    One element of the above to pay special consideration to is the slice operation which removes the current element from the list we recurse on:

    @_[0 .. $_-1, $_+1 .. $#_]

    Understanding how that works is left as an exercise, but be sure to understand it well--write a few small programs that display the results of that expression as the index iterates--because you can guarentee if this is homework, you are going to be questioned hard about that.

There is also an error in the above, so you're gonna have to understand it to some level before you will have a working program.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Recursion problem by BrowserUk
in thread Recursion problem by someone202

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.