{"id":7283,"date":"2022-09-01T06:00:43","date_gmt":"2022-09-01T04:00:43","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7283"},"modified":"2022-12-14T16:40:12","modified_gmt":"2022-12-14T14:40:12","slug":"disyuncion-excluyente","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/disyuncion-excluyente\/","title":{"rendered":"Disyunci\u00f3n excluyente"},"content":{"rendered":"<p>La disyunci\u00f3n excluyente de dos f\u00f3rmulas se verifica si una es verdadera y la otra es falsa. Su tabla de verdad es<\/p>\n<pre lang=\"text\">\n   x     | y     | xor x y\n   ------+-------+---------\n   True  | True  | False\n   True  | False | True\n   False | True  | True\n   False | False | False\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   xor :: Bool -> Bool -> Bool\n<\/pre>\n<p>tal que <code>(xor x y)<\/code> es la disyunci\u00f3n excluyente de <code>x<\/code> e <code>y<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   xor True  True  == False\n   xor True  False == True\n   xor False True  == True\n   xor False False == False\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\nxor1 :: Bool -> Bool -> Bool\nxor1 True  True  = False\nxor1 True  False = True\nxor1 False True  = True\nxor1 False False = False\n\n-- 2\u00aa soluci\u00f3n\nxor2 :: Bool -> Bool -> Bool\nxor2 True  y = not y\nxor2 False y = y\n\n-- 3\u00aa soluci\u00f3n:\nxor3 :: Bool -> Bool -> Bool\nxor3 x y = (x || y) && not (x && y)\n\n-- 4\u00aa soluci\u00f3n:\nxor4 :: Bool -> Bool -> Bool\nxor4 x y = (x && not y) || (y && not x)\n\n-- 5\u00aa soluci\u00f3n:\nxor5 :: Bool -> Bool -> Bool\nxor5 x y = x \/= y\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La propiedad es\nprop_xor :: Bool -> Bool -> Bool\nprop_xor x y =\n  all (== xor1 x y)\n      [xor2 x y,\n       xor3 x y,\n       xor4 x y,\n       xor5 x y]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_xor\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\/Disyuncion_excluyente.hs\">GitHub<\/a>.<\/p>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom hypothesis import given, strategies as st\n\n# 1\u00aa soluci\u00f3n\ndef xor1(x, y):\n    match x, y:\n        case True,  True: return False\n        case True,  False: return True\n        case False, True: return True\n        case False, False: return False\n\n# 2\u00aa soluci\u00f3n\ndef xor2(x: bool, y: bool) -> bool:\n    if x:\n        return not y\n    return y\n\n# 3\u00aa soluci\u00f3n\ndef xor3(x: bool, y: bool) -> bool:\n    return (x or y) and not(x and y)\n\n# 4\u00aa soluci\u00f3n\ndef xor4(x: bool, y: bool) -> bool:\n    return (x and not y) or (y and not x)\n\n# 5\u00aa soluci\u00f3n\ndef xor5(x: bool, y: bool) -> bool:\n    return x != y\n\n# La propiedad de equivalencia es\n@given(st.booleans(), st.booleans())\ndef test_equiv_xor(x, y):\n    assert xor1(x, y) == xor2(x, y) == xor3(x, y) == xor4(x, y) == xor5(x, y)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q disyuncion_excluyente.py\n#    1 passed in 0.11s\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/disyuncion_excluyente.py\">GitHub<\/a>.<\/p>\n<p><b>Comentarios<\/b><\/p>\n<ul>\n<li>La negaci\u00f3n de <code>x<\/code> se escribe igual en Haskell y Python; <code>not x<\/code><\/p>\n<\/li>\n<li>\n<p>La disyunci\u00f3n de <code>x<\/code> e <code>y<\/code> se escribe<\/p>\n<ul>\n<li>en Haskell, como <code>x || y<\/code> y<\/li>\n<li>en Python, como <code>x or y<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>La disyunci\u00f3n excluyente de dos f\u00f3rmulas se verifica si una es verdadera y la otra es falsa. Su tabla de verdad es x | y | xor x y &#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212; True | True | False True | False | True False | True | True False | False | False Definir la funci\u00f3n xor ::&#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":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7283"}],"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=7283"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7283\/revisions"}],"predecessor-version":[{"id":7701,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7283\/revisions\/7701"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}