in reply to Re: Recursion problem
in thread Recursion problem

First consider the case where the list has one element that matches the target:
The base case is even simpler: for $target == 0 any list fulfills the condition, so a simple return 1 if $target == 0; should do.

Replies are listed 'Best First'.
Re^3: Recursion problem
by psini (Deacon) on May 25, 2008 at 09:55 UTC

    ... and if $target < 0 no list fulfills the condition, so return 0 if $target < 0; is the second hint

    Update: no, sorry, I wrongly assumed that the array containn only positive integers...

    So the second hint should be return 0 if @array==0

    Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

Re^3: Recursion problem
by BrowserUk (Patriarch) on May 25, 2008 at 10:27 UTC
      Just pick zero elements of that list, they sum up to zero.

        Hm. A mathemeatically correct degenerate case I suppose, but given that the list can contain negative numbers, which makes a zero target a legitimate non-degenerate possibility, I can't see a useful case for returning an empty list and calling it success.


        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.