Search code examples
functional-programmingclean-language

How to split a string on spaces in Clean?


I'm a newbie with functional programming and Clean. I want to split a string on whitespace, like the words function in Haskell.

words :: String -> [String]
input: "my separated list " 
output: ["my","separated","list"]

This is the definition in Haskell:

words :: String -> [String]
words s =  case dropWhile {-partain:Char.-}isSpace s of
             "" -> []
             s' -> w : words s''
                where (w, s'') =
                    break {-partain:Char.-}isSpace s'

But Clean doesn't have break, and I dont know what it means, and how to implement it in Clean:

s' -> w : words s''
where (w, s'')

Solution

  • As the StdEnvApi document advises you should convert the String to a list to use the StdList API functions (section 6, page 20). This results in something like this:

    splitString :: String -> [String]
    splitString x = [foldr (+++) "" i\\i<- splitString` (fromString x)]
        where
            splitString` :: [String] -> [[String]]
            splitString` x = let (p, n) = span ((<>) " ") x in
                if (isEmpty n) [p] [p:splitString` (tl n)]