1

How can I split a string into another string without using Data.List.Split function? To be more concrete: to turn "Abc" into "['A','b','c']"

3 Answers 3

5

If you want literally the string "['A','b','c']" as opposed to the expression ['A','b','c'] which is identical to "Abc" since in Haskell the String type is a synonym for [Char], then something like the following will work:

'[': (intercalate "," $ map show "Abc") ++ "]" 

The function intercalate is in Data.List with the type

intercalate :: [a] -> [[a]] -> [a] 

It intersperses its first argument between the elements of the list given as its second argument.

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

Comments

3

I assume you meant how to turn "Abc"into ["A", "b", "c"]. This is quite simple, if the string to split is s, then this will do the trick:

map (\x -> [x]) s 

3 Comments

or perhaps the OP meant turn "Abc" into ['A','b','c'] which can be done with \x->x or if you want to go point free: id. :-)
actually i need exactly what is written in example
What Philip JF means: In Haskell String is simply a list of Chars. Thus, "Abc" is exactly the same as ['A', 'b', 'c'].
3

Fire up ghci to see that the expressions you wrote are the same:

Prelude> ['A','b','c'] "Abc" 

Comments