Adding swap to Data.Tuple

roconnor at theorem.ca roconnor at theorem.ca
Wed Jun 17 12:11:50 EDT 2009


On Wed, 17 Jun 2009, Bertram Felgenhauer wrote:

> roconnor at theorem.ca wrote:
>> I have a new proposal to add swap and swap' to Data.Tuple
>
> I'd ask the opposite of Neil's question - what is a good use case for
> the extra laziness that swap provides over swap'? It seems to fill
> a rather small niche to me.

let foo x [] = (x,0); foo x (y:xs) = let !z = x + y in swap (foo z xs) in
    foo 0 [0..1000000]

The above runs fine while

let foo x [] = (x,0); foo x (y:xs) = let !z = x + y in swap' (foo z xs) in
    foo 0 [0..1000000]

causes a stack overflow in GHC.

> To provide the extra laziness, my favourite haskell compiler will have
> to create and later evaluate two thunks in addition to allocating a
> new pair.

I think we should be as lazy as possible in general, because lazier code 
supports more (data) fixpoint definitions than strict code.  Adding 
strictness is generally used as an optimization, and adding it as default 
would be prematurely optimize *everyone's* code.

> In other words, I'd rather add only one function,
>
>   swap (a, b) = (b, a)

-- 
Russell O'Connor                                      <http://r6.ca/>
``All talk about `theft,''' the general counsel of the American Graphophone
Company wrote, ``is the merest claptrap, for there exists no property in
ideas musical, literary or artistic, except as defined by statute.''


More information about the Libraries mailing list