Commit d41dd03f authored by David Feuer's avatar David Feuer Committed by Joachim Breitner
Browse files

Make mapAccumL a good consumer

This fixes #9502.
parent 4e1dfc37
...@@ -481,11 +481,28 @@ mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list ...@@ -481,11 +481,28 @@ mapAccumL :: (acc -> x -> (acc, y)) -- Function of elt of input list
-> acc -- Initial accumulator -> acc -- Initial accumulator
-> [x] -- Input list -> [x] -- Input list
-> (acc, [y]) -- Final accumulator and result list -> (acc, [y]) -- Final accumulator and result list
{-# NOINLINE [1] mapAccumL #-}
mapAccumL _ s [] = (s, []) mapAccumL _ s [] = (s, [])
mapAccumL f s (x:xs) = (s'',y:ys) mapAccumL f s (x:xs) = (s'',y:ys)
where (s', y ) = f s x where (s', y ) = f s x
(s'',ys) = mapAccumL f s' xs (s'',ys) = mapAccumL f s' xs
{-# RULES
"mapAccumL" [~1] forall f s xs . mapAccumL f s xs = foldr (mapAccumLF f) pairWithNil xs s
"mapAccumLList" [1] forall f s xs . foldr (mapAccumLF f) pairWithNil xs s = mapAccumL f s xs
#-}
pairWithNil :: acc -> (acc, [y])
{-# INLINE [0] pairWithNil #-}
pairWithNil x = (x, [])
mapAccumLF :: (acc -> x -> (acc, y)) -> x -> (acc -> (acc, [y])) -> acc -> (acc, [y])
{-# INLINE [0] mapAccumLF #-}
mapAccumLF f = \x r s -> let (s', y) = f s x
(s'', ys) = r s'
in (s'', y:ys)
-- | The 'mapAccumR' function behaves like a combination of 'map' and -- | The 'mapAccumR' function behaves like a combination of 'map' and
-- 'foldr'; it applies a function to each element of a list, passing -- 'foldr'; it applies a function to each element of a list, passing
-- an accumulating parameter from right to left, and returning a final -- an accumulating parameter from right to left, and returning a final
......
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
second argument, so that the fusion RULES for it do not change the second argument, so that the fusion RULES for it do not change the
semantics. (#9596) semantics. (#9596)
* `scanr` now takes part in list fusion (#9355) * `scanr` and `mapAccumL` now take part in list fusion (#9355, #9502)
## 4.7.0.1 *Jul 2014* ## 4.7.0.1 *Jul 2014*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment