{"id":549,"date":"2014-11-06T06:30:55","date_gmt":"2014-11-06T04:30:55","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=549"},"modified":"2014-12-27T21:39:59","modified_gmt":"2014-12-27T19:39:59","slug":"rompecabeza-matematico","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/rompecabeza-matematico\/","title":{"rendered":"Rompecabeza matem\u00e1tico"},"content":{"rendered":"<h4>Enunciado<\/h4>\n<pre lang=\"text\">\n-- Definir una funci\u00f3n\n--    f :: Int -> Int\n-- tal que para todo n, f(f(n)) = -n y comprobar con QuickCheck que se\n-- cumple la propiedad\n--    prop_f :: Int -> Bool\n--    prop_f n = f (f n) == -n\n-- es decir,\n--    ghci> quickCheck prop_f\n--    +++ OK, passed 100 tests.\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa definici\u00f3n (por casos)\nf :: Int -> Int\nf n | even n && n > 0 = n-1\n    | even n && n < 0 = n+1\n    | odd  n &#038;&#038; n > 0 = -n-1\n    | odd  n && n < 0 = -n+1\n    | otherwise       = 0\n\n-- La propiedad es\nprop_f :: Int -> Bool\nprop_f n = f (f n) == -n\n\n-- La comprobaci\u00f3n es\n--    ghci> quickCheck prop_f\n--    +++ OK, passed 100 tests.\n\n-- 2\u00aa definici\u00f3n (por casos y signo):\nf2 :: Int -> Int\nf2 n | even n    =  n - signum n\n     | odd  n    = -n - signum n\n     | otherwise = 0\n\n-- La propiedad es\nprop_f2 :: Int -> Bool\nprop_f2 n = f2 (f2 n) == -n\n\n-- La comprobaci\u00f3n es\n--    ghci> quickCheck prop_f2\n--    +++ OK, passed 100 tests.\n\n-- 3\u00aa soluci\u00f3n (sin casos):\nf3 :: Int -> Int\nf3 n = n * (2 * mod n 2 - 1) + signum n\n\n-- La propiedad es\nprop_f3 :: Int -> Bool\nprop_f3 n = f3 (f3 n) == -n\n\n-- La comprobaci\u00f3n es\n--    ghci> quickCheck prop_f3\n--    +++ OK, passed 100 tests.\n\n-- 4\u00aa soluci\u00f3n (sin casos):\nf4 :: Int -> Int\nf4 n = (-1)^(abs n)*n - signum n\n\n-- La propiedad es\nprop_f4 :: Int -> Bool\nprop_f4 n = f4 (f4 n) == -n\n\n-- La comprobaci\u00f3n es\n--    ghci> quickCheck prop_f4\n--    +++ OK, passed 100 tests.\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Enunciado &#8212; Definir una funci\u00f3n &#8212; f :: Int -> Int &#8212; tal que para todo n, f(f(n)) = -n y comprobar con QuickCheck que se &#8212; cumple la propiedad &#8212; prop_f :: Int -> Bool &#8212; prop_f n = f (f n) == -n &#8212; es decir, &#8212; ghci> quickCheck prop_f &#8212; +++ OK,&#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":[130,149,91,89,92,151,146],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/549"}],"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=549"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":745,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/549\/revisions\/745"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}