【发布时间】:2012-10-19 12:38:56
【问题描述】:
可能重复:
Iterating through a String and replacing single chars with substrings in haskell
我正在尝试实现一个查看字符串 ([Chars]) 并检查每个字母是否应该用另一个字符串替换的函数。例如,我们可能有一个由“XYF”和“X = HYHY”、“Y = OO”的规则组成的 [Chars],那么我们的输出应该变成“HYHYOOF”。
我想使用我定义的以下两种类型:
type Letters = [Char]
data Rule = Rule Char Letters deriving Show
我的想法是,使用警卫的函数应该如下所示。但是问题是,当我想浏览我的所有规则以查看它们中的任何一个是否适合当前字母 x 时,我找不到任何关于递归调用应该是什么样子的信息。我希望任何人都可以提供一些关于符号如何进行的提示。
apply :: Letters -> [Rule] -> Letters
apply _ _ = []
apply (x:xs) (Rule t r:rs)
| x /= t = apply x (Rule t rs)
| x == t = r++rs:x
| otherwise =
【问题讨论】:
标签: haskell types recursion notation