{"id":1479,"date":"2015-05-22T06:00:33","date_gmt":"2015-05-22T04:00:33","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=1479"},"modified":"2015-06-13T16:28:48","modified_gmt":"2015-06-13T14:28:48","slug":"numeros-comenzando-con-un-digito-dado","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/numeros-comenzando-con-un-digito-dado\/","title":{"rendered":"N\u00fameros comenzando con un d\u00edgito dado"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   comienzanCon :: [Int] -> Int -> [Int]\n<\/pre>\n<p>tal que (comienzanCon xs d) es la lista de los elementos de xs que empiezan por el d\u00edgito d. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   comienzanCon [123,51,11,711,52] 1 == [123,11]\n   comienzanCon [123,51,11,711,52] 5 == [51,52]\n   comienzanCon [123,51,11,711,52] 6 == []\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Char (intToDigit)\n\n-- 1\u00aa definici\u00f3n (por comprensi\u00f3n)\ncomienzanCon :: [Int]-> Int -> [Int]\ncomienzanCon xs d = [x | x <- xs, head (show x) == d']\n    where d' = head (show d)\n\n-- 2\u00aa definici\u00f3n (por recursi\u00f3n)\ncomienzanCon2 :: [Int]-> Int -> [Int]\ncomienzanCon2 xs d = aux xs\n    where aux [] = []\n          aux (x:xs) | head (show x) == d' = x : aux xs\n                     | otherwise           = aux xs\n          d' = head (show d)\n\n-- 3\u00aa definici\u00f3n (por filtrado)\ncomienzanCon3 :: [Int]-> Int -> [Int]\ncomienzanCon3 xs d = filter (\\x -> head (show x) == d') xs\n    where d' = head (show d)\n\n-- 4\u00aa definici\u00f3n (por plegado)\ncomienzanCon4 :: [Int]-> Int -> [Int]\ncomienzanCon4 xs d = foldr f [] xs\n    where f x ys | head (show x) == d' = x : ys\n                 | otherwise           = ys\n          d' = head (show d)\n\n-- En las definiciones anteriores se puede usa intToDigit en lugar de\n-- read. \n\ncomienzanCon' :: [Int]-> Int -> [Int]\ncomienzanCon' xs d = [x | x <- xs, head (show x) == intToDigit d]\n\ncomienzanCon2' :: [Int]-> Int -> [Int]\ncomienzanCon2' xs d = aux xs\n    where aux [] = []\n          aux (x:xs) | head (show x) == intToDigit d = x : aux xs\n                     | otherwise                     = aux xs\n\ncomienzanCon3' :: [Int]-> Int -> [Int]\ncomienzanCon3' xs d = filter ((==intToDigit d) . head . show) xs\n\ncomienzanCon4' :: [Int]-> Int -> [Int]\ncomienzanCon4' xs d = foldr f [] xs\n    where f x ys | head (show x) == intToDigit d = x : ys\n                 | otherwise                     = ys\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n comienzanCon :: [Int] -> Int -> [Int] tal que (comienzanCon xs d) es la lista de los elementos de xs que empiezan por el d\u00edgito d. Por ejemplo, comienzanCon [123,51,11,711,52] 1 == [123,11] comienzanCon [123,51,11,711,52] 5 == [51,52] comienzanCon [123,51,11,711,52] 6 == [] Soluciones import Data.Char (intToDigit) &#8212; 1\u00aa definici\u00f3n (por comprensi\u00f3n)&#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":[5],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1479"}],"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=1479"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"predecessor-version":[{"id":1514,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/1479\/revisions\/1514"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}