Q: Junctions & send+more=money
I have two questions about this example code (taken from http://svn.openfoundry.org/pugs/examples/sendmoremoney.p6) (btw, a really nice example of how to use junctions - just try to write this in perl5 :) #!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). 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? How would I rewrite this example to be more general, so that given 3 strings (in this case 'send', 'more', 'money'), the program would give all possible results for the equation + = . -- Markus Laire
S06: Pairs as lvalues
Hi, quoting http://dev.perl.org/perl6/synopsis/S06.html: > Pairs can be used as lvalues. The value of the pair is the > recipient of the assignment: > > (key => $var) = "value"; > > When binding pairs, names can be used to "match up" lvalues > and rvalues: > > (who => $name, why => $reason) := (why => $because, who => "me"); that's really convenient, but what will the following code do? my $x = (a => 42); # $x is a Pair. $x = 13; # Is $x now the Pair (a => 13) or # the Int 13? When executing the second line, $x is a Pair. And it is used as a lvalue. So, according to S06, the assignment will only change the value of the Pair, not the Pair itself. But I doubt this is what most programmers will expect -- example: sub foo (Any $x) { $x = 13; # Won't always make $x an Int. ...; } my $pair = (a => 42); foo($pair); So, does this Pairs-as-lvalue rule only affect "real syntax-Pairs"? If not, what would the sub unpairify in the following code have to look like? my $x = (a => 42); unpairify($x, 13); # $x is now the Int 13. sub unpairify (Any $x is rw, Int $num) { ...; # fill in please } --Ingo -- Linux, the choice of a GNU | self-reference, n. - See self-reference generation on a dual AMD- | Athlon!|
Re: S06: Pairs as lvalues
> "IB" == Ingo Blechschmidt <[EMAIL PROTECTED]> writes: IB> Hi, IB> quoting http://dev.perl.org/perl6/synopsis/S06.html: >> Pairs can be used as lvalues. The value of the pair is the >> recipient of the assignment: >> >> (key => $var) = "value"; >> >> When binding pairs, names can be used to "match up" lvalues ^^^ >> and rvalues: >> >> (who => $name, why => $reason) := (why => $because, who => "me"); note the binding := which is not the same as =. binding is similar to aliasing. in the above case it matches the names and assigns the new values accordingly. i am not sure what happens if you have more pairs on one side or the other (does it behave as hashes would and just overwrite the common keys and assign new ones as needed?) IB> that's really convenient, but what will the following code do? IB> my $x = (a => 42); # $x is a Pair. IB> $x = 13; # Is $x now the Pair (a => 13) or IB> # the Int 13? $x is 13 now as you assigned it. to assign the value of the pair as an lvalue i think you would do: $x.value = 13 ; uri -- Uri Guttman -- [EMAIL PROTECTED] http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs http://jobs.perl.org
Re: S06: Pairs as lvalues
Hi, Uri Guttman stemsystems.com> writes: > note the binding := which is not the same as =. binding is > similar to aliasing. in the above case it matches the names > and assigns the new values accordingly. that makes sense. But consider: > > "IB" == Ingo Blechschmidt web.de> writes: > IB> quoting http://dev.perl.org/perl6/synopsis/S06.html: > >> Pairs can be used as lvalues. The value of the pair is the > >> recipient of the assignment: > >> > >> (key => $var) = "value"; # Note: Simple assignment, no binding! Here, there's a simple assignment, no binding. Is this a typo then, and it should be read as C<< (key => $var) := "value" >>? If yes, everything's clear. If not, then consider: (key => $var) = "value"; # Example from S06 my $value = "value"; (key => $var) = $value; # RHS replaced by a variable, # should make not difference. my $pair = (key => 3); $pair= $value; # Former LHS (a Pair) replaced # by a Pair variable, should # make no difference, too... say $pair.value; # Will print "value"... --Ingo -- Linux, the choice of a GNU | Mathematicians practice absolute freedom. generation on a dual AMD- | -- Henry Adams Athlon!|
Re: Q: Junctions & send+more=money
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(..) & none(..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 that together total between 1..9?" 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 9 "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
Re: S06: Pairs as lvalues
Ingo Blechschmidt writes: > that's really convenient, but what will the following code do? > my $x = (a => 42); # $x is a Pair. > $x = 13; # Is $x now the Pair (a => 13) or > # the Int 13? It's the Int 13. Your example looks a lot like this one: my $x = [ 1, 2, 3 ]; $x = 13; And you could say that is an error because: [ 1, 2, 3 ] = 13; Is an error, but you'd be wrong. You see, in your example, the pair is not "functioning as an lvalue". The variable is the thing that is the lvalue, not the pair. I belive you could get the pair as an lvalue if you did: my (Pair $x) := (a => 42); $x = 13; Because the variable x is now *fundamentally* a pair; it has no container, so to speak. But it would die, because you're trying to change a constant value. Luke