#3709: Data.Either.partitionEithers is not lazy enough
----------------------------------+-----------------------------------------
    Reporter:  daniel.is.fischer  |       Owner:                
        Type:  bug                |      Status:  new           
    Priority:  normal             |   Component:  libraries/base
     Version:  6.10.4             |    Keywords:                
          Os:  Unknown/Multiple   |    Testcase:                
Architecture:  Unknown/Multiple   |     Failure:  None/Unknown  
----------------------------------+-----------------------------------------
 Data.Either.partitionEithers gives a stack overflow for long or infinite
 lists.

 Change
 {{{
 partitionEithers :: [Either a b] -> ([a],[b])
 partitionEithers = foldr (either left right) ([],[])
  where
   left  a (l, r) = (a:l, r)
   right a (l, r) = (l, a:r)
 }}}
 to
 {{{
 partitionEithers :: [Either a b] -> ([a],[b])
 partitionEithers = foldr (either left right) ([],[])
  where
   left  a ~(l, r) = (a:l, r)
   right a ~(l, r) = (l, a:r)
 }}}
 to make it lazy enough.

 Example:
 {{{
 Data.Either> let fun k = case gcd k 15 of { 3 -> Left "Fizz"; 5 -> Left
 "Buzz"; 15 -> Left "FizzBuzz"; _ -> Right k }
 Data.Either> take 10 . snd . partitionEithers $ map fun [1 .. 2*10^6 ::
 Integer]
 *** Exception: stack overflow
 Data.Either> take 10 . snd . lpartitionEithers $ map fun [1 :: Integer ..
 ]
 [1,2,4,7,8,11,13,14,16,17]
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3709>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to