Lazy evaluation?

R

Rob Hoelz

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!
 
M

Mirco Wahab

Michele said:
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
 
J

John W. Krahn

Mirco said:
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
 
U

Uri Guttman

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
 
X

xhoster

Rob Hoelz said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top