{"id":6200,"date":"2021-03-23T06:00:01","date_gmt":"2021-03-23T04:00:01","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6200"},"modified":"2021-03-30T09:06:33","modified_gmt":"2021-03-30T07:06:33","slug":"minima-profundidad","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/minima-profundidad\/","title":{"rendered":"M\u00ednima profundidad"},"content":{"rendered":"<p>En la librer\u00eda <a href=\"http:\/\/bit.ly\/2mLEVg4\">Data.Tree<\/a> se definen los \u00e1rboles y los bosques como sigue<\/p>\n<pre lang=\"text\">\n   data Tree a   = Node a (Forest a)\n   type Forest a = [Tree a]\n<\/pre>\n<p>Por ejemplo, los \u00e1rboles<\/p>\n<pre lang=\"text\">\n     1               3\n    \/ \\             \/|\\\n   6   3           \/ | \\\n       |          5  4  7\n       1          |     \/\\\n                  3    6  5\n<\/pre>\n<p>se representan por<\/p>\n<pre lang=\"text\">\n   ej1, ej2 :: Tree Int\n   ej1 = Node 1 [Node 6 [],Node 3 [Node 1 []]]\n   ej2 = Node 3 [Node 5 [Node 3 []], Node 4 [], Node 7 [Node 6 [], Node 5 []]]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n    minimaProfundidad :: Ord a => a -> Tree a -> Maybe Int\n<\/pre>\n<p>tal que (minimaProfundidad x ns) es justamente la m\u00ednima  donde aparece x en el \u00e1rbol ns, si aparece y Nothing, en caso contrario. Por ejemplo,<\/p>\n<pre lang=\"text\">\n    minimaProfundidad 1 ej1  ==  Just 0\n    minimaProfundidad 3 ej1  ==  Just 1\n    minimaProfundidad 2 ej1  ==  Nothing\n    minimaProfundidad 3 ej2  ==  Just 0\n    minimaProfundidad 4 ej2  ==  Just 1\n    minimaProfundidad 6 ej2  ==  Just 2\n    minimaProfundidad 9 ej2  ==  Nothing\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Tree (Tree (Node), levels)\nimport Data.Maybe (isNothing, fromJust, listToMaybe)\n\nej1, ej2 :: Tree Int\nej1 = Node 1 [Node 6 [],Node 3 [Node 1 []]]\nej2 = Node 3 [Node 5 [Node 3 []], Node 4 [], Node 7 [Node 6 [], Node 5 []]]\n\n-- 1\u00aa definici\u00f3n\nminimaProfundidad1 :: Ord a => a -> Tree a -> Maybe Int\nminimaProfundidad1 x (Node y ns)\n  | x == y    = Just 0\n  | null zs   = Nothing\n  | otherwise = Just (1 + minimum zs)\n  where zs = [z | Just z <- filter (\/=Nothing) (map (minimaProfundidad1 x) ns)]\n\n-- 2\u00aa definici\u00f3n\nminimaProfundidad2 :: Ord a => a -> Tree a -> Maybe Int\nminimaProfundidad2 x (Node y ns)\n  | x == y       = Just 0\n  | z == Nothing = Nothing\n  | otherwise    = Just (1 + fromJust z)\n  where z = minimum (map (minimaProfundidad2 x) ns)\n\n-- 3\u00aa definici\u00f3n\nminimaProfundidad3 :: Ord a => a -> Tree a -> Maybe Int\nminimaProfundidad3 x (Node y ns)\n  | x == y       = Just 0\n  | otherwise    = Just (+1) <*> minimum (map (minimaProfundidad3 x) ns)\n\n-- 4\u00aa definici\u00f3n\nminimaProfundidad4 :: Ord a => a -> Tree a -> Maybe Int\nminimaProfundidad4 x ns\n  | null zs   = Nothing\n  | otherwise = Just (head zs)\n  where zs = [z | (z,ys) <- zip [0..] (levels ns), x `elem` ys]\n\n-- 5\u00aa definici\u00f3n\nminimaProfundidad5 :: Ord a => a -> Tree a -> Maybe Int\nminimaProfundidad5 x ns =\n  listToMaybe [z | (z,ys) <- zip [0..] (levels ns), x `elem` ys]\n<\/pre>\n<h4>Nuevas soluciones<\/h4>\n<ul>\n<li>En los comentarios se pueden escribir nuevas soluciones.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=&quot;haskell&quot;&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>En la librer\u00eda Data.Tree se definen los \u00e1rboles y los bosques como sigue data Tree a = Node a (Forest a) type Forest a = [Tree a] Por ejemplo, los \u00e1rboles 1 3 \/ \\ \/|\\ 6 3 \/ | \\ | 5 4 7 1 | \/\\ 3 6 5 se representan por ej1,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[2],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6200"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=6200"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6200\/revisions"}],"predecessor-version":[{"id":6245,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6200\/revisions\/6245"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}