category-theory-presentation/categories/30_How/300_Catamorphisms/080_morphism_extension_to_Trees.html
2013-02-28 16:49:12 +01:00

12 lines
420 B
HTML

<h2 id="κατα-morphism-extension-to-trees">κατα-morphism: extension to Trees</h2>
<p>Once you get the trick, it is easy to extent to most Functor.</p>
<pre><code>type Tree = Mu TreeF
data TreeF x = Node Int [x]
instance Functor TreeF where
fmap f (Node e xs) = Node e (fmap f xs)
depth = cata phi where
phi :: Algebra TreeF Int -- TreeF Int -&gt; Int
phi (Node x sons) = 1 + foldr max 0 sons</code></pre>