Haskell string to list [duplicate]

泄露秘密 提交于 2020-01-12 07:53:46

问题


Possible Duplicate:
convert string list to int list in haskell

I have a string 12345. How can I show it in list form 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 [#,#,%,%]?


回答1:


Use intersperse

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



回答2:


import Data.Char (digitToInt)

map digitToInt "12345"



回答3:


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]



回答4:


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. :)




回答5:


Try using splitEvery with length 1



来源:https://stackoverflow.com/questions/5679797/haskell-string-to-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!