{"id":342,"date":"2014-06-23T07:00:51","date_gmt":"2014-06-23T05:00:51","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=342"},"modified":"2022-03-26T12:14:25","modified_gmt":"2022-03-26T10:14:25","slug":"filtro-booleano","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/filtro-booleano\/","title":{"rendered":"Filtro booleano"},"content":{"rendered":"<h4>Enunciado<\/h4>\n<pre lang=\"text\">\n-- Definir la funci\u00f3n\n--    filtroBooleano :: [Bool] -> [a] -> [Maybe a]\n-- tal que (filtroBooleano xs ys) es la lista de los elementos de ys\n-- tales que el elemento de xs en la misma posici\u00f3n es verdadero. Por\n-- ejemplo, \n--    ghci> filtroBooleano [True,False,True] \"Sevilla\"\n--    [Just 'S',Nothing,Just 'v']\n--    ghci> filtroBooleano (repeat True) \"abc\"\n--    [Just 'a',Just 'b',Just 'c']\n--    ghci> take 3 (filtroBooleano (repeat True) [1..])\n--    [Just 1,Just 2,Just 3]\n--    ghci> take 3 (filtroBooleano (repeat False) [1..])\n--    [Nothing,Nothing,Nothing]\n--    ghci> take 3 (filtroBooleano (cycle [True,False]) [1..])\n--    [Just 1,Nothing,Just 3]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\n-- 1\u00aa soluci\u00f3n (por comprensi\u00f3n):\nfiltroBooleano1 :: [Bool] -> [a] -> [Maybe a]\nfiltroBooleano1 xs ys = [f (x,y) | (x,y) <- zip xs ys]\n    where f (x,y) | x         = Just y\n                  | otherwise = Nothing\n\n-- 2\u00aa soluci\u00f3n (con zipWith):\nfiltroBooleano2 :: [Bool] -> [a] -> [Maybe a]\nfiltroBooleano2 = zipWith f\n    where f x y  | x         = Just y\n                 | otherwise = Nothing\n\n-- 3\u00aa soluci\u00f3n (con zipWith y lambda):\nfiltroBooleano3 :: [Bool] -> [a] -> [Maybe a]\nfiltroBooleano3 = zipWith (\\x y -> if x then Just y else Nothing)\n\n-- 4\u00aa soluci\u00f3n (por recursi\u00f3n):\nfiltroBooleano4 :: [Bool] -> [a] -> [Maybe a]\nfiltroBooleano4 (x:xs) (y:ys) | x         = Just y : filtroBooleano4 xs ys\n                              | otherwise = Nothing : filtroBooleano4 xs ys\nfiltroBooleano4 _ _                       = []\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Enunciado &#8212; Definir la funci\u00f3n &#8212; filtroBooleano :: [Bool] -> [a] -> [Maybe a] &#8212; tal que (filtroBooleano xs ys) es la lista de los elementos de ys &#8212; tales que el elemento de xs en la misma posici\u00f3n es verdadero. Por &#8212; ejemplo, &#8212; ghci> filtroBooleano [True,False,True] \u00abSevilla\u00bb &#8212; [Just &#8216;S&#8217;,Nothing,Just &#8216;v&#8217;] &#8212; ghci>&#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":[8,500,6,9,76],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/342"}],"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=342"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/342\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}