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

Re: Sort is lazy?!? (as in Haskell)

Thread Previous | Thread Next
From:
Bernie Cosell
Date:
July 19, 2002 04:41
Subject:
Re: Sort is lazy?!? (as in Haskell)
Message ID:
3D37C2FD.15936.BCFE7306@localhost
On 18 Jul 2002 at 23:39, belg4mit wrote:

> Heh I just came across this today. It's an interesting optimization,
> but does it occur that often? It also precludes the possiblity of action at a
> distance / more complicated sort blocks/subroutines doing more than just
> sorting...
> 
> Peter Makholm wrote:
> > 
> > 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

 [...]

What's the trick here --- is it that the sort isn't done *at*all* [it isn't 
that the side-effects aren't happening, but that the *SORT* isn't happening]?
If so, that's pretty interesting --- how many other actions/operators just "go 
away" when evaluated in a void context ...  Does a map in void context actually 
happen?

  /Bernie\
-- 
Bernie Cosell                     Fantasy Farm Fibers
mailto:bernie@fantasyfarm.com     Pearisburg, VA
    -->  Too many people, too few sheep  <--          


Thread Previous | 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