Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Lazy evaluation?

Reply
Thread Tools

Lazy evaluation?

 
 
Rob Hoelz
Guest
Posts: n/a
 
      02-14-2007
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!
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-14-2007
>>>>> "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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lazy evaluation is sometimes too lazy... help please. Ken Pu Python 3 01-16-2009 11:23 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:46 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:37 AM
hello! first post to clr. I'm asking about an attempt at a lazy rubysolution to computing fibonacci numbers for a project euler problem. seems tobe a bug in lazy ruby... tphyahoo Ruby 6 08-08-2008 08:15 PM
Reg Ex Help for a Lazy VB Programmer adams114@comcast.net Perl 6 04-21-2004 08:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57