可能重複:
convert string list to int list in haskellHaskell的字符串列表
嗨,
我有一個字符串 「12345」。我如何以列表形式顯示它[1,2,3,4,5]?還有,如果我有像「## %%」這樣的字符串呢?我無法將其轉換爲int。如何以[#,#,%,%]的形式查看它?
可能重複:
convert string list to int list in haskellHaskell的字符串列表
嗨,
我有一個字符串 「12345」。我如何以列表形式顯示它[1,2,3,4,5]?還有,如果我有像「## %%」這樣的字符串呢?我無法將其轉換爲int。如何以[#,#,%,%]的形式查看它?
myShow :: String -> String
myShow s = concat ["[", intersperse ',' s, "]"]
嘗試使用splitEvery長度爲1
import Data.Char (digitToInt)
map digitToInt "12345"
Thx,學到了新的東西。 – markmywords 2011-04-15 16:49:12
如果我沒有數字怎麼辦? – thetux4 2011-04-15 17:47:28
你已經採取了看看這個答案嗎? Convert string list to int list
字符串只是Haskell中的一個字符列表。 :)
您應該映射函數讀取X ::詮釋每個字符串的元素作爲一個字符列表:
map (\x -> read [x]::Int) "1234"
如果你有非數字字符,你應該先喜歡它過濾這樣的:
import Data.Char
map (\x -> read [x]::Int) (filter (\x -> isDigit x) "1234##56")
導致:
[1,2,3,4,5,6]
這些不重複。一種是將字符串轉換爲列表,另一種是將字符串列表轉換爲列表。 – Scott 2011-05-01 14:20:52
在Haskell中,「12345」實際上是['1','2','3','4','5']。這是你想要的還是它[[1,2,3,4,5]]? – 2012-01-02 13:20:46