{"id":3353,"date":"2017-06-07T07:03:00","date_gmt":"2017-06-07T05:03:00","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3353"},"modified":"2021-04-25T17:03:18","modified_gmt":"2021-04-25T15:03:18","slug":"valores-de-polinomios-y-de-expresiones-17","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/valores-de-polinomios-y-de-expresiones-17\/","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 coeficientes. 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<p>[schedule expon=&#8217;2017-06-14&#8242; expat=\u00bb06:00&#8243;]<\/p>\n<ul>\n<li>Las soluciones se pueden escribir en los comentarios hasta el 14 de junio.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=\u00bbhaskell\u00bb&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n<p>[\/schedule]<\/p>\n<p>[schedule on=&#8217;2017-06-14&#8242; at=\u00bb06:00&#8243;]<\/p>\n<pre lang=\"haskell\">\r\nimport Test.QuickCheck\r\n\r\ndata Exp = Var | Const Int | Sum Exp Exp | Mul Exp  Exp\r\n           deriving Show\r\n \r\nexp1 :: Exp\r\nexp1 = Sum (Const 3) (Mul Var (Mul Var (Const 5)))\r\n \r\nvalorE :: Exp -> Int -> Int\r\nvalorE Var         n = n\r\nvalorE (Const a)   n = a\r\nvalorE (Sum e1 e2) n = valorE e1 n + valorE e2 n\r\nvalorE (Mul e1 e2) n = valorE e1 n * valorE e2 n\r\n \r\nexpresion :: [Int] -> Exp\r\nexpresion [a]   = Const a\r\nexpresion (a:p) = Sum (Const a) (Mul Var (expresion p))\r\n \r\nvalorP :: [Int] -> Int -> Int\r\nvalorP [a] _ = a\r\nvalorP (a:p) n = a + n * valorP p n\r\n \r\n-- La propiedad es\r\nprop_valor :: [Int] -> Int -> Property\r\nprop_valor p n =\r\n    not (null p) ==> \r\n    valorP p n == valorE (expresion p) n\r\n \r\n-- La comprobaci\u00f3n es\r\n--    \u03bb> quickCheck prop_valor\r\n--    +++ OK, passed 100 tests.\r\n<\/pre>\n<p>[\/schedule]<\/p>\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":[2],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3353"}],"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=3353"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3353\/revisions"}],"predecessor-version":[{"id":3356,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3353\/revisions\/3356"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}