Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell string to list [duplicate]

Tags:

list

haskell

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 [#,#,%,%]?

like image 671
thetux4 Avatar asked Apr 15 '11 16:04

thetux4


3 Answers

Use intersperse

myShow :: String -> String
myShow s = concat ["[", intersperse ',' s, "]"]
like image 39
dave4420 Avatar answered Oct 10 '22 15:10

dave4420


import Data.Char (digitToInt)

map digitToInt "12345"
like image 158
Phil Avatar answered Oct 10 '22 15:10

Phil


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]
like image 6
Santiago Alessandri Avatar answered Oct 10 '22 15:10

Santiago Alessandri