Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Control Structures II: loop

8 views
Skip to first unread message

Timothy S. Nelson

unread,
Nov 14, 2002, 3:37:51 PM11/14/02
to perl6-l...@perl.org
Here's the next part to the Control Structures message I sent before.

The next part is to apply the same idea to loop. Please note that
this syntax conflicts with stuff already in Perl, but it's a bit clearer what
I mean when I do it this way; the question is, do we scrap my idea, or the
other syntax? :)

I'll begin with a few words of explanation of what follows. First,
you normally wouldn't spread it out this much. Second, each line is
optional, except "loop" and { blockL }.

----------------------------------------------------------------------
loop
parallel
first { BLOCKF }
each [ actual ] [ $key [ => $value ] ] (@array|%hash)
while ( EXPR )
count [ $autocount ] [ ($start, $end, $step) ]
nest { BLOCKT }
{ BLOCKL }
next { BLOCKX }
all { BLOCKA }
any { BLOCKB }
some { BLOCKS }
none { BLOCKN }
----------------------------------------------------------------------

Anyway, in this one, the loop can function as anything. I'm calling
the each, while, and count lines "iterator sections".

each:
"each" takes top priority. If it's present, the loop goes around
@array.end times. The "actual" means that if you modify $_ (or $key, or
whatever), then it modifies the array element too.

while:
If while is present it gets evaluated each time the loop goes around.
It takes second priority to each. If there is no each present, then when the
while is false, the loop exists. If there's an each present, the status of
the while expr only affects the $truecount and $falsecount variables.

count:
The count section sets the automatic counter variable, and the
parameters of the count. $start defaults to 0. $end defaults to infinity.
$step also defaults to 1. If the count reaches $end, but there is a while or
an each, then the autocount stops incrementing, but the loop keeps going.

nest:
Nest is the power loop thingy documented in Raphael Finkel's top notch
book "Advanced Programming Language Design", near the end of the "Control
Structures" chapter -- this book is in PDF format:
http://www.nondot.org/sabre/Mirrored/AdvProgLangDesign/

post-loop structures:
The all|any|some|none set work exactly the same way as with the given
statement; they are based on the values of $truecount and $falsecount.

Anyway, I hope this makes sense.

:)

