Re: List to hash/dict?



Mike Austin <no@xxxxxxxx> writes:

> I noticed the different styles of anonymous functions in your code and
> the one on the web page:
>
> sortBy (\(_,x) (_,y) -> compare x y)
>
> sortBy (\a b -> snd b `compare` snd a)
>
> Is it simply that, a style, or is there something deeper?

In this case I would say it's a style, but they are not technically
equivalent because Haskell is lazy.

In the first version the comparator evaluates both arguments (pairs)
and passes their second components to compare.

In the second version the comparator passes to compare two expressions
which, when evaluated, evaluate the arguments and extract their second
components.

The function 'compare' is dispatched basing on the type of values
being compared. If the the type of the second components is fixed in
this place of the program, or if this place is decided worth inlining
or specializing, then an optimizing compiler will notice that
comparison for this type is strict (i.e. always needs values of both
arguments) and generate the same code for both versions. Comparisons
are usually strict by their nature.

But if this call site is polymorphic, the compiler can't a priori
assume that 'compare' will evaluate both arguments, it's just a
function, so the comparator passes to 'compare' lazy thunks which
will evaluate arguments of the comparator and extract its second
components when they are themselves evaluated.

So if the comparison is indeed strict but the compiler doesn't
know the type, the first version might generate better code.
In an artificial case when it's not strict, the first version will
unnecessarily evaluate pairs which are not evaluated by the second
version and thus in theory be slower or fail to terminate.

--
__("< Marcin Kowalczyk
\__/ qrczak@xxxxxxxxxx
^^ http://qrnik.knm.org.pl/~qrczak/
.



Relevant Pages

  • Re: Net::LDAP compare question
    ... I added "use strict;" and found some defects. ... I think I am doing something wrong as far as the compare statement. ... go to the next entry. ...
    (comp.lang.perl.modules)
  • Re: Appending a character to a field
    ... use strict; ... close DFOUT; ... it's to truncate the percent sign % from the df -k output, compare the ...
    (perl.beginners)
  • Re: Checking on logical functionality
    ... The content of file1 is A&B while file 2 ... How can I compare and print that the functionality is same. ... use strict; use warnings; ... sub A ...
    (perl.beginners)
  • Re: SAT question
    ... jennifer wrote: ... Indeed that makes C less than A+B, but if you compare ... In a nondegenerate triangle, the strict ... inequality has to hold for all three sides. ...
    (sci.math)
  • Re: Need help with REDEFINES (I think)....
    ... preclude the compiler optimizing it. ... (a compiler generated subroutine) ... generated subroutine) because it uses a simple compare (or series of ... would be fairly consistent across platforms, but even if I'm wrong and there ...
    (comp.lang.cobol)