{"id":6718,"date":"2022-03-03T06:00:39","date_gmt":"2022-03-03T04:00:39","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6718"},"modified":"2022-03-10T08:40:43","modified_gmt":"2022-03-10T06:40:43","slug":"suma-si-todos-los-valores-son-justos","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/suma-si-todos-los-valores-son-justos\/","title":{"rendered":"Suma si todos los valores son justos"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   sumaSiTodosJustos :: (Num a, Eq a) => [Maybe a] -> Maybe a\n<\/pre>\n<p>tal que (sumaSiTodosJustos xs) es justo la suma de todos los elementos de xs si todos son justos (es decir, si Nothing no pertenece a xs) y Nothing en caso contrario. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   sumaSiTodosJustos [Just 2, Just 5]           == Just 7\n   sumaSiTodosJustos [Just 2, Just 5, Nothing]  == Nothing\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Maybe (catMaybes, isJust, fromJust)\nimport Test.QuickCheck (quickCheck)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos1 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos1 xs\n  | todosJustos xs = Just (sum [x | (Just x) <- xs])\n  | otherwise      = Nothing\n\n-- (todosJustos xs) se verifica si todos los elementos de xs son justos\n-- (es decir, si Nothing no pertenece a xs) y Nothing en caso\n-- contrario. Por ejemplo,\n--    todosJustos [Just 2, Just 5]           == True\n--    todosJustos [Just 2, Just 5, Nothing]  == False\n\n-- 1\u00aa definici\u00f3n de todosJustos:\ntodosJustos1 :: Eq a => [Maybe a] -> Bool\ntodosJustos1 = notElem Nothing\n\n-- 2\u00aa definici\u00f3n de todosJustos:\ntodosJustos :: Eq a => [Maybe a] -> Bool\ntodosJustos = all isJust\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos2 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos2 xs\n  | todosJustos xs = Just (sum [fromJust x | x <- xs])\n  | otherwise      = Nothing\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos3 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos3 xs\n  | todosJustos xs = Just (sum (map fromJust xs))\n  | otherwise      = Nothing\n\n-- 4\u00aa soluci\u00f3n\n\nsumaSiTodosJustos4 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos4 xs\n  | todosJustos xs = Just (sum (catMaybes xs))\n  | otherwise      = Nothing\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos5 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos5 xs = suma (sequence xs)\n  where suma Nothing   = Nothing\n        suma (Just ys) = Just (sum ys)\n\n-- Nota. En la soluci\u00f3n anterior se usa la funci\u00f3n\n--    sequence :: Monad m => [m a] -> m [a]\n-- tal que (sequence xs) es la m\u00f3nada obtenida evaluando cada una de las\n-- de xs de izquierda a derecha. Por ejemplo,\n--    sequence [Just 2, Just 5]   ==  Just [2,5]\n--    sequence [Just 2, Nothing]  ==  Nothing\n--    sequence [[2,4],[5,7]]      ==  [[2,5],[2,7],[4,5],[4,7]]\n--    sequence [[2,4],[5,7],[6]]  ==  [[2,5,6],[2,7,6],[4,5,6],[4,7,6]]\n--    sequence [[2,4],[5,7],[]]   ==  []\n\n-- 6\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos6 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos6 xs = fmap sum (sequence xs)\n\n-- 7\u00aa soluci\u00f3n\n-- ===========\n\nsumaSiTodosJustos7 :: (Num a, Eq a) => [Maybe a] -> Maybe a\nsumaSiTodosJustos7 = fmap sum . sequence\n\n-- Equivalencia de las definiciones\n-- ================================\n\n-- La propiedad es\nprop_sumaSiTodosJustos :: [Maybe Integer] -> Bool\nprop_sumaSiTodosJustos xs =\n  all (== sumaSiTodosJustos1 xs)\n      [sumaSiTodosJustos2 xs,\n       sumaSiTodosJustos3 xs,\n       sumaSiTodosJustos4 xs,\n       sumaSiTodosJustos5 xs,\n       sumaSiTodosJustos6 xs,\n       sumaSiTodosJustos7 xs]\n\nverifica_sumaSiTodosJustos :: IO ()\nverifica_sumaSiTodosJustos =\n  quickCheck prop_sumaSiTodosJustos\n\n-- La comprobaci\u00f3n es\n--    \u03bb> verifica_sumaSiTodosJustos\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Suma_si_todos_justos.hs\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n sumaSiTodosJustos :: (Num a, Eq a) => [Maybe a] -> Maybe a tal que (sumaSiTodosJustos xs) es justo la suma de todos los elementos de xs si todos son justos (es decir, si Nothing no pertenece a xs) y Nothing en caso contrario. Por ejemplo, sumaSiTodosJustos [Just 2, Just 5] == Just&#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":[8,500,11],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6718"}],"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=6718"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6718\/revisions"}],"predecessor-version":[{"id":6747,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6718\/revisions\/6747"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}