How to split a string in Haskell?

后端 未结 13 1617
日久生厌
日久生厌 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:41

    Try this one:

    import Data.List (unfoldr)
    
    separateBy :: Eq a => a -> [a] -> [[a]]
    separateBy chr = unfoldr sep where
      sep [] = Nothing
      sep l  = Just . fmap (drop 1) . break (== chr) $ l
    

    Only works for a single char, but should be easily extendable.

提交回复
热议问题