{"id":515,"date":"2014-07-24T07:00:28","date_gmt":"2014-07-24T05:00:28","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=515"},"modified":"2014-12-27T14:09:49","modified_gmt":"2014-12-27T12:09:49","slug":"sustitucion-en-una-expresion","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/sustitucion-en-una-expresion\/","title":{"rendered":"Sustituci\u00f3n en una expresi\u00f3n"},"content":{"rendered":"<h4>Enunciado<\/h4>\n<pre lang=\"text\">\n-- La expresiones aritm\u00e9ticas se pueden representar mediante el\n-- siguiente tipo  \n--    data Expr = V Char \n--              | N Int \n--              | S Expr Expr\n--              | P Expr Expr\n--              deriving Show\n-- por ejemplo, representa la expresi\u00f3n \"z*(3+x)\" se representa por\n-- (P (V 'z') (S (N 3) (V 'x'))). \n--\n-- Definir la funci\u00f3n\n--    sustitucion :: Expr -> [(Char, Int)] -> Expr\n-- tal que (sustitucion e s) es la expresi\u00f3n obtenida sustituyendo las\n-- variables de la expresi\u00f3n e seg\u00fan se indica en la sustituci\u00f3n s. Por\n-- ejemplo, \n--    ghci> sustitucion (P (V 'z') (S (N 3) (V 'x'))) [('x',7),('z',9)]\n--    P (N 9) (S (N 3) (N 7))\n--    ghci> sustitucion (P (V 'z') (S (N 3) (V 'y'))) [('x',7),('z',9)]\n--    P (N 9) (S (N 3) (V 'y'))\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\ndata Expr = V Char \n          | N Int \n          | S Expr Expr\n          | P Expr Expr\n          deriving Show\n\nsustitucion :: Expr -> [(Char, Int)] -> Expr\nsustitucion e [] = e\nsustitucion (V c) ((d,n):ps) | c == d = N n\n                             | otherwise = sustitucion (V c) ps\nsustitucion (N n) _ = N n                                 \nsustitucion (S e1 e2) ps = S (sustitucion e1 ps) (sustitucion e2 ps)\nsustitucion (P e1 e2) ps = P (sustitucion e1 ps) (sustitucion e2 ps)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Enunciado &#8212; La expresiones aritm\u00e9ticas se pueden representar mediante el &#8212; siguiente tipo &#8212; data Expr = V Char &#8212; | N Int &#8212; | S Expr Expr &#8212; | P Expr Expr &#8212; deriving Show &#8212; por ejemplo, representa la expresi\u00f3n \u00abz*(3+x)\u00bb se representa por &#8212; (P (V &#8216;z&#8217;) (S (N 3) (V &#8216;x&#8217;)))&#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":[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\/515"}],"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=515"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/515\/revisions\/867"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}