{"id":4131,"date":"2018-06-04T10:13:38","date_gmt":"2018-06-04T08:13:38","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4131"},"modified":"2018-06-11T17:26:25","modified_gmt":"2018-06-11T15:26:25","slug":"valores-de-polinomios-y-de-expresiones","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/valores-de-polinomios-y-de-expresiones\/","title":{"rendered":"Valores de polinomios y de expresiones"},"content":{"rendered":"<p>Las expresiones aritm\u00e9ticas construidas con una variables, los n\u00fameros enteros y las operaciones de sumar y multiplicar se pueden representar mediante el tipo de datos Exp definido por<\/p>\n<pre lang=\"text\">\n   data Exp = Var | Const Int | Sum Exp Exp | Mul Exp  Exp\n              deriving Show\n<\/pre>\n<p>Por ejemplo, la expresi\u00f3n 3+5x^2 se puede representar por<\/p>\n<pre lang=\"text\">\n   exp1 :: Exp\n   exp1 = Sum (Const 3) (Mul Var (Mul Var (Const 5)))\n<\/pre>\n<p>Por su parte, los polinomios se pueden representar por la lista de  sus<br \/>\ncoeficientes. Por ejemplo, el polinomio 3+5x^2 se puede representar por [3,0,5].<\/p>\n<p>Definir las funciones<\/p>\n<pre lang=\"text\">\n   valorE :: Exp -> Int -> Int\n   expresion :: [Int] -> Exp\n   valorP :: [Int] -> Int -> Int\n<\/pre>\n<p>tales que<\/p>\n<ul>\n<li>(valorE e n) es el valor de la expresi\u00f3n e cuando se sustituye su variable por n. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> valorE (Sum (Const 3) (Mul Var (Mul Var (Const 5)))) 2\n     23\n<\/pre>\n<ul>\n<li>(expresion p) es una expresi\u00f3n aritm\u00e9tica equivalente al polinomio p. Por ejemplo, <\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> expresion [3,0,5]\n     Sum (Const 3) (Mul Var (Sum (Const 0) (Mul Var (Const 5))))\n<\/pre>\n<ul>\n<li>(valorP p n) es el valor del polinomio p cuando se sustituye su variable por n. Por ejemplo,<\/li>\n<\/ul>\n<pre lang=\"text\">\n     \u03bb> valorP [3,0,5] 2\n     23\n<\/pre>\n<p>Comprobar con QuickCheck que, para todo polinomio p y todo entero n,<\/p>\n<pre lang=\"text\">\n   valorP p n == valorE (expresion p) n\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\ndata Exp = Var | Const Int | Sum Exp Exp | Mul Exp  Exp\n           deriving Show\n \nexp1 :: Exp\nexp1 = Sum (Const 3) (Mul Var (Mul Var (Const 5)))\n \nvalorE :: Exp -> Int -> Int\nvalorE Var         n = n\nvalorE (Const a)   n = a\nvalorE (Sum e1 e2) n = valorE e1 n + valorE e2 n\nvalorE (Mul e1 e2) n = valorE e1 n * valorE e2 n\n \nexpresion :: [Int] -> Exp\nexpresion [a]   = Const a\nexpresion (a:p) = Sum (Const a) (Mul Var (expresion p))\n \nvalorP :: [Int] -> Int -> Int\nvalorP [a] _ = a\nvalorP (a:p) n = a + n * valorP p n\n \n-- La propiedad es\nprop_valor :: [Int] -> Int -> Property\nprop_valor p n =\n    not (null p) ==> \n    valorP p n == valorE (expresion p) n\n \n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_valor\n--    +++ OK, passed 100 tests.\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Las expresiones aritm\u00e9ticas construidas con una variables, los n\u00fameros enteros y las operaciones de sumar y multiplicar se pueden representar mediante el tipo de datos Exp definido por data Exp = Var | Const Int | Sum Exp Exp | Mul Exp Exp deriving Show Por ejemplo, la expresi\u00f3n 3+5x^2 se puede representar por exp1&#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":[181,141,6,146,133],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4131"}],"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=4131"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4131\/revisions"}],"predecessor-version":[{"id":4151,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4131\/revisions\/4151"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}