How to split a string in Haskell?

后端 未结 13 1572
日久生厌
日久生厌 2020-11-28 03:08

Is there a standard way to split a string in Haskell?

lines and words work great from splitting on a space or newline, but surely there is

13条回答
  •  自闭症患者
    2020-11-28 03:48

    Use Data.List.Split, which uses split:

    [me@localhost]$ ghci
    Prelude> import Data.List.Split
    Prelude Data.List.Split> let l = splitOn "," "1,2,3,4"
    Prelude Data.List.Split> :t l
    l :: [[Char]]
    Prelude Data.List.Split> l
    ["1","2","3","4"]
    Prelude Data.List.Split> let { convert :: [String] -> [Integer]; convert = map read }
    Prelude Data.List.Split> let l2 = convert l
    Prelude Data.List.Split> :t l2
    l2 :: [Integer]
    Prelude Data.List.Split> l2
    [1,2,3,4]
    

提交回复
热议问题