Lazy evaluation?

Discussion in 'Perl Misc' started by Rob Hoelz, Feb 14, 2007.

  1. Rob Hoelz

    Rob Hoelz Guest

    In a statement like this:

    local $\ = "\n";
    foreach $num (1..1000_000_000) {
    print $num;
    }

    or this:

    local $\ = "\n";
    foreach $line (<$handle>) {
    chomp $line;
    print $line;
    }

    does Perl use lazy evaluation? Thanks!
    Rob Hoelz, Feb 14, 2007
    #1
    1. Advertising

  2. Rob Hoelz wrote:
    >
    > does Perl use lazy evaluation?


    AFAIK no it does not.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Feb 14, 2007
    #2
    1. Advertising

  3. Rob Hoelz

    Mirco Wahab Guest

    Michele Dondi wrote:
    > On Tue, 13 Feb 2007 20:14:13 -0600, Rob Hoelz <> wrote:
    >> local $\ = "\n";
    >> foreach $num (1..1000_000_000) {
    >> print $num;
    >> }

    >
    > I seem to remember that this particular one is optimized, but I'm not
    > really sure if up to the point of lazy evaluation.


    The above (foreach list) *is* lazy evaluated (at least
    in my in 5.8.8/Linux), whereas the list in

    print map { exit } (1..1000_000_000);

    is not (panic: realloc at lazy.pl al line ##).

    Regards

    Mirco
    Mirco Wahab, Feb 14, 2007
    #3
  4. Mirco Wahab wrote:
    > Michele Dondi wrote:
    >> On Tue, 13 Feb 2007 20:14:13 -0600, Rob Hoelz <> wrote:
    >>> local $\ = "\n";
    >>> foreach $num (1..1000_000_000) {
    >>> print $num;
    >>> }

    >>
    >> I seem to remember that this particular one is optimized, but I'm not
    >> really sure if up to the point of lazy evaluation.

    >
    > The above (foreach list) *is* lazy evaluated (at least
    > in my in 5.8.8/Linux), whereas the list in


    That is not lazy evaluation. Perl is just internally evaluating:

    foreach $num (1..1000_000_000) {
    print $num;
    }

    As:

    for ( $num = 1; $num <= 1000_000_000; ++$num ) {
    print $num;
    }


    Lazy evaluation would imply that the assignment to $num is delayed until it is
    actually required for the print() function which is not what perl does.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Feb 14, 2007
    #4
  5. Rob Hoelz

    Uri Guttman Guest

    >>>>> "b" == bugbear <bugbear@trim_papermule.co.uk_trim> writes:

    b> John W. Krahn wrote:
    >> That is not lazy evaluation. Perl is just internally evaluating:
    >> foreach $num (1..1000_000_000) {
    >> print $num;
    >> }
    >> As:
    >> for ( $num = 1; $num <= 1000_000_000; ++$num ) {
    >> print $num;
    >> }


    b> You mean the entire construct is recognised and
    b> handled as a complete "idiom", as opposed
    b> to a more general evaluation model of ".." ?

    it is easy to optimize (or make pseudo lazy) a .. in a for loop. all you
    have to do is change how that is code generated for that special (and
    common) case. passing a large .. to map is a very different story as map
    expects a full list it can use. it would require more complex
    recognition of a .. (and nothing else) being passed to map and a
    different version of map that will do internal iteration of the .. vs
    iterating over its standard input list. so they didn't try to optimize
    ... in the general case as it would need work on every list consuming
    operator or function. and as someone said real lazy eval is the norm (in
    all cases!) in p6.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 14, 2007
    #5
  6. Rob Hoelz

    Guest

    Rob Hoelz <> wrote:
    > In a statement like this:
    >
    > local $\ = "\n";
    > foreach $num (1..1000_000_000) {
    > print $num;
    > }
    >
    > or this:
    >
    > local $\ = "\n";
    > foreach $line (<$handle>) {
    > chomp $line;
    > print $line;
    > }
    >
    > does Perl use lazy evaluation? Thanks!


    First one yes, second one no. (For the second, use while instead)

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Feb 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    324
  2. sturlamolden
    Replies:
    9
    Views:
    741
    Antoon Pardon
    May 3, 2007
  3. Ken Pu
    Replies:
    3
    Views:
    669
    Steven D'Aprano
    Jan 16, 2009
  4. Boris Borcic
    Replies:
    0
    Views:
    547
    Boris Borcic
    Jan 16, 2009
  5. Boris Borcic
    Replies:
    0
    Views:
    545
    Boris Borcic
    Jan 16, 2009
Loading...

Share This Page