Remove the first and the last element from a list in Haskell









up vote
6
down vote

favorite
1












I am writing a small function which can help me get rid of the first and last element of a list. This is what I am trying to do:



>firstLast::[a]->[a]
>firstLast [x] = [ ]
>firstLast h:t = [i|i!= head[a] || i!= last[a]]


As you can see I am trying to use list comprehension here,but apparently I didn't use it properly.










share|improve this question



























    up vote
    6
    down vote

    favorite
    1












    I am writing a small function which can help me get rid of the first and last element of a list. This is what I am trying to do:



    >firstLast::[a]->[a]
    >firstLast [x] = [ ]
    >firstLast h:t = [i|i!= head[a] || i!= last[a]]


    As you can see I am trying to use list comprehension here,but apparently I didn't use it properly.










    share|improve this question

























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      I am writing a small function which can help me get rid of the first and last element of a list. This is what I am trying to do:



      >firstLast::[a]->[a]
      >firstLast [x] = [ ]
      >firstLast h:t = [i|i!= head[a] || i!= last[a]]


      As you can see I am trying to use list comprehension here,but apparently I didn't use it properly.










      share|improve this question















      I am writing a small function which can help me get rid of the first and last element of a list. This is what I am trying to do:



      >firstLast::[a]->[a]
      >firstLast [x] = [ ]
      >firstLast h:t = [i|i!= head[a] || i!= last[a]]


      As you can see I am trying to use list comprehension here,but apparently I didn't use it properly.







      list haskell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 29 at 22:24









      Mateusz Piotrowski

      3,69163049




      3,69163049










      asked Nov 6 '13 at 0:52









      Nob Wong

      94311




      94311






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          14
          down vote



          accepted










          Why not just this?



          firstLast::[a]->[a]
          firstLast =
          firstLast [x] =
          firstLast xs = tail (init xs)





          share|improve this answer




















          • Yes.. I simply forgot there is a function called init.. THanks!
            – Nob Wong
            Nov 6 '13 at 1:09






          • 2




            Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
            – jozefg
            Nov 6 '13 at 3:15










          • @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
            – Will Ness
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19802039%2fremove-the-first-and-the-last-element-from-a-list-in-haskell%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          14
          down vote



          accepted










          Why not just this?



          firstLast::[a]->[a]
          firstLast =
          firstLast [x] =
          firstLast xs = tail (init xs)





          share|improve this answer




















          • Yes.. I simply forgot there is a function called init.. THanks!
            – Nob Wong
            Nov 6 '13 at 1:09






          • 2




            Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
            – jozefg
            Nov 6 '13 at 3:15










          • @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
            – Will Ness
            yesterday














          up vote
          14
          down vote



          accepted










          Why not just this?



          firstLast::[a]->[a]
          firstLast =
          firstLast [x] =
          firstLast xs = tail (init xs)





          share|improve this answer




















          • Yes.. I simply forgot there is a function called init.. THanks!
            – Nob Wong
            Nov 6 '13 at 1:09






          • 2




            Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
            – jozefg
            Nov 6 '13 at 3:15










          • @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
            – Will Ness
            yesterday












          up vote
          14
          down vote



          accepted







          up vote
          14
          down vote



          accepted






          Why not just this?



          firstLast::[a]->[a]
          firstLast =
          firstLast [x] =
          firstLast xs = tail (init xs)





          share|improve this answer












          Why not just this?



          firstLast::[a]->[a]
          firstLast =
          firstLast [x] =
          firstLast xs = tail (init xs)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 6 '13 at 1:02









          Benten

          708814




          708814











          • Yes.. I simply forgot there is a function called init.. THanks!
            – Nob Wong
            Nov 6 '13 at 1:09






          • 2




            Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
            – jozefg
            Nov 6 '13 at 3:15










          • @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
            – Will Ness
            yesterday
















          • Yes.. I simply forgot there is a function called init.. THanks!
            – Nob Wong
            Nov 6 '13 at 1:09






          • 2




            Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
            – jozefg
            Nov 6 '13 at 3:15










          • @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
            – Will Ness
            yesterday















          Yes.. I simply forgot there is a function called init.. THanks!
          – Nob Wong
          Nov 6 '13 at 1:09




          Yes.. I simply forgot there is a function called init.. THanks!
          – Nob Wong
          Nov 6 '13 at 1:09




          2




          2




          Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
          – jozefg
          Nov 6 '13 at 3:15




          Simplified to firstLast xs@(_:_) = tail (init xs); firstLast _ =
          – jozefg
          Nov 6 '13 at 3:15












          @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
          – Will Ness
          yesterday




          @jozefg subst "simplified" with "shorter, more complicated, and incorrect". :) You of course meant xs@(_:_:_). "simpler" means "more self-evidently correct".
          – Will Ness
          yesterday

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19802039%2fremove-the-first-and-the-last-element-from-a-list-in-haskell%23new-answer', 'question_page');

          );

          Post as a guest














































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Carbon dioxide

          Why am i infinitely getting the same tweet with the Twitter Search API?