我正在尝试创建一个更改
的函数“猫”到“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的初学者,我无处可去。
答案 0 :(得分:3)
要回答你输入检查错误。您声明sky
与a
的任何列表一起使用,但isConsonant
仅与Char
一起使用,因此sky
只能在Char
的情况下使用列表太
解决方案:将sky
类型签名修复为String -> String
,或者甚至将其删除。