stringhaskellsplit

What is the best way to split a string by a delimiter functionally?


I tried to write the program in Haskell that will take a string of integer numbers delimitated by comma, convert it to list of integer numbers and increment each number by 1.

For example "1,2,-5,-23,15" -> [2,3,-4,-22,16]

Below is the resulting program

import Data.List

main :: IO ()
main = do
  n <- return 1
  putStrLn . show . map (+1) . map toInt . splitByDelimiter delimiter
    $ getList n

getList :: Int -> String
getList n = foldr (++) [] . intersperse [delimiter] $ replicate n inputStr

delimiter = ','

inputStr = "1,2,-5,-23,15"

splitByDelimiter :: Char -> String -> [String]
splitByDelimiter _ "" = []
splitByDelimiter delimiter list =
  map (takeWhile (/= delimiter) . tail)
    (filter (isPrefixOf [delimiter])
       (tails
           (delimiter : list)))

toInt :: String -> Int
toInt = read

The most hard part for me was programming of function splitByDelimiter that take a String and return list of Strings

"1,2,-5,-23,15" -> ["1","2","-5","-23","15"]

Thought it is working, I am not happy with the way it is written. There are a lot of parentheses, so it looks Lisp like. Also the algorithm is somewhat artificial:

  1. Prepend delimiter to beginning of string ",1,2,-5,-23,15"

  2. Generate list of all tails [",1,2,-5,-23,15", "1,2,-5,-23,15", ",2,-5,-23,15", .... ]

  3. Filter and left only strings that begins with delimiter [",1,2,-5,-23,15", ",2,-5,-23,15", .... ]

  4. Drop first delimiter and take symbols until next delimiter will be met ["1", "2", .... ]

So the questions are:

How I can improve function splitByDelimiter?

Can I remove prepend and drop of delimiter and make direct split of string?

How I can rewrite the function so there will be less parentheses?

May be I miss something and there are already standard function with this functionality?


Solution

  • Doesn't Data.List.Split.splitOn do this?