【问题标题】:How to split a string on spaces in Clean?如何在 Clean 中的空格上拆分字符串?
【发布时间】:2014-10-14 16:59:51
【问题描述】:

我是函数式编程和 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'')

【问题讨论】:

  • 好吧,如果你在 CLEAN 中实现了 break 函数,那么你就可以完成这个函数,是吗?然后去看看haskell的break定义。如果我没记错的话,它不会使用很多花哨的技巧,而且很容易理解
  • 好像有人有类似的问题stackoverflow.com/questions/26357128/…
  • 如果我要实现 break,那么仍然是突出显示的句法结构,我不知道它是如何工作的
  • 另一个我认为它不起作用的帖子,因为在 spliton 中,您无法在语义上将字符串作为列表处理,会产生编译错误
  • Haskell 的 break 函数与 Clean 的 span 函数非常相似。但是跨度正在处理列表并制作列表(字符)列表,之后我无法在另一个步骤中合并列表,或者我不知道如何......

标签: functional-programming clean-language


【解决方案1】:

正如StdEnvApi document 建议的那样,您应该将字符串转换为列表以使用 StdList API 函数(第 6 节,第 20 页)。 结果如下:

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

【讨论】:

    猜你喜欢
    • 2015-11-26
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多