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
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.
Comments
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
Philip JF
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. :-)bzn
What Philip JF means: In Haskell
String is simply a list of Chars. Thus, "Abc" is exactly the same as ['A', 'b', 'c'].