[Haskell-cafe] Building a tree?

wren ng thornton wren at freegeek.org
Wed Jun 10 20:23:08 EDT 2009


michael rice wrote:
> Here's a function from Data.Tree:
> 
> unfoldTree :: (b -> (a, [b])) -> b -> Tree a
> Build a tree from a seed value
> 
> Could someone please give me a brief example of usage.


In as far as understanding it, it may be easier if you look at the 
generalized version of the anamorphism:


     newtype Fix f = Fix { unFix :: f (Fix f) }

     type Tree a = Fix (Node a)

     data Node a b = Node a [b]

     instance Functor (Node a) where
         fmap f (Node a bs) = Node a (map f bs)



     unfoldTree f = Fix . fmap (unfoldTree f) . f


-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list