【问题标题】:How to implement split function Haskell?Haskell如何实现拆分功能?
【发布时间】:2020-07-31 11:49:16
【问题描述】:

我想实现一个 Haskell 函数 wordToken,它将一个单词字符串拆分为一个字符串列表,包括句号和逗号

例如"the man saw." 应该导致["the", "man","saw","."]

所以我所做的是检查 Char 是逗号还是句号,然后按原样添加。 然后,如果它是一个 Char ,然后是一个 Char ,则将它们都添加。 否则,如果它是一个字符,然后是一个空格,则添加它并继续到列表的其余部分。 但我不知道如何告诉它分开单词本身,或者当我将一个 char 添加到一个 char 时,那就是一个新字符串

 wordToken []= " "

 wordToken (x:y:z) | x==',' || x=='.' = " "(++)x:wordToken( y:z)
              | x/='\n' && y/='\n'= " "(++)x(++)y(++)wordToken z
              | x/='\n' && y=='\n'= " "(++)x:wordToken z
              |     otherwise = wordToken z 

我也尝试使用 words 函数,只添加标点符号的一部分,但它给了我一个类型不匹配 wordToken (x:xs) | x=='.' || x==',' = 'x':wordToken xs |否则 =words (x:xs)

【问题讨论】:

  • 你在 Haskell 中查找过正则表达式或解析吗?
  • 不,我没有查过,但我会检查一下
  • wordToken []= " " wordToken (x:y:z) | x==',' || x=='.' = " "(++)x:wordToken(y:z) | x/='\n' && y/='\n'= ""(++)x(++)y(++)wordToken z | x/='\n' && y=='\n'= " "(++)x:wordToken z |否则 = wordToken z
  • 请将您的尝试放在问题部分。注意它做了什么而不是你期望它做什么。

标签: string function haskell split char


【解决方案1】:

为了改进您的想法,我建议使用带有累加器的辅助函数,它将当前字符存储到下一个分隔符。一旦到达字符串的末尾或另一个分隔符,就将累加的新单词添加到列表中,并将累加器重置为""

wordToken :: String -> [String]
wordToken "" = [] -- empty list
wordToken str = helper str "" -- start helper with empty current word
    where helper :: String -> String -> [String]
          -- when the entire string is consumed
          helper "" ""      = [] -- if no current word, append nothing
          helper "" current = [current] -- if current word, append this to the list
          -- otherwise
          helper (x:xs) current
              | x == ',' || x == '.' = current : [x] : helper xs "" -- add comma or fullstop as extra word
              | x == ' '             = current : helper xs "" -- but skip on whitespaces
              | otherwise            = helper xs (current ++ [x]) -- if no seperator, just continue building up the current word

这会产生预期的输出:

wordToken "the man saw."
> ["the", "man", "saw", "."]

【讨论】:

  • 我可以删除我的问题吗?
  • 这通常不受欢迎,因为回答您问题的人通常会努力解决您的问题。
  • 是的,但这是我第一次来这里,所以我不知道这个规则,很抱歉给您带来不便,但因为我刚刚意识到如果我发布了我的代码,这可能会被视为我项目的抄袭跨度>
  • 不用担心。您没有发布最终代码,同时,您可以查找一些内容。
  • 您是否可以删除您的答案,因为这是我删除它的唯一方法? @Erich
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 2020-03-18
  • 2023-03-22
  • 2021-04-15
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
相关资源
最近更新 更多