heiLou
heiLou

Reputation: 207

String to list of characters

I was wondering if I can convert a string to a list of characters?

"jt5x=!" -> ["j","t","5","x","=","!"]

Essentially, it would be?

example :: String -> [Char]

Upvotes: 12

Views: 20047

Answers (1)

Franky
Franky

Reputation: 2421

(Collecting comments into an answer)

Because in haskell, a String is a list of characters, i.e. [Char], just returning the input as given will do.

example = id

does what you want. Note that id is defined as

id x = x

Your example "jt5x=!" -> ["j","t","5","x","=","!"] does not match the description: Double quotes "" enclose Strings not single Characters. For characters use single quotes '. You can type

"jt5x=!" == ['j','t','5','x','=','!']

into GHCi and see it returns True. Type map (:[]) "jt5x=!" to actually see ["j","t","5","x","=","!"].

Upvotes: 11

Related Questions