[Haskell-cafe] Inline makes program slow?
Tom Ellis
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Thu Apr 3 16:48:42 UTC 2014
On Thu, Apr 03, 2014 at 04:21:38PM +0000, Simon Peyton Jones wrote:
> Dominick's program is very delicate:
>
> fibonacci :: (Integral a) => [a]
> fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci)
>
> Remember, the "(Integral a) =>" thing is very like "Integral a ->"; it's
> an extra value argument. Would you expect this program to be fast?
>
> foo :: Int -> [Int]
> foo n = 0 : 1 : zipWith (+) (foo n) (tail (foo n))
Indeed. The curious may be interested to know you can fix this by hand by
sharing a monomorphic bind
fibonacci = let f = fibonacci in 0 : 1 : zipWith (+) f (tail f)
unless the dreaded monomorphism restriction is off, in which case you have
to find some other way of making 'f' monomorphic, for example with
ScopedTypeVariables:
fibonacci = let f = fibonacci :: [a] in 0 : 1 : zipWith (+) f (tail f)
(I guess examples like this are exactly the reason the monomorphism
restriction exists).
Tom
More information about the Haskell-Cafe
mailing list