{"id":7265,"date":"2022-08-24T06:00:38","date_gmt":"2022-08-24T04:00:38","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7265"},"modified":"2022-12-14T16:46:13","modified_gmt":"2022-12-14T14:46:13","slug":"segmento-de-una-lista","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/segmento-de-una-lista\/","title":{"rendered":"Segmento de una lista"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   segmento :: Int -> Int -> [a] -> [a]\n<\/pre>\n<p>tal que <code>(segmento m n xs)<\/code> es la lista de los elementos de <code>xs<\/code> comprendidos entre las posiciones <code>m<\/code> y <code>n<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   segmento 3 4 [3,4,1,2,7,9,0]  ==  [1,2]\n   segmento 3 5 [3,4,1,2,7,9,0]  ==  [1,2,7]\n   segmento 5 3 [3,4,1,2,7,9,0]  ==  []\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nsegmento :: Int -> Int -> [a] -> [a]\nsegmento m n xs = drop (m-1) (take n xs)\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Segmento_de_una_lista.hs\">GitHub<\/a>.<\/p>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom typing import TypeVar\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\n\nA = TypeVar('A')\n\n# 1\u00aa definici\u00f3n\ndef segmento1(m: int, n: int, xs: list[A]) -> list[A]:\n    ys = xs[:n]\n    if m == 0:\n        return ys\n    return ys[m - 1:]\n\n# 2\u00aa definici\u00f3n\ndef segmento2(m: int, n: int, xs: list[A]) -> list[A]:\n    if m == 0:\n        return xs[:n]\n    return xs[m-1:n]\n\n# La propiedad de equivalencia es\n@given(st.integers(min_value=0),\n       st.integers(min_value=0),\n       st.lists(st.integers()))\ndef test_equiv_segmento(m: int, n: int, xs: list[int]) -> None:\n    assert segmento1(m, n, xs) == segmento2(m, n, xs)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q segmento_de_una_lista.py\n#    1 passed in 0.19s\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/segmento_de_una_lista.py\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n segmento :: Int -> Int -> [a] -> [a] tal que (segmento m n xs) es la lista de los elementos de xs comprendidos entre las posiciones m y n. Por ejemplo, segmento 3 4 [3,4,1,2,7,9,0] == [1,2] segmento 3 5 [3,4,1,2,7,9,0] == [1,2,7] segmento 5 3 [3,4,1,2,7,9,0] == [] Soluciones A&#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":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7265"}],"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=7265"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7265\/revisions"}],"predecessor-version":[{"id":7707,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7265\/revisions\/7707"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}