Commit c2feee5d authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

SumBag: Implement conversion to and from lists

This is used in the Eq instance and also makes debugging and testing easier.
parent fb7a40c2
......@@ -8,6 +8,8 @@ module Data.SumBag
, insert
, delete
, elem
, toAscList
, fromList
) where
import Prelude hiding (sum, min, elem)
......@@ -79,6 +81,17 @@ delete a (Node _ e left right)
let (min, rest) = delmin right
in balance1 min left rest
toAscList :: Ord a => SumBag a -> [a]
toAscList bag = helper bag []
where helper Leaf accu = accu
helper (Node _ e left right) accu =
helper left (mkList e ++ helper right accu)
mkList (Element val mult) = map (const val) (NE.toList mult)
fromList :: (Ord a, Monoid a) => [a] -> SumBag a
fromList = foldr insert empty
-- Internal functions
-- | "Smart" constructor for Node. Will compute the meta data from its subtrees
......
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