list of Data to String












0















So I have a list of Data, I know newtype is currently better but I will add more things to it. I would like to convert a list of Pack to a String.



unpack [Pack ('a','b'), Pack ('c','d') , Pack (' ', 'e') ] = "abcd e"  


I was thinking about using a foldl but am stuck trying to figure it out.



data Pack= Pack (Char, Char) deriving ( Show)
unPack:: [Pack] -> String

unpack list = foldr (Pack (a,b) -> show a + show b -> concat) "" list


Thx for the helping










share|improve this question





























    0















    So I have a list of Data, I know newtype is currently better but I will add more things to it. I would like to convert a list of Pack to a String.



    unpack [Pack ('a','b'), Pack ('c','d') , Pack (' ', 'e') ] = "abcd e"  


    I was thinking about using a foldl but am stuck trying to figure it out.



    data Pack= Pack (Char, Char) deriving ( Show)
    unPack:: [Pack] -> String

    unpack list = foldr (Pack (a,b) -> show a + show b -> concat) "" list


    Thx for the helping










    share|improve this question



























      0












      0








      0








      So I have a list of Data, I know newtype is currently better but I will add more things to it. I would like to convert a list of Pack to a String.



      unpack [Pack ('a','b'), Pack ('c','d') , Pack (' ', 'e') ] = "abcd e"  


      I was thinking about using a foldl but am stuck trying to figure it out.



      data Pack= Pack (Char, Char) deriving ( Show)
      unPack:: [Pack] -> String

      unpack list = foldr (Pack (a,b) -> show a + show b -> concat) "" list


      Thx for the helping










      share|improve this question
















      So I have a list of Data, I know newtype is currently better but I will add more things to it. I would like to convert a list of Pack to a String.



      unpack [Pack ('a','b'), Pack ('c','d') , Pack (' ', 'e') ] = "abcd e"  


      I was thinking about using a foldl but am stuck trying to figure it out.



      data Pack= Pack (Char, Char) deriving ( Show)
      unPack:: [Pack] -> String

      unpack list = foldr (Pack (a,b) -> show a + show b -> concat) "" list


      Thx for the helping







      haskell functional-programming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 16:38







      swaffelay

















      asked Nov 13 '18 at 16:37









      swaffelayswaffelay

      325




      325
























          2 Answers
          2






          active

          oldest

          votes


















          4














          Your Pack type is isomorphic to two-character strings:



          pack2String :: Pack -> String
          pack2String (Pack (a,b)) = a:[b]

          string2Pack :: String -> Pack -- partial, since String isn't limited in length
          string2Pack (a:[b]) = Pack (a, b)


          (Note that Pack (a,b) already adds an unnecessary level of wrapping; data Pack = Pack Char Char is also isomorphic to Pack (Char, Char).)



          As such, you don't actually need foldr; you can use the list monad instead.



          unpack :: [Pack] -> String
          unpack xs = xs >>= pack2String


          If you aren't yet comfortable with monads, you can just use the concatMap function directly:



          unpack :: [Pack] -> String
          unpack = concatMap pack2String





          share|improve this answer





















          • 1





            unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

            – 4castle
            Nov 13 '18 at 19:53











          • @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

            – chepner
            Nov 13 '18 at 20:21





















          4














          To pattern-patch on Pack xyz in a lambda, you need to put it in parentheses:



                      foldr ((Pack (a,b)) -> ...)


          What you wrote would actually parse as two separate arguments



                      foldr ((Pack) -> (a,b) -> ...)


          Next, you can't concatenate strings with +, that's for numbers. ++ or <> are for lists / strings.



          Then, the -> concat isn't valid syntax. What you want to do is concatenate the remainder of the foldr computation to the shown a and b. That remainder is the second argument of the folding function:



                      foldr ((Pack (a,b)) rest -> show a ++ show b ++ rest)


          ...or shorter,



                      foldr ((Pack (a,b)) -> shows a . shows b)





          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%2f53285607%2flist-of-data-to-string%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









            4














            Your Pack type is isomorphic to two-character strings:



            pack2String :: Pack -> String
            pack2String (Pack (a,b)) = a:[b]

            string2Pack :: String -> Pack -- partial, since String isn't limited in length
            string2Pack (a:[b]) = Pack (a, b)


            (Note that Pack (a,b) already adds an unnecessary level of wrapping; data Pack = Pack Char Char is also isomorphic to Pack (Char, Char).)



            As such, you don't actually need foldr; you can use the list monad instead.



            unpack :: [Pack] -> String
            unpack xs = xs >>= pack2String


            If you aren't yet comfortable with monads, you can just use the concatMap function directly:



            unpack :: [Pack] -> String
            unpack = concatMap pack2String





            share|improve this answer





















            • 1





              unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

              – 4castle
              Nov 13 '18 at 19:53











            • @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

              – chepner
              Nov 13 '18 at 20:21


















            4














            Your Pack type is isomorphic to two-character strings:



            pack2String :: Pack -> String
            pack2String (Pack (a,b)) = a:[b]

            string2Pack :: String -> Pack -- partial, since String isn't limited in length
            string2Pack (a:[b]) = Pack (a, b)


            (Note that Pack (a,b) already adds an unnecessary level of wrapping; data Pack = Pack Char Char is also isomorphic to Pack (Char, Char).)



            As such, you don't actually need foldr; you can use the list monad instead.



            unpack :: [Pack] -> String
            unpack xs = xs >>= pack2String


            If you aren't yet comfortable with monads, you can just use the concatMap function directly:



            unpack :: [Pack] -> String
            unpack = concatMap pack2String





            share|improve this answer





















            • 1





              unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

              – 4castle
              Nov 13 '18 at 19:53











            • @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

              – chepner
              Nov 13 '18 at 20:21
















            4












            4








            4







            Your Pack type is isomorphic to two-character strings:



            pack2String :: Pack -> String
            pack2String (Pack (a,b)) = a:[b]

            string2Pack :: String -> Pack -- partial, since String isn't limited in length
            string2Pack (a:[b]) = Pack (a, b)


            (Note that Pack (a,b) already adds an unnecessary level of wrapping; data Pack = Pack Char Char is also isomorphic to Pack (Char, Char).)



            As such, you don't actually need foldr; you can use the list monad instead.



            unpack :: [Pack] -> String
            unpack xs = xs >>= pack2String


            If you aren't yet comfortable with monads, you can just use the concatMap function directly:



            unpack :: [Pack] -> String
            unpack = concatMap pack2String





            share|improve this answer















            Your Pack type is isomorphic to two-character strings:



            pack2String :: Pack -> String
            pack2String (Pack (a,b)) = a:[b]

            string2Pack :: String -> Pack -- partial, since String isn't limited in length
            string2Pack (a:[b]) = Pack (a, b)


            (Note that Pack (a,b) already adds an unnecessary level of wrapping; data Pack = Pack Char Char is also isomorphic to Pack (Char, Char).)



            As such, you don't actually need foldr; you can use the list monad instead.



            unpack :: [Pack] -> String
            unpack xs = xs >>= pack2String


            If you aren't yet comfortable with monads, you can just use the concatMap function directly:



            unpack :: [Pack] -> String
            unpack = concatMap pack2String






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 20:22

























            answered Nov 13 '18 at 16:48









            chepnerchepner

            247k32233326




            247k32233326








            • 1





              unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

              – 4castle
              Nov 13 '18 at 19:53











            • @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

              – chepner
              Nov 13 '18 at 20:21
















            • 1





              unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

              – 4castle
              Nov 13 '18 at 19:53











            • @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

              – chepner
              Nov 13 '18 at 20:21










            1




            1





            unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

            – 4castle
            Nov 13 '18 at 19:53





            unpack xs = xs >>= pack2String would make more sense to me as the list monad example.

            – 4castle
            Nov 13 '18 at 19:53













            @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

            – chepner
            Nov 13 '18 at 20:21







            @4castle I'll comment later if I can think of a good excuse for not saying that in the first place :) (Right now, I'll blame lack of coffee and/or good breakfast.)

            – chepner
            Nov 13 '18 at 20:21















            4














            To pattern-patch on Pack xyz in a lambda, you need to put it in parentheses:



                        foldr ((Pack (a,b)) -> ...)


            What you wrote would actually parse as two separate arguments



                        foldr ((Pack) -> (a,b) -> ...)


            Next, you can't concatenate strings with +, that's for numbers. ++ or <> are for lists / strings.



            Then, the -> concat isn't valid syntax. What you want to do is concatenate the remainder of the foldr computation to the shown a and b. That remainder is the second argument of the folding function:



                        foldr ((Pack (a,b)) rest -> show a ++ show b ++ rest)


            ...or shorter,



                        foldr ((Pack (a,b)) -> shows a . shows b)





            share|improve this answer




























              4














              To pattern-patch on Pack xyz in a lambda, you need to put it in parentheses:



                          foldr ((Pack (a,b)) -> ...)


              What you wrote would actually parse as two separate arguments



                          foldr ((Pack) -> (a,b) -> ...)


              Next, you can't concatenate strings with +, that's for numbers. ++ or <> are for lists / strings.



              Then, the -> concat isn't valid syntax. What you want to do is concatenate the remainder of the foldr computation to the shown a and b. That remainder is the second argument of the folding function:



                          foldr ((Pack (a,b)) rest -> show a ++ show b ++ rest)


              ...or shorter,



                          foldr ((Pack (a,b)) -> shows a . shows b)





              share|improve this answer


























                4












                4








                4







                To pattern-patch on Pack xyz in a lambda, you need to put it in parentheses:



                            foldr ((Pack (a,b)) -> ...)


                What you wrote would actually parse as two separate arguments



                            foldr ((Pack) -> (a,b) -> ...)


                Next, you can't concatenate strings with +, that's for numbers. ++ or <> are for lists / strings.



                Then, the -> concat isn't valid syntax. What you want to do is concatenate the remainder of the foldr computation to the shown a and b. That remainder is the second argument of the folding function:



                            foldr ((Pack (a,b)) rest -> show a ++ show b ++ rest)


                ...or shorter,



                            foldr ((Pack (a,b)) -> shows a . shows b)





                share|improve this answer













                To pattern-patch on Pack xyz in a lambda, you need to put it in parentheses:



                            foldr ((Pack (a,b)) -> ...)


                What you wrote would actually parse as two separate arguments



                            foldr ((Pack) -> (a,b) -> ...)


                Next, you can't concatenate strings with +, that's for numbers. ++ or <> are for lists / strings.



                Then, the -> concat isn't valid syntax. What you want to do is concatenate the remainder of the foldr computation to the shown a and b. That remainder is the second argument of the folding function:



                            foldr ((Pack (a,b)) rest -> show a ++ show b ++ rest)


                ...or shorter,



                            foldr ((Pack (a,b)) -> shows a . shows b)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 16:47









                leftaroundaboutleftaroundabout

                79.3k3117233




                79.3k3117233






























                    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%2f53285607%2flist-of-data-to-string%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly