简体   繁体   中英

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

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM