1

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'')
Thomas
  • 366
  • 6
  • 19
  • 1
    Well, if you implement the break function in CLEAN then you'd be able to finish this function, yes? Then go look at the definition for break for haskell. If I remember correctly it doesn't use many fancy tricks and is pretty simple to understand – bheklilr Oct 14 '14 at 17:13
  • 1
    looks like someone had a similar question http://stackoverflow.com/questions/26357128/split-string-to-a-list-of-strings-in-clean – shakirthow Oct 14 '14 at 17:15
  • if i would implement break, then remains the highlighted syntactical construction which i dont know how working – Thomas Oct 14 '14 at 17:36
  • the another post i dont think it's working because in the spliton' you cannot handling String as a list semantically, generates compilation error – Thomas Oct 14 '14 at 18:00
  • Haskell's break function is very similar to Clean's span function. But span is working on list and makes list of lists(of chars), after that i cannot merge the lists in another step, or i dont know how... – Thomas Oct 14 '14 at 18:07

1 Answers1

1

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)]