Hello,
Many texts explain the following Fibonacci code:
fibs :: [Integer]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
But this code is very slow because evaluation of (+) is done
lazily. If we have the following strict zipWith', the code above
becomes much faster.
zipWith' f (a:as) (b:bs) = x `
Hi Kazu.
I'd be surprised if zipWith' yields significant improvements. In the
case of foldl', the strictness affects an internal value (the
accumulator). However, in the case of zipWith', you're just forcing
the result a bit more, but I guess the "normal" use pattern of fibs is
that you want to se
On Friday 01 February 2013, 12:50:18, Andres Löh wrote:
> Hi Kazu.
>
> I'd be surprised if zipWith' yields significant improvements. In the
> case of foldl', the strictness affects an internal value (the
> accumulator). However, in the case of zipWith', you're just forcing
> the result a bit more,
On Friday 01 February 2013, 13:06:09, Daniel Fischer wrote:
>
> zipWith' would [I haven't tested, but I'm rather confident] make a
> difference if you benchmarked
>
> bench "name" (whnf (fibs !!) 10)
>
> etc.
Well, it took a little bit of persuasion to let GHC not cache the list(s), but
> Well, it took a little bit of persuasion to let GHC not cache the list(s), but
> with
>
>
> fibs :: Int -> Integer
> fibs k = igo i !! k
> where
> i | k < 100 = 1
> | otherwise = 2
> igo :: Integer -> [Integer]
> igo i = let go = 0 : i : zipWith (+) go (tail go) in go
>
On Friday 01 February 2013, 13:43:59, Andres Löh wrote:
>
> Right, I'm not arguing that it's impossible to produce a difference,
> but I think that if you're defining the sequence of fibs, the most
> likely scenario might be that you're actually interested in a prefix,
Right. If you only want one
I recently asked a similar question about strict scans (e.g. scanl')
and got the same response to use a strictify function.
Although I would argue that fun' is syntactically more convenient than
(strictList . fun), I'd agree that composition is good.
Maybe it would make sense to add to have tha
Hi,
> zipWith' would [I haven't tested, but I'm rather confident] make a difference
> if
> you benchmarked
>
> bench "name" (whnf (fibs !!) 10)
>
> etc.
Yes. fibs is slow if used with "!!".
--Kazu
___
Haskell-Cafe mailing list
Haskell-Cafe
> Right, I'm not arguing that it's impossible to produce a difference,
> but I think that if you're defining the sequence of fibs, the most
> likely scenario might be that you're actually interested in a prefix,
> and more importantly, you can still, from the outside, force the
> prefix even if you