Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to turn lists inside of a list into strings

I have a list that i partitioned with a function and I want to turn the lists inside of a list into strings.

list : [[0,0],[0,1]]

If i use show list, I will get "[[0,0],[0,1]]", which turns the whole list into a string. What output I want to get is ["[0,0]","[0,1]"], which turns only the objects inside the list into strings. I’m assuming I could use show in the function I use to partition the list but I’m not sure how to accomplish this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function that I am using

partition :: Int -> [a] -> [[a]]
partition n = go
    where go [] = []
          go xs = ys : go yss
              where (ys, yss) = splitAt n xs

I assumed I could just change [[a]] to [string], but that didn’t work. I don’t really have a clue on how I could even insert show in this function either.

Is there a way to use show but only on the objects inside a list? If not I’d like to get a tip on how to edit this function to work

>Solution :

Is there a way to use show but only on the objects inside a list?

Yes, you can just map the show function, to get a list of strings of the form that you say you want.

That is:

map show list

should give you what you’re looking for.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading