Haskell中的海盗语言

时间:2016-09-04 05:50:19

标签: haskell

我正在尝试创建一个更改

的函数
  

“猫”到“cocatot”

如果检测到辅音,则加上'o',然后重复相同的辅音。

这是我到目前为止所提出的。

更新(清洁版)

isConsonant letter = notElem letter "aeiouy"
sky :: [a] -> [a]
--sky [a] = [a]
sky [] = []
sky (x:rest) = if isConsonant x then x : 'o' : [x] ++ sky rest else x : sky rest

并获得这些错误

 * Couldn't match expected type `Char' with actual type `a'
      `a' is a rigid type variable bound by
        the type signature for:
          sky :: forall a. [a] -> [a]

 * In the first argument of `isConsonant', namely `(x)'
      In the expression: isConsonant (x)
      In the expression:
        if isConsonant (x) then
            x : ('o' : [x]) ++ sky (rest)
        else
            x : sky (rest)

非常感谢任何帮助。我是Haskell的初学者,我无处可去。

1 个答案:

答案 0 :(得分:3)

要回答你输入检查错误。您声明skya的任何列表一起使用,但isConsonant仅与Char一起使用,因此sky只能在Char的情况下使用列表太

解决方案:将sky类型签名修复为String -> String,或者甚至将其删除。

相关问题