
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. Michael

On Wed, 10 Jun 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.
unfoldTree (\n -> (chr (n + ord 'a'), replicate n (n-1))) 3 Or did you expect a "real world" example? :-)

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.
Data.Tree> let t = unfoldTree (\i -> (show i, [i`div`2..i-1])) Data.Tree> putStr . drawTree $ t 8 -- Live well, ~wren

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
participants (3)
-
Henning Thielemann
-
michael rice
-
wren ng thornton