develooper Front page | perl.perl6.language | Postings from August 2005

Re: zip with ()

Thread Previous | Thread Next
August 4, 2005 02:50
Re: zip with ()
Message ID:

Luke Palmer wrote:
> On 8/1/05, Ingo Blechschmidt <> wrote:
>>In general, (@foo, @bar) returns a new list with the element joined,
>>i.e. "@foo.concat(@bar)". If you want to create a list with two sublists,
>>you've to use (\@foo, \@bar) or ([@foo], [@bar]). But of course, I could
>>be totally wrong. :)
> I think that's right.  However, it might be a good idea not to
> auto-enreference such bare lists:

I don't like this notion of auto enreference/dereference at all.
Either the type system manages to dispatch things correctly or
you get an error. For the List versus Array problem this dispatch
is IMHO decideable at compile time by strictly typing @ vars as
Array and subtypes thereof. The List type to me is a Code subtype
and as such travels in & vars. I'm only unsure how easily such
variables should enter name space, that is how they behave without

>     sub foo ($x) {...}
>     foo (1,2,3,4,5);   # foo gets called with [1,2,3,4,5]

Yes, the $x makes foo an Item or even Value taker of arity 1.
The call foo (1,2,3,4,5) OTOH calls it with a List. This should
result in a type error. But sub foo (&x) might then work for a List
but not for an Item|Value call foo(1).

Since I think that * in a signature is for extending the arity of
the sub to infinity I wonder if it is possible to capture the caller's
list into a single *$arg?

    sub foo (*$x) {...}
    foo (1,2,3,4,5); # type of $x is now Ref of List of Int?

But with an additional array the slurpy item gets at most one value.

    sub foo (*$x, *@a) {...}
    foo (1,2,3,4,5); # $x == 1; +@a == 4
    foo @array; # type of $x is now Ref of Array; @a is undef

> When you could just as easily have said:
>     foo [1,2,3,4,5];
> And we'll probably catch a lot of Perl 5 switchers that way.  That
> actually makes a lot of sense to me.  The statement:
>     my $x = (1,2,3,4,5);
> Looks like an error more than anything else.

Yep. I opt for type error "Can't assign List to Item".
By the same token I would disallow

   my @a = 3; # type error "Can't assign Item to Array".

It should be

   my @a = *3;


   my @a = (3,);

Hmm, wasn't there a nullary *?

   my @a = *;
   say +@a;  # prints 0

>  That's the "scalar
> comma", which has been specified to return a list.  But maybe it
> should be an error.

Sorry, I don't understand this.  I thought comma just is *the*
List constructor per se. Parens required to lift precedence :)
Same applies to semi-colon. (1,2,3;4,5,6) is a List of List of Int.

>  The main reason that we've kept a scalar comma is
> for:
>     loop (my $x = 0, my $y = 0; $x*$y <= 16; $x++, $y++)
>     {...}
> However, I think we can afford to hack around that.  Make the first
> and last arguments to loop take lists and just throw them away.

My interpretation of the loop block controler special form is that
it gets a 4-tupel (Block,Block,Block,Block). The last one is of course
the loop's body. The first is the initializer that is executed in a
scope outside the body. The second and third are the condition and
the stepper and also scoped outside the body.

Now to the comma. It should be parsed as List of Block. In your example
the argument type of loop is (List of Block,Block,List of Block,Block).
The loop instanciates an Iterator[List of Block] and uses it to
execute the Blocks one at a time. The only special case is in the
condition which evaluates only the last Block from the List of Block
for truth and the others in Void context.

Is loop supposed to be a topicalizer? Does it bind the block owner?
Does a pointy body block make sense?

    loop (my $x = 0; $x < 10; $x++) -> {...}  # current count in $_?
    loop (my $x = 0; $x < 10; $x++)    {...}  # $_ unchanged from outside?

    loop (my $x = 0; $x < 10; $x++)
        .blubber   # what is the invocant?

Can the last Block also be separated with semi-colon? I guess not.
How about a Code var?

   loop my $x = 0; $x < 10; $x++; say $x;  # works?

   loop my $x = 0; $x < 10; $x++; &foo; # works?

   loop( my $x = 0; $x < 10; $x++; &foo ) # perhaps as function call?

      my $x = 0;
      $x < 10;

   say $x; # still the loop body? Or does it need { say $x }?

   loop foo; bar; blubb ->
       say  # prints return value of blubb while bar returns true
            # first iteration prints return value of foo

>  Can
> anyone think of any other common uses of the scalar comma?

Not me. It's a C relict.
$TSa.greeting := "HaloO"; # mind the echo!

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About