Split String by delimiter in Haskell I wrote a split by delimiter function in Haskell and wanted some feedback on this piece of code.
Since I come from an imperative programming background, I often write too complex functions in haskell.
split :: Char -&gt; String -&gt; [String]
split c str = fst $ splitInternal c ([], str)

splitInternal :: Char -&gt; ([String], String) -&gt; ([String], String)
splitInternal _ (result, &quot;&quot;)  = (result, &quot;&quot;)
splitInternal c (result, str) = splitInternal c (
    result ++ [takeWhile (/= c) str], 
    case dropWhile (/= c) str of
        &quot;&quot; -&gt; &quot;&quot;
        rest -&gt; tail rest
    )

My questions are

Is it bad to have splitInternal function? I couldn't figure out a way without it.
Is there maybe a simpler way to write the function?
Any other feedback is welcome as well
Is it bad to have splitInternal function? I couldn't figure out a way without it.

Well, according to your procedure, I think it is necessary, but you can improve the readibility by writing some small functions and then combining them together. Besides, if there are consecutive delimiters, your split function doesn't work as expected. The code can be rewritten as following:
splitInternal :: Char -&gt; ([String], String) -&gt; ([String], String)
splitInternal _ (result, &quot;&quot;) = (result, &quot;&quot;)
splitInternal c (result, remain) = splitInternal c (getBefore c remain, getAfter c remain)
  where
    getBefore delimiter rest = result ++ [takeWhile (/= delimiter) rest]
    getAfter delimiter rest = dropWhile (== delimiter) . dropWhile (/= delimiter) $ rest


Is there maybe a simpler way to write the function?

Yes, you can use the break and span function defined in Prelude:
split :: Char -&gt; String -&gt; [String]
split _ &quot;&quot; = []
split delimiter str = 
    let (start, rest) = break (== delimiter) str
        (_, remain) = span (== delimiter) rest
     in start : split delimiter remain

So in this case, your splitInternal is unnecessary.

Any other feedback is welcome as well

Well, if you are dealing with string, then a better choice is Text from Data.Text. Text is more efficient than String when you are dealing with string. In the module Data.Text, there is a pre-defined function splitOn that works almost as you expect:
ghci&gt; :seti -XOverloadedString

ghci&gt; splitOn &quot;,&quot; &quot;123,456,789&quot;
[&quot;123&quot;,&quot;456&quot;,&quot;789&quot;]

ghci&gt; splitOn &quot;,&quot; &quot;123,,,456,789&quot;
[&quot;123&quot;,&quot;&quot;,&quot;&quot;,&quot;456&quot;,&quot;789&quot;]           -- This is what I mean &quot;almost&quot;, since splitOn doesn't use the consecutive delimiters. Maybe this is what you want.
```