[英]How to split a string on spaces in Clean?
I'm a newbie with functional programming and Clean. 我是函数编程和Clean的新手。 I want to split a string on whitespace, like the
words
function in Haskell. 我想在空白上拆分字符串,就像Haskell中的
words
函数一样。
words :: String -> [String]
input: "my separated list "
output: ["my","separated","list"]
This is the definition in Haskell: 这是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: 但是Clean没有
break
,并且我不知道它的含义以及如何在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). 正如StdEnvApi文档所建议的那样,您应该将String转换为列表以使用StdList API函数(第20页第6节)。 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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.