1

I was looking at this post here: Haskell get character array from string?

I see it says that in haskell strings are essentially arrays containing each letter, but I was wondering; how would I turn the format from the string to an array of individual components, for example:

["ABCD","EFGH"]

to

[["A","B","C","D"],["E","F","G","H"]]

I'd like to know a method without using any external imports.

2
  • 2
    Do you know how to turn 'A' into "A"? Commented Apr 13, 2021 at 16:12
  • Strings are lists of Char, not arrays. And ["E","F","G","H"] is a list of String, not an array. Just to avoid future confusion. Commented Apr 13, 2021 at 18:21

1 Answer 1

2

You can wrap each element in a singleton list, so:

map (map pure) ["ABCD", "EFGH"] :: [[String]]

this then produces:

Prelude> map (map pure) ["ABCD", "EFGH"] :: [[String]]
[["A","B","C","D"],["E","F","G","H"]]

That being said, a String is simply a list of Chars, indeed:

type String = [Char]

so if you just want to work with a list of Chars, you can simply work with the string directly. By converting it to a list of list of Strings, we know that all these strings contain one Char, but that is no longer guaranteed by the type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.