Proposal: Add strict iterate'
David Feuer
david.feuer at gmail.com
Wed Aug 6 23:00:40 UTC 2014
On Wed, Aug 6, 2014 at 1:46 PM, Bas van Dijk <v.dijk.bas at gmail.com> wrote:
> As suggested[1] by Takano Akio a few years ago iterate can be made strict by
> applying headStrict to its result:
>
> headStrict :: [a] -> [a]
> headStrict = foldr (\x y -> seq x (x : y)) []
The version of iterate' that I gave fuses with foldr, once unfoldr is
fixed properly. The version above doesn't, with that
definition of headStrict, but we can write
headStrict :: [a] -> [a]
headStrict xs = build $ \c n ->
foldr (\x y -> seq x (x `c` y)) n xs
This does indeed work well in this case, and it looks like it's
probably safe to fuse in general. That seems like a
reasonable approach.
> We might want to add this to Data.List unless there are no use-cases for a
> lazy accumulator in iterate in which case I'm +1 for a strict
> implementation.
It's a bit hard for me to imagine why you'd need really need a lazy
iterate. I believe that essentially all it does is make things like
[1,2,3,undefined,undefined,...] instead of ones like 1:2:3:undefined.
Since it always produces an infinite list, there's nothing much you
can *do* with that spine.
More information about the Libraries
mailing list