In those thing that seem the most simple, may lay the greatest complexity..
This is a search for enlightment. I can see that it works, but I don't know why; is the definition of "faith"?

Page 36 of "Higher Order Perl" contains a function called "find_share", as part of a solution to the partitioning problem.

sub find_share { my ($target, $treasures) = @_; return [] if $target == 0; # return 0 if $target == 0; #my code experiment # return '' if $target == 0; #my code experiment # return '0' if $target == 0; #my code experiment # return () if $target == 0; #my code experiment # return undef if $target == 0;#my code experiment return if $target < 0 || @$treasures == 0; my ($first, @rest) = @$treasures; my $solution = find_share($target-$first, \@rest); return [$first, @$solution] if $solution; return find_share($target , \@rest); } sub partition { my $total = 0; my $share_2; for my $treasure (@_) { $total += $treasure; } my $share_1 = find_share($total/2, [@_]); return unless defined $share_1; my %in_share_1; for my $treasure (@$share_1) { ++$in_share_1{$treasure}; } for my $treasure (@_) { if ($in_share_1{$treasure}) { --$in_share_1{$treasure}; } else { push @$share_2, $treasure; } } ## My code to run samples my ($rslt1,$rslt2) = partition ( 1,5,3,1,6,2 ); if ($rslt1){ printf "rslt1= %s\n",join ',',@$rslt1; printf "rslt2= %s\n",join ',',@$rslt2; }
The accompanying text says:
We take care of some trivial cases first. If the target amount is exactly zero, then it's easy to produce a list of treasures that total the target amount: the empty list is sure to have value zero, so we return that right away.
? return the 'empty list' right away? Wouldn't that be ()?
? [] = empty list?

The closer I look, the more puzzling things are. But I think they all revolve around the use of the Anonymous Array Composer to passing references rather than data.

The calling sequence is:

my $share_1 = find_share($total/2, [@_]);
which passes as argument 2, I am hoping; the parameter array and re-encapsulates it in an anonymous array and passes the reference to the encapsulation to find_share.
find_share returns references to anon arrays as well. Looking at the variable $solution, I see it is set with the return value from a recursive call the this code; and subsequently used in a Boolean context on the next line.
Checking "perlsyn::Truth and Falsehood" to see what would make $solution FALSE, gives:
The number 0, the strings '0' and '', the empty list "()", and "undef" are all false in a boolean context. All other values are true.
None of those values worked in place of [] (the commented lines). So what type and value is $solution???
It is always better to have seen your target for yourself, rather than depend upon someone else's description.

In reply to A new FALSEness by Wiggins

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.