---------------------------------------------------------------------
| Name: Tim Nelson | Because the Creator is, |
| E-mail: way...@smartchat.net.au | I am |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.1
GCS d? s: a-- C++>++++$ US+ P++ L++ E- W+++ N+ w+> M-- V- Y+>++
PGP->++ R(+) !tv B++ DI++++ D+ G e>++ h!/* y-
-----END GEEK CODE BLOCK-----


Luke Palmer

unread,
Nov 14, 2002, 1:51:57 PM11/14/02
to way...@smartchat.net.au, perl6-l...@perl.org
> Mailing-List: contact perl6-lan...@perl.org; run by ezmlm
> Date: Fri, 15 Nov 2002 07:37:51 +1100 (EST)
> From: "Timothy S. Nelson" <way...@smartchat.net.au>
> Sender: way...@elphin.nelson.org.au
> X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/

>
> Here's the next part to the Control Structures message I sent before.
>
> The next part is to apply the same idea to loop. Please note that
> this syntax conflicts with stuff already in Perl, but it's a bit clearer what
> I mean when I do it this way; the question is, do we scrap my idea, or the
> other syntax? :)
>
> I'll begin with a few words of explanation of what follows. First,
> you normally wouldn't spread it out this much. Second, each line is
> optional, except "loop" and { blockL }.
>
> ----------------------------------------------------------------------
> loop
> parallel
> first { BLOCKF }
> each [ actual ] [ $key [ => $value ] ] (@array|%hash)
> while ( EXPR )
> count [ $autocount ] [ ($start, $end, $step) ]
> nest { BLOCKT }
> { BLOCKL }
> next { BLOCKX }
> all { BLOCKA }
> any { BLOCKB }
> some { BLOCKS }
> none { BLOCKN }
> ----------------------------------------------------------------------

Do you grok the current C<for> syntax? Do you know about the FIRST,
NEXT, and LAST blocks? If so, you'll easily see that your
million-and-a-half keywords are complicating what we already have.

for parallel(<>, 0..Inf) -> $line, $count {
FIRST { $line //= "#!/usr/bin/perl" }
# processing...
NEXT { print STDERR "Next line...\n" }
LAST { print STDERR "Done\n" }
}

That 'example' (ignoring the fact that examples usually have some
purpose ;) has all the useful stuff from your list, and IMO is more
comprehensible than a LISPish do-ish loop.

Also, keep in mind that that C<parallel> function can be any (possibly
user-defined) function. I really like that interface. Also keep in
mind that that may not be The Interface :( i.e. still under debate ).

The only thing it doesn't cover is C<nest>, which, in practical
situations, isn't all that useful anyway. It's cleaner just to nest
manually.

Luke

Timothy S. Nelson

unread,
Nov 15, 2002, 12:48:06 PM11/15/02
to Luke Palmer, perl6-l...@perl.org

Thanks for the comments about the FIRST, NEXT, and LAST blocks. I
knew about NEXT, but the other two I've only seen in answers to my recent
posts (including yours :) ).

> for parallel(<>, 0..Inf) -> $line, $count {
> FIRST { $line //= "#!/usr/bin/perl" }
> # processing...
> NEXT { print STDERR "Next line...\n" }
> LAST { print STDERR "Done\n" }
> }
>
> That 'example' (ignoring the fact that examples usually have some
> purpose ;) has all the useful stuff from your list, and IMO is more
> comprehensible than a LISPish do-ish loop.

Hmm. It's still missing the all/any/some/none part. After looking at
your example here, and a few others I found elsewhere when I went searching, I
modified my original plan. Here's something more like what I'm aiming at
(example now, instead of spec, so I've left out some of the bits I like):

for each actual $key => $value (%hash) while ( ($key + $value) < 3 )
count $autocount (40, 50, 0.2) {

carpe_jugulum($key, $autocount + $value);
LAST {
all { do_something(); }
any { do_something_else(); }
some { do_something_different(); }
none { do_something_the_same(); }
}
}

So, it loops for each element in %hash, evaluates the "while" clause
(modifying $truecount and $falsecount), sets up the $autocount variable, and
loops around the carpe_jugulum; when it's all over, it executes the
all/any/some/none in the LAST section depending on the values of $truecount
and $falsecount.

I'm not particularly attached to that syntax I just used -- more to
the concept behind it.

As for "LISPish", I never got the hang of LISP :).

> Also, keep in mind that that C<parallel> function can be any (possibly
> user-defined) function. I really like that interface. Also keep in
> mind that that may not be The Interface :( i.e. still under debate ).
>
> The only thing it doesn't cover is C<nest>, which, in practical
> situations, isn't all that useful anyway. It's cleaner just to nest
> manually.

As the Finkel book points out, that only works if you know ahead of
time how many layers deep you're going to be nesting. Personally, I don't
expect to need nest more than once or twice in my life, but it'd be useful for
the times it's needed.

Anyway, hope this explains what I'm going on about a bit more.

fear...@figaro.weizmann.ac.il

unread,
Nov 15, 2002, 8:13:21 AM11/15/02
to Luke Palmer, way...@smartchat.net.au, perl6-l...@perl.org
Luke Palmer writes:
>
> for parallel(<>, 0..Inf) -> $line, $count {
> FIRST { $line //= "#!/usr/bin/perl" }
> # processing...
> NEXT { print STDERR "Next line...\n" }
> LAST { print STDERR "Done\n" }
> }
>
....

>
> Also, keep in mind that that C<parallel> function can be any (possibly
> user-defined) function. I really like that interface. Also keep in
> mind that that may not be The Interface :( i.e. still under debate ).
>

is parallel ( or whatever I pu there ) *always* a lazy function or it
returns a lazy list . I dont exactly see the differnce in case of

parallel(<>, 0..Inf) .

but in more "earthly" example

@a = (1,2,3); @b = qw( a b c ) ;
for parallel(@a,@b) { $^x mumble $^y }

will parallel *first* evaluate to ( 1 , "a" , 2, "b" , ... "c" )
and then for "walk" it over -- or parallel lazyli "yeld" one value at
a time when for request .

what I am concerned with here is : will for - parallel pair be smart
enough to optimize away unnecessary ( and CPU - costly ) reshuffling
of @a,@b -> (@a[0], @b[0], @a[1], @b[1], ... ) . or , actually I am
talking about imaginary problem and this reshuffling happens in zero
time. ( But *I* always can write parallel-like function that will do
it *very* inefficiently . so what happens in this case )

since I feel that I am not *very* clear , just one question :

what is faster ( in perl6 ) ?

for parallel(@a,@b) { mumble $^x, $^y } # assuming parallel stop when
# one of the arrays is exhosted
or

my $i=0;
my $x,$y;
loop {
$x = @a[$i ] or last loop
$y = @a[$i++] or last loop
mumble $x , $y ;
}

arcadi

Timothy S. Nelson

unread,
Feb 12, 2005, 12:24:08 AM2/12/05
to Luke Palmer, perl6-l...@perl.org
On Thu, 14 Nov 2002, Luke Palmer wrote:

> > nest:
> > Nest is the power loop thingy documented in Raphael Finkel's top notch
> > book "Advanced Programming Language Design", near the end of the "Control
> > Structures" chapter -- this book is in PDF format:
> > http://www.nondot.org/sabre/Mirrored/AdvProgLangDesign/
>

> The only thing it doesn't cover is C<nest>, which, in practical
> situations, isn't all that useful anyway. It's cleaner just to nest
> manually.

In 2002, I said:
As the Finkel book points out, that only works if you know ahead of
time how many layers deep you're going to be nesting. Personally, I don't
expect to need nest more than once or twice in my life, but it'd be useful for
the times it's needed.

Now I add:
I knew there was a situation where these are useful, and I've found
it, and it'll probably be more frequent in Perl6 than Perl5: recursing through
multidimensional arrays where you don't know how many dimensions there are.
For example, nest would be great for implementing some of the APL operators.
Just fors the record:
Perl APL
Operators Functions
Hyper-operators Operators

http://www.info.univ-angers.fr/pub/gh/wAides/sax6_userdoc.pdf
Look under Language Guide/Operators.

:)


---------------------------------------------------------------------
| Name: Tim Nelson | Because the Creator is, |
| E-mail: way...@smartchat.net.au | I am |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+ s:- a- C++>++++$ U++ P++ L++ E- W+++ N+ w>--- V- Y+>++
PGP->++ R !tv b++ DI++++ D+ G e++>++++ h! y-
-----END GEEK CODE BLOCK-----

0 new messages