Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to list of characters

Tags:

haskell

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]
like image 894
heiLou Avatar asked Mar 30 '15 10:03

heiLou


People also ask

Can you convert a string to a list?

You can also convert a string to a list using a separator with the split() method. The separator can be any character you specify. The string will separate based on the separator you provide. For example, you can use a comma, , , as the separator.

How do I convert a string to a list of strings?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

How do you split a string into a list of elements?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


1 Answers

(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","=","!"].

like image 188
Franky Avatar answered Nov 05 '22 06:11

Franky