Markus Laire wrote:
I have two questions about this example code (taken from http://svn.openfoundry.org/pugs/examples/sendmoremoney.p6)
I have a few issues with this code. Or at least observations of how it differs from the classic "SEND + MORE = MONEY" problem. see below.
#!perl6 use v6;
my $s; my $e; my $n; my $d; my $m; my $o; my $r; my $y;
$s = any(0..10) & none(0); $e = any(0..10); $n = any(0..10); $d = any(0..10); $m = any(0..10) & none(0); $o = any(0..10); $r = any(0..10); $n = any(0..10); $y = any(0..10);
I think these should be any(0..9).
Indeed they should. I will assume they are written as such in my discussion below.
my $send := construct($s,$e,$n,$d); my $more := construct($m,$o,$r,$e); my $money := construct($m,$o,$n,$e,$y);
if ($send + $more == $money) { say " send = $send"; say "+more = $more"; say "-------------" say "money = $money"; }
sub foldl(Code &op, Any $initial, [EMAIL PROTECTED]) returns Any { if ([EMAIL PROTECTED] == 0) { return $initial; } else { return &op(shift @values, &?SUB(&op, $initial, @values)); } }
sub add(Int $x, Int $y) returns Int { return $x + $y; }
sub construct([EMAIL PROTECTED]) returns Junction { return foldl( sub ($x, $y) { $x * 10 + $y}, 0, @values); }
How would the if (...) {...} work if there were more than one possible match to this equation?
As written, this generates a rather large number of solutions. I do not see any test to make sure that the individual letters are different. Nor is there any check to see if all the "e"'s assume the same value.
So what you reach the:
if ($send + $more == $money) {...}
stage is $send being something equivalent to any(0000..9999) & none(0000..0999), only no where near as simplified. Similar values can be found in $more and $money. Therefore, you're asking something like: "Are there any two numbers between 1000 and 9999 that together total between 10000..99999?" The answer is yes, and then you get an error as you attempt to print a raw junction, if you're lucky, or 9000 "send" lines, followed by 9000 "more" lines, followed by 90000 "money" lines.
Junctions are _not_ the same as unbound variables in Prolog. They do not "widdle away" inconsistent values as those inconsistencies are found. Most of them (all except all()) do not have to use the same value each time they are evaluated.
If I'm wrong about this interpretation of this code, I apologize. But it certainly fits my understanding of junctions, which has grown by leaps and bounds over the last few weeks.
HTH,
-- Rod Adams