Jeff Polakow wrote:
[...] This can be easily fixed by defining a suitable strict sum:
sum' = foldl' (+) 0
and now sum' has constant space. We could try to redefine mean using
sum':
mean1 xs = sum' xs / fromIntegral (length xs)
but this still gobbles up memory. The reason is that xs is used twice
and cannot be discarded as it is generated.
As an experiment I tried using "pointfree" to see if it would do
something similar.
> $ pointfree "\xs -> foldl' (+) 0 xs / fromIntegral (length xs)"
> ap ((/) . foldl' (+) 0) (fromIntegral . length)
But when I try this in GHCi 6.8.2 I get:
> Prelude Data.List Control.Monad> let mean2 = ap ((/) . foldl' (+) 0)
(fromIntegral . length)
>
> <interactive>:1:12:
> No instance for (Monad ((->) [b]))
> arising from a use of `ap' at <interactive>:1:12-58
> Possible fix: add an instance declaration for (Monad ((->) [b]))
> In the expression: ap ((/) . foldl' (+) 0) (fromIntegral . length)
> In the definition of `mean2':
> mean2 = ap ((/) . foldl' (+) 0) (fromIntegral . length)
Any ideas? Would the auto-generated pointfree version be any better if
it could be made to work?
Paul.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe