user721588
user721588

Reputation:

split string into string in haskell

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']"

Upvotes: 1

Views: 539

Answers (3)

Chris Taylor
Chris Taylor

Reputation: 47382

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

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

Upvotes: 3

Dan Feltey
Dan Feltey

Reputation: 425

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.

Upvotes: 5

tofcoder
tofcoder

Reputation: 2382

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

Upvotes: 3

Related Questions