develooper Front page | perl.perl6.language | Postings from January 2004

Semantics of vector operations

Thread Next
From:
Luke Palmer
Date:
January 20, 2004 00:54
Subject:
Semantics of vector operations
Message ID:
20040120085433.GA23075@babylonia.flatirons.org
A thought occurred to me.  What should this return:

    [1,2,3] »+« [4,5,6]

At first glance, one might say [5,7,9].  But is that really the best
way to go?  I'm beginning to think that it should be the same as
whatever [1,2,3]+[4,5,6] is, hopefully an error.

Here's my reasoning.  Substitute $a = [1,2,3] and $b = [4,5,6].  Those
are list I<references>, after all.  So now it becomes:

    $a »+« $b

That might just be okay, since they're both listrefs, and you shouldn't
expect a vector on two scalars to do much besides dereference its
arguments.  But now, instead of $a, use the real list (1,2,3):

    (1,2,3) »+« $b

That looks extremely different from before.  That looks like it's adding
$b to each of (1,2,3).  Not only that, but say you have:

    $x »+« $y

$x is a number, and $y is a listref.  Extrapolating from before, you'd
think that this should add $x to each of $y's elements.  But this is
starting to feel like run-time DWIMmery, which is almost always a Bad
Idea (favoring syntactic DWIMmery).

So I'm going to argue that:

    [1,2,3] »+« [4,5,6]

either give an error because you can't add listrefs, or give a "useless
use of vector operation on two scalars" error.  And if you want what
we originally thought, use:

    (1,2,3) »+« (4,5,6)
    @$a »+« @$b
    $x »+« @$y

Luke

à


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About