[Haskell-cafe] Separate a string into a list of strings

J. Garrett Morris trevion at gmail.com
Mon Jun 12 17:15:57 EDT 2006


Off the top of my head:

separate :: String -> [String]
separate [] = []
separate s =
  case break (',' ==) s of
    (s,[]) -> [s]
    (s,',':s') -> s : separate s'
    _ -> error "how did we get here?"

There is at least one cunning rewriting with foldl, I think, but I
think this version is clearer.

 /g

On 6/12/06, Sara Kenedy <sarakenedy at gmail.com> wrote:
> Hi all,
>
> I want to write a function to separate a string into a list of strings
> separated by commas.
>
> Example:
> separate :: String -> [String]
>
> separate "Haskell, Haskell, and Haskell" = ["Haskell", "Haskell", "and Haskell"]
>
> If anyone has some ideas, please share with me. Thanks.
>
> S.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


-- 
We have lingered in the chambers of the sea	
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.


More information about the Haskell-Cafe mailing list