Proposal: Add functions to get consecutive elements to Data.List

Henning Thielemann lemming at henning-thielemann.de
Wed Apr 13 08:47:10 UTC 2016


On Wed, 13 Apr 2016, David Feuer wrote:

> -1. I think these are good functions, but I don't think base is the place for them. They just don't seem "fundamental"
> enough. That said, they can be optimized for list fusion based on this implementation, and even use build to fuse the
> other way. No zipWith implementation will do this.
> 
> zipWithAdj f xs = foldr go (`seq` []) xs Nothing where
>   go x r Nothing = r (Just x)
>   go x r (Just prev) = f prev x : r (Just x)


Btw. I have also this more general variant:

mapAdjacent :: (Traversable f) => (a -> a -> b) -> NonEmpty f a -> f b
mapAdjacent f (NonEmpty x xs) =
    snd $ Trav.mapAccumL (\a0 a1 -> (a1, f a0 a1)) x xs


Maybe this is also better for fusion?


http://hackage.haskell.org/package/non-empty-0.2.1/docs/Data-NonEmpty.html#v:mapAdjacent


More information about the Libraries mailing list