[Haskell-cafe] Trouble with `foldl` in composition chain.

David Banas dbanas at banasfamily.net
Mon Jun 27 04:08:28 CEST 2011


When profiling this code:

        consolidateRPR :: [Sample] -> [Sample]
        
        smplUnionRecursRev :: [Sample] -> Sample -> [Sample]
        
        sortSamps :: [Sample] -> [Sample]
        
        smplSetUnion :: [Sample] -> [Sample]
        smplSetUnion = consolidateRPR . (foldl smplUnionRecursRev []) . sortSamps
        
I'm getting this:

        smplSetUnion       Data.RandProc                                       1038       32794   0.7    0.2    76.9   72.6
         consolidateRPR    Data.RandProc                                       1041       32794   0.0    0.0     8.2   11.8
         smplUnionRecursRev Data.RandProc                                       1040      918112   8.8    5.6     8.8    5.6
         sortSamps         Data.RandProc                                       1039     1081514  21.1   55.0    59.2   55.0
        
I'm confused as to why `sortSamps` is being called so many times. I
would expect it to be called 32794 times, just like `consolidateRPR`.
It's as if having a `foldl` to the left of `sortSamps` in the
composition chain is causing it to execute more than once, per call of
`smplSetUnion`, but I wouldn't expect that.

This doesn't change anything:

        smplSetUnion = consolidateRPR . (\ss -> foldl smplUnionRecursRev [] ss) . sortSamps
        
(I thought it might explicitly close the `foldl`.)

In fact, even this:

        smplSetUnion ss = consolidateRPR $ foldl smplUnionRecursRev [] ss'
            where ss' = sortSamps ss
        
doesn't change anything. `sortSamps` still gets entered 1081514 times!

Can anyone help clear up this newbie's fog?

Thanks, in advance!
-db





More information about the Haskell-Cafe mailing list