简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM