开发者

Haskell string to list [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

convert string list to int list in haskell

I have a string 12345. How can I show it in list fo开发者_如何学JAVArm like [1,2,3,4,5]? And also what if i have a string like ##%%? I can't convert it to Int. How can view it in the form [#,#,%,%]?


import Data.Char (digitToInt)

map digitToInt "12345"


Use intersperse

myShow :: String -> String
myShow s = concat ["[", intersperse ',' s, "]"]


You should map the function read x::Int to each of the elements of the string as a list of chars:

map (\x -> read [x]::Int) "1234"

If you have non-digit characters you should filter it first like this:

import Data.Char
map (\x -> read [x]::Int) (filter (\x -> isDigit x) "1234##56")

That results in:

[1,2,3,4,5,6]


Have you taken a look at this answer? Convert string list to int list

A String is just a list of characters in Haskell after all. :)


Try using splitEvery with length 1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