RE: List.partition should be more lazy
It was wrong in the Haskell report too (now fixed; see my home page). Also fixed in GHC 5.02. Simon | -----Original Message----- | From: Bastiaan Heeren [mailto:bastiaan@cs.uu.nl] | Sent: 23 November 2001 13:55 | To: haskell@haskell.org | Subject: List.partition should be more lazy | | | Using the function partition from the List module, a control | stack overflow occurs when evaluating the following expression: | | | List> head $ fst $ partition even [1..] | | (35929 reductions, 63867 cells) | ERROR: Control stack overflow | | | The definition of partition in both hugs and GHC libraries is: | | | partition :: (a -> Bool) -> [a] -> ([a],[a]) | partition p xs = foldr select ([],[]) xs | where select x (ts,fs) | p x = (x:ts,fs) | | otherwise = (ts,x:fs) | | | The helper-function select is strict in its second argument | because of the pattern match on the tuple (ts,fs). Making the | pattern match lazy by adding a ~ solves the problem! | | | partition :: (a -> Bool) -> [a] -> ([a],[a]) | partition p xs = foldr select ([],[]) xs | where select x ~(ts,fs) | p x = (x:ts,fs) | | otherwise = (ts,x:fs) | | | Cheers, | Bastiaan | | | _______________________________________________ | Haskell mailing list | Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell |
participants (1)
-
Simon Peyton-Jones