[Haskell-cafe] Paths to tree

Tomasz Zielonka tomasz.zielonka at gmail.com
Mon Jan 29 07:07:10 EST 2007

On Mon, Jan 29, 2007 at 10:10:47PM +1100, John Ky wrote:
> I've written some code and was wondering if there was a better way to write
> it in terms of readability, brevity and/or efficiency.

This version of mergeForest2 should be more efficient. For even better
efficiency it should use Data.Sequence for efficient concatenation
(instead of ++). I also made it more general. You have to judge
readability yourself.

    import qualified Data.Map as Map

    data ArcData = ArcData
     { name :: String
     } deriving (Show, Eq, Ord) -- derive Ord and Eq

    mergeForest2 :: (Ord k) => [Tree k] -> Forest k
    mergeForest2 =
        map pairToNode .
        Map.toList .
        Map.map mergeForest2 .
        Map.fromListWith (++) .
        map nodeToPair
        nodeToPair (Node x y) = (x, y)
        pairToNode = uncurry Node

Best regards

More information about the Haskell-Cafe mailing list