{"id":4033,"date":"2018-05-03T06:00:24","date_gmt":"2018-05-03T04:00:24","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4033"},"modified":"2018-05-10T07:03:55","modified_gmt":"2018-05-10T05:03:55","slug":"maximos-de-expresiones-aritmeticas","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/maximos-de-expresiones-aritmeticas\/","title":{"rendered":"M\u00e1ximos de expresiones aritm\u00e9ticas"},"content":{"rendered":"<p>Las expresiones aritm\u00e9ticas se pueden definir usando el siguiente tipo de datos<\/p>\n<pre lang=\"text\">\n   data Expr = N Int \n             | X \n             | S Expr Expr \n             | R Expr Expr \n             | P Expr Expr \n             | E Expr Int\n             deriving (Eq, Show)\n<\/pre>\n<p>Por ejemplo, la expresi\u00f3n<\/p>\n<pre lang=\"text\">\n   3*x - (x+2)^7\n<\/pre>\n<p>se puede definir por<\/p>\n<pre lang=\"text\">\n   R (P (N 3) X) (E (S X (N 2)) 7)\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   maximo :: Expr -> [Int] -> (Int,[Int])\n<\/pre>\n<p>tal que (maximo e xs) es el par formado por el m\u00e1ximo valor de la expresi\u00f3n e para los puntos de xs y en qu\u00e9 puntos alcanza el m\u00e1ximo. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> maximo (E (S (N 10) (P (R (N 1) X) X)) 2) [-3..3]\n   (100,[0,1])\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Expr = N Int \n          | X \n          | S Expr Expr \n          | R Expr Expr \n          | P Expr Expr \n          | E Expr Int\n          deriving (Eq, Show)\n\nmaximo :: Expr -> [Int] -> (Int,[Int])\nmaximo e ns = (m,[n | n <- ns, valor e n == m])  \n    where m = maximum [valor e n | n <- ns]\n\nvalor :: Expr -> Int -> Int\nvalor (N x) _ = x\nvalor X     n = n\nvalor (S e1 e2) n = (valor e1 n) + (valor e2 n)\nvalor (R e1 e2) n = (valor e1 n) - (valor e2 n)\nvalor (P e1 e2) n = (valor e1 n) * (valor e2 n)\nvalor (E e  m ) n = (valor e  n)^m\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Las expresiones aritm\u00e9ticas se pueden definir usando el siguiente tipo de datos data Expr = N Int | X | S Expr Expr | R Expr Expr | P Expr Expr | E Expr Int deriving (Eq, Show) Por ejemplo, la expresi\u00f3n 3*x &#8211; (x+2)^7 se puede definir por R (P (N 3) X) (E&#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,15,6,133],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4033"}],"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=4033"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4033\/revisions"}],"predecessor-version":[{"id":4067,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4033\/revisions\/4067"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}