@@ -34,18 +34,14 @@ module Data.Tree(
3434 unfoldTreeM_BF , unfoldForestM_BF ,
3535 ) where
3636
37- #if MIN_VERSION_base(4,8,0)
38- import Data.Foldable (toList )
39- #else
37+ #if !(MIN_VERSION_base(4,8,0))
4038import Control.Applicative (Applicative (.. ), (<$>) )
41- import Data.Foldable (Foldable (foldMap ) , toList )
39+ import Data.Foldable (Foldable (foldMap , toList ) )
4240import Data.Monoid (Monoid (.. ))
4341import Data.Traversable (Traversable (traverse ))
4442#endif
4543
4644import Control.Monad (liftM )
47- import Data.Sequence (Seq , empty , singleton , (<|) , (|>) , fromList ,
48- ViewL (.. ), ViewR (.. ), viewl , viewr )
4945import Data.Typeable
5046import Control.DeepSeq (NFData (rnf ))
5147
@@ -163,37 +159,23 @@ unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
163159#endif
164160unfoldForestM f = Prelude. mapM (unfoldTreeM f)
165161
166- -- | Monadic tree builder, in breadth-first order,
167- -- using an algorithm adapted from
168- -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
169- -- by Chris Okasaki, /ICFP'00/.
162+ -- | Monadic tree builder, in breadth-first order.
170163unfoldTreeM_BF :: Monad m => (b -> m (a , [b ])) -> b -> m (Tree a )
171- unfoldTreeM_BF f b = liftM getElement $ unfoldForestQ f (singleton b)
172- where
173- getElement xs = case viewl xs of
174- x :< _ -> x
175- EmptyL -> error " unfoldTreeM_BF"
176-
177- -- | Monadic forest builder, in breadth-first order,
178- -- using an algorithm adapted from
179- -- /Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design/,
180- -- by Chris Okasaki, /ICFP'00/.
181- unfoldForestM_BF :: Monad m => (b -> m (a , [b ])) -> [b ] -> m (Forest a )
182- unfoldForestM_BF f = liftM toList . unfoldForestQ f . fromList
183-
184- -- takes a sequence (queue) of seeds
185- -- produces a sequence (reversed queue) of trees of the same length
186- unfoldForestQ :: Monad m => (b -> m (a , [b ])) -> Seq b -> m (Seq (Tree a ))
187- unfoldForestQ f aQ = case viewl aQ of
188- EmptyL -> return empty
189- a :< aQ' -> do
190- (b, as) <- f a
191- tQ <- unfoldForestQ f (Prelude. foldl (|>) aQ' as)
192- let (tQ', ts) = splitOnto [] as tQ
193- return (Node b ts <| tQ')
164+ unfoldTreeM_BF f b0 = do
165+ (a, bs) <- f b0
166+ Node a `liftM` unfoldForestM_BF f bs
167+
168+ -- | Monadic forest builder, in breadth-first order.
169+ unfoldForestM_BF :: Monad m
170+ => (b -> m (a , [b ])) -> [b ] -> m (Forest a )
171+ unfoldForestM_BF _f [] = return []
172+ unfoldForestM_BF f bs = do
173+ asbss' <- mapM f bs
174+ rebuild asbss' `liftM` unfoldForestM_BF f (concatMap snd asbss')
175+
194176 where
195- splitOnto :: [a' ] -> [ b' ] -> Seq a' -> ( Seq a' , [ a' ])
196- splitOnto as [] q = (q, as)
197- splitOnto as (_ : bs) q = case viewr q of
198- q' :> a -> splitOnto (a : as) bs q'
199- EmptyR -> error " unfoldForestQ "
177+ rebuild :: [( a , [ any ])] -> [ Tree a ] -> [ Tree a ]
178+ rebuild [] ts = ts
179+ rebuild ((a, bs') : xs) ts =
180+ case splitAt ( length bs') ts of
181+ (us, ts') -> Node a us : rebuild xs ts'
0 commit comments