Data.List.partition on infinite lists

Josef Svenningsson josefs at cs.chalmers.se
Mon Nov 1 05:40:15 EST 2004


This mail pretty much sums up the situation:
http://www.haskell.org/pipermail/glasgow-haskell-bugs/2004-October/004389.ht
ml

This will be fixed in GHC 6.4.

	/Josef

> -----Original Message-----
> From: libraries-bounces at haskell.org [mailto:libraries-bounces at haskell.org]
> On Behalf Of Lemming
> Sent: den 31 oktober 2004 18:37
> To: libraries at haskell.org
> Subject: Data.List.partition on infinite lists
> 
> I encountered that the implementation of 'partition' in GHC 6.2.1 fails
> on infinite lists:
> 
> > partition :: (a -> Bool) -> [a] -> ([a],[a])
> > partition p xs = foldr (select p) ([],[]) xs
> 
> > select p x (ts,fs) | p x       = (x:ts,fs)
> >                    | otherwise = (ts, x:fs)
> 
> 
> With the following definition we don't have this problem:
> 
> > partition :: (a -> Bool) -> [a] -> ([a], [a])
> > partition _ [] = ([],[])
> > partition p (x:xs) =
> >    let (y,z) = partition p xs
> >    in  if p x then (x : y, z)
> >               else (y, x : z)
> 
> 
> Prelude> take 10 $ fst $ partition even [0..]
> [0,2,4,6,8,10,12,14,16,18]
> Prelude> take 10 $ snd $ partition even [0..]
> [1,3,5,7,9,11,13,15,17,19]
> 
> 
> 
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries



More information about the Libraries mailing list