{"id":2804,"date":"2017-01-13T06:00:45","date_gmt":"2017-01-13T04:00:45","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2804"},"modified":"2017-01-20T08:51:19","modified_gmt":"2017-01-20T06:51:19","slug":"suma-ordenada-de-listas-infinitas-ordenadas","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/suma-ordenada-de-listas-infinitas-ordenadas\/","title":{"rendered":"Suma ordenada de listas infinitas ordenadas"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   sumaOrdenada :: [Integer] -> [Integer] -> [Integer]\n<\/pre>\n<p>tal que (sumaOrdenada xs ys) es la suma ordenada de las listas infinitas crecientes xs e ys. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> take 15 (sumaOrdenada [5,10..] [7,14..])\n   [12,17,19,22,24,26,27,29,31,32,33,34,36,37,38]\n   \u03bb> take 15 (sumaOrdenada [2^n | n <- [0..]] [3^n | n <- [0..]])\n   [2,3,4,5,7,9,10,11,13,17,19,25,28,29,31]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\n-- 1\u00aa definici\u00f3n\n-- =============\n\nsumaOrdenada1 :: [Integer] -> [Integer] -> [Integer]\nsumaOrdenada1 (x:xs) (y:ys) =\n  x+y : map (+x) ys `mezcla` map (+y) xs `mezcla` sumaOrdenada1 ys xs\n\nmezcla :: Ord a => [a] -> [a] -> [a]\nmezcla (x:xs) (y:ys) | x < y  = x : mezcla xs (y:ys)\n                     | x == y = x : mezcla xs ys\n                     | x > y  = y : mezcla (x:xs) ys\n\n-- 2\u00aa definici\u00f3n\n-- =============\n\nsumaOrdenada2 :: [Integer] -> [Integer] -> [Integer]\nsumaOrdenada2 xs ys = mezclaTodas [map (+x) ys | x <- xs]\n\nmezclaTodas :: Ord a => [[a]] -> [a]\nmezclaTodas = foldr1 xmezcla\n  where xmezcla (x:xs) ys = x : mezcla xs ys\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n sumaOrdenada :: [Integer] -> [Integer] -> [Integer] tal que (sumaOrdenada xs ys) es la suma ordenada de las listas infinitas crecientes xs e ys. Por ejemplo, \u03bb> take 15 (sumaOrdenada [5,10..] [7,14..]) [12,17,19,22,24,26,27,29,31,32,33,34,36,37,38] \u03bb> take 15 (sumaOrdenada [2^n | n [Integer] sumaOrdenada1 (x:xs) (y:ys) = x+y : map (+x) ys `mezcla` map&#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":[4],"tags":[243,10,11,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2804"}],"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=2804"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2804\/revisions"}],"predecessor-version":[{"id":2843,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2804\/revisions\/2843"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}