Number each list element and format text in haskell












1















I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.



numberL :: [String] -> [String]
numberL =
numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))


Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?










share|improve this question



























    1















    I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.



    numberL :: [String] -> [String]
    numberL =
    numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))


    Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?










    share|improve this question

























      1












      1








      1








      I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.



      numberL :: [String] -> [String]
      numberL =
      numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))


      Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?










      share|improve this question














      I want to give each one a number from 1 to length(x:xs), like a book's page number. Unfortunately it only works backwards.



      numberL :: [String] -> [String]
      numberL =
      numberL (x:xs) = ([show (length(x:xs)) ++ ": " ++ x] ++ numberL (xs))


      Also how do I remove any new line and tab from the text and replace it with the actual new line and tabulator?







      haskell numbers element






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 16:02









      Akos FajsziAkos Fajszi

      445




      445
























          2 Answers
          2






          active

          oldest

          votes


















          3














          There are multiple built-in Haskell functions in Prelude that are good to learn and use them. zip and zipWith are two of them, when you think about something to be done using two different lists into one result list:



          [1..] will generate the list of indices for you, it's an infinite list



          appendIndex :: String -> Int -> String
          appendIndex s i = (show i) ++ " :" ++ s

          indexThem :: [String] -> [String]
          indexThem l = zipWith appendIndex l [1..]


          if you wanted to use zip, which is more basic but a little more verbose:



          appendIndex :: (String,Int) -> String
          appendIndex (s,i) = (show i) ++ " :" ++ s

          indexThem :: [String] -> [String]
          indexThem l = fmap appendIndex $ zip l [1..]
          -- if you dont know about Functors yet, `fmap` is the generic way of doing `map`





          share|improve this answer

































            3














            To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:



            numberL (x:xs) = ... ++ numberL xs


            So you calculate numberL xs and then put something in front of it. If numberL xs were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:.... So you could never build numberL (x:xs) from numberL xs just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs.



            The problem therefore is that it's not very useful to know numberL xs in order to calculate numberL (x:xs), due to the fact numberL always starts numbering from 1.



            So lift that restriction. Build a function that numbers starting at n,



            numberLFrom :: Int -> [String] -> [String]
            numberLFrom n = ...
            numberLFrom n (x:xs) = ...


            Now the question you have to ask yourself is, in order to number (x:xs) starting at n you need to number xs starting at which number? And then how do you introduced the numbered x to that result?






            share|improve this answer

























              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',
              autoActivateHeartbeat: false,
              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%2f53449776%2fnumber-each-list-element-and-format-text-in-haskell%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              There are multiple built-in Haskell functions in Prelude that are good to learn and use them. zip and zipWith are two of them, when you think about something to be done using two different lists into one result list:



              [1..] will generate the list of indices for you, it's an infinite list



              appendIndex :: String -> Int -> String
              appendIndex s i = (show i) ++ " :" ++ s

              indexThem :: [String] -> [String]
              indexThem l = zipWith appendIndex l [1..]


              if you wanted to use zip, which is more basic but a little more verbose:



              appendIndex :: (String,Int) -> String
              appendIndex (s,i) = (show i) ++ " :" ++ s

              indexThem :: [String] -> [String]
              indexThem l = fmap appendIndex $ zip l [1..]
              -- if you dont know about Functors yet, `fmap` is the generic way of doing `map`





              share|improve this answer






























                3














                There are multiple built-in Haskell functions in Prelude that are good to learn and use them. zip and zipWith are two of them, when you think about something to be done using two different lists into one result list:



                [1..] will generate the list of indices for you, it's an infinite list



                appendIndex :: String -> Int -> String
                appendIndex s i = (show i) ++ " :" ++ s

                indexThem :: [String] -> [String]
                indexThem l = zipWith appendIndex l [1..]


                if you wanted to use zip, which is more basic but a little more verbose:



                appendIndex :: (String,Int) -> String
                appendIndex (s,i) = (show i) ++ " :" ++ s

                indexThem :: [String] -> [String]
                indexThem l = fmap appendIndex $ zip l [1..]
                -- if you dont know about Functors yet, `fmap` is the generic way of doing `map`





                share|improve this answer




























                  3












                  3








                  3







                  There are multiple built-in Haskell functions in Prelude that are good to learn and use them. zip and zipWith are two of them, when you think about something to be done using two different lists into one result list:



                  [1..] will generate the list of indices for you, it's an infinite list



                  appendIndex :: String -> Int -> String
                  appendIndex s i = (show i) ++ " :" ++ s

                  indexThem :: [String] -> [String]
                  indexThem l = zipWith appendIndex l [1..]


                  if you wanted to use zip, which is more basic but a little more verbose:



                  appendIndex :: (String,Int) -> String
                  appendIndex (s,i) = (show i) ++ " :" ++ s

                  indexThem :: [String] -> [String]
                  indexThem l = fmap appendIndex $ zip l [1..]
                  -- if you dont know about Functors yet, `fmap` is the generic way of doing `map`





                  share|improve this answer















                  There are multiple built-in Haskell functions in Prelude that are good to learn and use them. zip and zipWith are two of them, when you think about something to be done using two different lists into one result list:



                  [1..] will generate the list of indices for you, it's an infinite list



                  appendIndex :: String -> Int -> String
                  appendIndex s i = (show i) ++ " :" ++ s

                  indexThem :: [String] -> [String]
                  indexThem l = zipWith appendIndex l [1..]


                  if you wanted to use zip, which is more basic but a little more verbose:



                  appendIndex :: (String,Int) -> String
                  appendIndex (s,i) = (show i) ++ " :" ++ s

                  indexThem :: [String] -> [String]
                  indexThem l = fmap appendIndex $ zip l [1..]
                  -- if you dont know about Functors yet, `fmap` is the generic way of doing `map`






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 5:48

























                  answered Nov 23 '18 at 16:30









                  Stephane RollandStephane Rolland

                  19.1k2698146




                  19.1k2698146

























                      3














                      To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:



                      numberL (x:xs) = ... ++ numberL xs


                      So you calculate numberL xs and then put something in front of it. If numberL xs were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:.... So you could never build numberL (x:xs) from numberL xs just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs.



                      The problem therefore is that it's not very useful to know numberL xs in order to calculate numberL (x:xs), due to the fact numberL always starts numbering from 1.



                      So lift that restriction. Build a function that numbers starting at n,



                      numberLFrom :: Int -> [String] -> [String]
                      numberLFrom n = ...
                      numberLFrom n (x:xs) = ...


                      Now the question you have to ask yourself is, in order to number (x:xs) starting at n you need to number xs starting at which number? And then how do you introduced the numbered x to that result?






                      share|improve this answer






























                        3














                        To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:



                        numberL (x:xs) = ... ++ numberL xs


                        So you calculate numberL xs and then put something in front of it. If numberL xs were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:.... So you could never build numberL (x:xs) from numberL xs just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs.



                        The problem therefore is that it's not very useful to know numberL xs in order to calculate numberL (x:xs), due to the fact numberL always starts numbering from 1.



                        So lift that restriction. Build a function that numbers starting at n,



                        numberLFrom :: Int -> [String] -> [String]
                        numberLFrom n = ...
                        numberLFrom n (x:xs) = ...


                        Now the question you have to ask yourself is, in order to number (x:xs) starting at n you need to number xs starting at which number? And then how do you introduced the numbered x to that result?






                        share|improve this answer




























                          3












                          3








                          3







                          To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:



                          numberL (x:xs) = ... ++ numberL xs


                          So you calculate numberL xs and then put something in front of it. If numberL xs were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:.... So you could never build numberL (x:xs) from numberL xs just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs.



                          The problem therefore is that it's not very useful to know numberL xs in order to calculate numberL (x:xs), due to the fact numberL always starts numbering from 1.



                          So lift that restriction. Build a function that numbers starting at n,



                          numberLFrom :: Int -> [String] -> [String]
                          numberLFrom n = ...
                          numberLFrom n (x:xs) = ...


                          Now the question you have to ask yourself is, in order to number (x:xs) starting at n you need to number xs starting at which number? And then how do you introduced the numbered x to that result?






                          share|improve this answer















                          To get it right, it's important to understand why you're thinking wrong. Your recursion looks like this:



                          numberL (x:xs) = ... ++ numberL xs


                          So you calculate numberL xs and then put something in front of it. If numberL xs were correct, then then it would be numbered from 1 onwards, like: 1:..., 2:..., 3:.... So you could never build numberL (x:xs) from numberL xs just by adding new elements at the front. The whole numbering would be wrong. Instead you'd have to change the whole numbering of numberL xs.



                          The problem therefore is that it's not very useful to know numberL xs in order to calculate numberL (x:xs), due to the fact numberL always starts numbering from 1.



                          So lift that restriction. Build a function that numbers starting at n,



                          numberLFrom :: Int -> [String] -> [String]
                          numberLFrom n = ...
                          numberLFrom n (x:xs) = ...


                          Now the question you have to ask yourself is, in order to number (x:xs) starting at n you need to number xs starting at which number? And then how do you introduced the numbered x to that result?







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 '18 at 16:45

























                          answered Nov 23 '18 at 16:36









                          Jorge AdrianoJorge Adriano

                          2,210918




                          2,210918






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449776%2fnumber-each-list-element-and-format-text-in-haskell%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Berounka

                              Sphinx de Gizeh

                              Different font size/position of beamer's navigation symbols template's content depending on regular/plain...