in reply to Comprehending Binary :=

Basically, the := operator is a lazy assignment, which means that, in a conceptual manner, the calculation/operation is not really done until the result is absolutely needed as opposed to '=' and being done right then and there (from an implimentation standpoint, I would not be surprised to see that lazy evaluations are 'flattened' into the actual locations where needed.).

If you've ever used a symbolic math program like Maple, Mathematica, or Mathcad, you're probably familar with this idea already. If not, then consider that using := is similar to an algebric statement; that is, if I say "$y := $x - 5", then y will ALWAYS being x - 5 until I redefine it or it goes out of scope.

A better example would be how to use this to simplify conversions from a database when needed. Without lazy evaluation:

while ( my ( $name, $height, $weight ) = $sth->fetchrow_array() ) { my $ratio = $height/$weight; send_to_html( $name, $ratio ); }
With lazy eval:
my ( $name , $height, $weight ); my $ratio := $height/$weight; # I would presure that # this would need to be # done under -w/strict send_to_html( $name, $ratio ) while ( $name, $height, $weight ) = $sth->fetchrow_array() );
This may not seem as powerful now, but there's a lot of potental for it.

Now, in regard to your swap example, the behavior there is currently undefined in that you're creating circular references with lazy evaluation (Just like you can do in Excel). I expect this to flag a runtime error , possibly a compile-time error. But until we have Perl 6 in our hands, we won't know for sure.

Update as pointed out below, I got myself confused with the := operator and perl laziness.

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re (tilly) 2: Comprehending Binary :=
by tilly (Archbishop) on Oct 06, 2001 at 16:21 UTC
    I think not.

    Larry Wall's use of the word "unification" gave me similar hopes. However TheDamian's comment at Re: Re (tilly) 1: Apocalypse 3 put me straight, and then his comparison with typeglob aliasing made it obvious.

    Perl 5 has typeglobs. Perl 6 will not, but needs an equivalent way to do the same thing. := is the answer that they came up with.

    But since it is aliasing if you, for instance, try:

    $y := $x - 5;
    what will happen is that $x - 5 is calculated, that goes into a new (constant) variable, and then $y is aliased to that constant. If you later change $x, $y is still aliased to the constant.

    Sorry.

      Okay, so if I'm understanding these comments correctly (which is not guaranteed since I don't grok typeglbos and I've never touched Prolog or used a "with" statement, we're back to := just saving keystrokes. It isn't lazily evaluated.

      Which just doesn't seem right, I have to be missing something.

      So how is $bound := %data{$key} different from $bound = \%data{$key} aside from the latter being a reference? (And since we'll have automatic dereferencing to the expected context, that shouldn't be a problem, correct?)

      I now see that = still slurps, so (@a, @b) := (@b, @a) isn't easily reproduceable, and thus we have a clear use for :=....but is that really the extent of it?

        So how is $bound := %data{$key} different from $bound = \%data{$key}

        In the first case, $bound becomes another name for %data{$key}. In the second case, $bound simply stores a reference to %data{$key}.

        That one extra level of indirection may not seem important, but consider what happens later in the code when we write:

        $bound = $nuclear_reactor_shutoff_command;
        In the first case, the shutoff command goes into the entry %data{$key}. In the second case, the reference to %data{$key} is (silently!) replaced by the shutoff command, and %data{$key} itself is not updated at all.

        Which is better? Well, of course, it depends on what you were trying to achieve. But, with the departure of typeglobs, we need to have some way to do the former, and that way is going to be binding.

        And, indeed, as I mentioned in E3, binding has a couple of major advantages over typeglobbing: it's typesafe(r), and bound variables can be lexically scoped.

        we're back to := just saving keystrokes

        Hey! That's an important achievement. ;-)

        But, of course, there are many other uses for binding too (some of which have already been mentioned in this thread). Here's another that hasn't been mentioned yet. Binding is particularly useful when you have lvalue subroutines/methods and you want to do something complex with the lvalue. Rather than repeatedly calling the subroutine/method:

        while (<>) { $obj.threshold += $_ unless $obj.threshold > $min; }

        you can just bind the lvalue once and use the bound variable instead:

        my $thresh := $obj.threshold; while (<>) { $thresh += $_ unless $thresh > $min; }
        Well first of all it is important that something like := exists because we need to translate old Perl 5 code into Perl 6.

        But beyond that, well if you don't know how to use typeglobs in Perl 5, then you likely won't need to know it in Perl 6. However that does not mean that its job is lessening how many keystrokes to use. Rather think of it as selectively changing which keystrokes to use. Without that ability you couldn't write something like Exporter. Without the ability to produce Explorer you wouldn't be able to easily write something like:

        use CGI qw(:standard); my @parameter_names = param();
        So you see it is quite important that the capability be in the language, even if you will never use it directly.
Re: Re: Comprehending Binary :=
by demerphq (Chancellor) on Oct 06, 2001 at 16:19 UTC
    Weird. I'm utterly at a loss as to how you got this interpretation for the := operator from Exegesis 3 or from Apocalypse 3

    I dont think this new operator has anything to do with lazy evaluation. My reading of these docs is that it is fairly straightforward aliasing going on.

    Consider Exegesis clearly states that

    (@x,@y):=(@y,@x);
    is an efficient way to swap the contents of the two arrays. It would do this by simply aliasing the variable name @x with the array that the variable name @y refers to, and vice versa.

    tilly suggested that Re: Re (tilly) 1: Apocalypse 3 may be useful in understanding our different views :-)

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: Re: Comprehending Binary :=
by raptor (Sexton) on Oct 06, 2001 at 20:43 UTC
    if it is really easily evealuated it wll be cute, i didn't thought it will work that way :")
    That way it will be possible to say :

    $x := $y + 5; ...code... $x := $z + 12 if cond1; ...code... $x := 6 + $p if cond2; ...code... print $x

    Now think if both cond1 and cond2 succeed ... in a normal assigment u will calculate $x three times.(what if the calculation is very complex).
    In our case it will be calculated only when we call 'print'..
    On the other hand using several subs for this thing is waste, not to mention that the call to sub is slow... So if I'm right := is/can-be-used-as :

    1. binding
    2. alaising
    3. simple-sub (mostly for expressions)
    4. assignment
    any other ideas.. :")

    =====
    raptor