develooper Front page | perl.fwp | Postings from March 2002

Sort is lazy?!? (as in Haskell)

Thread Next
From:
Peter Makholm
Date:
March 3, 2002 06:28
Subject:
Sort is lazy?!? (as in Haskell)
Message ID:
87zo1pbuaa.fsf@xyzzy.adsl.dk
Trying to do completly stupid stuff with perl I tried to do the
following:

xyzzy% perl -le 'sort {print "a:$a\tb:$b"; $a cmp $b} qw(5 2 3 4 1)' 
xyzzy%

Somehow the print statement isn't evaled. Playing a little more around
I tried the following:

xyzzy% perl -le '(sort {print "a:$a\tb:$b"; $a cmp $b} qw(5 2 3 4 1))'
xyzzy% perl -le '(sort {print "a:$a\tb:$b"; $a cmp $b} qw(5 2 3 4 1))[3]'
a:2	b:5
a:3	b:5
a:3	b:2
a:4	b:5
a:4	b:3
a:1	b:5
a:1	b:4
a:1	b:3
a:1	b:2
xyzzy% perl -le 'print sort {print "a:$a\tb:$b"; $a cmp $b} qw(5 2 3 4 1)'     
a:2	b:5
a:3	b:5
a:3	b:2
a:4	b:5
a:4	b:3
a:1	b:5
a:1	b:4
a:1	b:3
a:1	b:2
12345
xyzzy% perl -le 'print(sort {print "a:$a\tb:$b"; $a cmp $b} qw(5 2 3 4 1))'
a:2	b:5
a:3	b:5
a:3	b:2
a:4	b:5
a:4	b:3
a:1	b:5
a:1	b:4
a:1	b:3
a:1	b:2
12345
xyzzy% 

So the question is when does sort just ignores the sideeffects and
when is they evaled?


I have yet to find a usefull purpose for having side effects in the
sort statement but that is not the point here.

-- 
Emacs er det eneste moderne styresystem der ikke er multitr├ądet.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About