Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • NEXT and the general loop statement

    6 answers - 272 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Is a NEXT clause called before or after the update portion of a general loop
    statement? For instance, consider the following code:
    loop $n = 0; $n < 5; ++$n {
    NEXT {print $n;}
    }
    Is the output 01234 or 12345?
    Joe Gottman
  • No.1 | | 608 bytes | |

    Wed, Aug 16, 2006 at 08:21:29PM -0400, Joe Gottman wrote:
    : Is a NEXT clause called before or after the update portion of a general loop
    : statement? For instance, consider the following code:
    :
    :
    :
    : loop $n = 0; $n < 5; ++$n {
    :
    : NEXT {print $n;}
    :
    : }
    :
    :
    :
    : Is the output 01234 or 12345?

    I'd say 01234 on the theory that the 3-arg loop is really saying:

    $n = 0;
    while $n < 5 {
    NEXT { ++$n }
    NEXT { print $n }
    }

    and also on the theory that block exiting blocks always run in reverse order.

    Larry
  • No.2 | | 990 bytes | |

    8/16/06, Larry Wall <larry (AT) wall (DOT) orgwrote:
    : Is the output 01234 or 12345?

    I'd say 01234 on the theory that the 3-arg loop is really saying:

    $n = 0;
    while $n < 5 {
    NEXT { ++$n }
    NEXT { print $n }
    }

    and also on the theory that block exiting blocks always run in reverse order.

    Wasn't NEXT supposed to do something tricky, such as being mutually
    exclusive with LAST? I remember a debate some time ago where some
    complained "but that would be hard to implement", and the solution
    being mostly correct but failing in this case.

    I seem to recall NEXT being created in order to do things like this:

    for @objs {
    .print;
    NEXT { print ", " }
    LAST { print "\n" }
    }

    We also might consider using perl6's hypothetical features to
    implement NEXT correctly in all cases (except for those cases where
    the loop update condition cannot be hypotheticalized).

    Luke
  • No.3 | | 1500 bytes | |

    Thu, Aug 17, 2006 at 11:45:06PM -0400, Joe Gottman wrote:
    Message
    From: Luke Palmer [mailto:lrpalmer (AT) gmail (DOT) com]
    Sent: Thursday, August 17, 2006 8:44 PM
    To: Perl6 Language List

    Wasn't NEXT supposed to do something tricky, such as being mutually
    exclusive with LAST? I remember a debate some time ago where some
    complained "but that would be hard to implement", and the solution
    being mostly correct but failing in this case.

    I seem to recall NEXT being created in order to do things like this:

    for @objs {
    .print;
    NEXT { print ", " }
    LAST { print "\n" }
    }

    We also might consider using perl6's hypothetical features to
    implement NEXT correctly in all cases (except for those cases where
    the loop update condition cannot be hypotheticalized).

    Is this even possible? This would require Perl to know which iteration is
    going to be the last one. In many cases there is no way to know this:

    repeat {
    $num = rand;
    print $num;
    NEXT {print ',';}
    LAST {print "\n";}
    } while $num < 0.9;

    If rand is a true random-number generator it would take a time machine to
    determine whether to call NEXT or LAST.
    (Sorry for the double post.)

    Depends on when it fires I guess. Your example might be equivalent to
    this perl5ish:

    while (1) {
    $num = rand;
    print $num;
    last if $num < 0.9;
    print ","; # NEXT
    }
    print "\n"; # LAST
    -Scott
  • No.4 | | 1676 bytes | |

    8/17/06, Jonathan Scott Duff <duff (AT) pobox (DOT) comwrote:
    Depends on when it fires I guess. Your example might be equivalent to
    this perl5ish:

    while (1) {
    $num = rand;
    print $num;
    last if $num < 0.9;
    print ","; # NEXT
    }
    print "\n"; # LAST

    Which, incidentally, relates back to the discussion at hand. If this
    were the case, though, the state of the variables (in three-arg for
    loops and side-effecty while loops) would seem to reflect the state of
    the next iteration from the rest of the code. It would also
    (obviously) fire after user input for eg. a for =<loop.

    So, unless the next paragraph is feasible, I think NEXT ought to be
    equivalent to LEAVE, perhaps with the exception of exceptional
    circumstances.

    But I was talking about hypotheticalization. That is, unless I am
    mistaken we have temp {} and let {} using UND blocks (which are given
    default definitions where possible). So perhaps we could use that
    feature to provide a NEXT trait which actually executes only if there
    is a next iteration, while giving the block the impression that it is
    executing in the previous iteration. This, of course, would not work
    for loops whose conditions have irreversible side-effects.

    , it might be feasible in some circumstances, but it may not be
    worth it. Its implementation (and usage caveats) are quite complex
    for a relatively minor convenience feature. For a user to implement
    its effect by himself, using the extended knowledge he has of the loop
    semantics, would probably not take more than four extra lines in the
    worst case.

    Luke
  • No.5 | | 2355 bytes | |

    Fri, Aug 18, 2006 at 01:44:35AM -0600, Luke Palmer wrote:
    : 8/17/06, Jonathan Scott Duff <duff (AT) pobox (DOT) comwrote:
    : >Depends on when it fires I guess. Your example might be equivalent to
    : >this perl5ish:
    : >
    : while (1) {
    : $num = rand;
    : print $num;
    : last if $num < 0.9;
    : print ","; # NEXT
    : }
    : print "\n"; # LAST
    :
    : Which, incidentally, relates back to the discussion at hand. If this
    : were the case, though, the state of the variables (in three-arg for
    : loops and side-effecty while loops) would seem to reflect the state of
    : the next iteration from the rest of the code. It would also
    : (obviously) fire after user input for eg. a for =<loop.
    :
    : So, unless the next paragraph is feasible, I think NEXT ought to be
    : equivalent to LEAVE, perhaps with the exception of exceptional
    : circumstances.

    I think it is equivalent to LEAVE, unless you've explicitly opted out
    with "last" or an exception.

    : But I was talking about hypotheticalization. That is, unless I am
    : mistaken we have temp {} and let {} using UND blocks (which are given
    : default definitions where possible). So perhaps we could use that
    : feature to provide a NEXT trait which actually executes only if there
    : is a next iteration, while giving the block the impression that it is
    : executing in the previous iteration. This, of course, would not work
    : for loops whose conditions have irreversible side-effects.
    :
    : , it might be feasible in some circumstances, but it may not be
    : worth it. Its implementation (and usage caveats) are quite complex
    : for a relatively minor convenience feature. For a user to implement
    : its effect by himself, using the extended knowledge he has of the loop
    : semantics, would probably not take more than four extra lines in the
    : worst case.

    It would mean that we can't use NEXT to replace Perl 5's "continue"
    blocks, for instance, since those are required to run before the
    while loop's condition is tested. Which also basically means we
    couldn't rewrite loop (;;) using NEXT. So I think NEXT should just
    be equivalent to LEAVE from the viewpoint of conditionals outside
    the loop body. You have to use "last" to do better.

    Larry
  • No.6 | | 945 bytes | |

    8/17/06, Larry Wall <larry (AT) wall (DOT) orgwrote:

    I'd say 01234 on the theory that the 3-arg loop is really saying:

    $n = 0;
    while $n < 5 {
    NEXT { ++$n }
    NEXT { print $n }
    }

    and also on the theory that block exiting blocks always run in reverse order.

    Dear Larry, I think it's important enough to explicitly express this
    rule in S04 after the following IRC conversation with Audrey:

    <agentzhaudreyt: do multiple NEXT {} run in the reversed order?
    <audreytI don't know, actually.
    <audreytI don't have a clear intuision of what makes sense.
    <agentzhlarry once said on p6l that quitting blocks should run in
    reversed order.
    <audreytI read that too. thing is, is NEXT quitting or entering
    <audreytI can argue either way eloquently, which means it's best deferred to
    TimToady, sorry :)

    Cheers,
    Agent

Re: NEXT and the general loop statement


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 9 =
QUESTION ON "Perl"

EMSDN.COM