[英]How to split a string on spaces in Clean?
我是函數編程和Clean的新手。 我想在空白上拆分字符串,就像Haskell中的words
函數一樣。
words :: String -> [String]
input: "my separated list "
output: ["my","separated","list"]
這是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'
但是Clean沒有break
,並且我不知道它的含義以及如何在Clean中實現它:
s' -> w : words s''
where (w, s'')
正如StdEnvApi文檔所建議的那樣,您應該將String轉換為列表以使用StdList API函數(第20頁第6節)。 結果是這樣的:
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.