1 haskell functional-programming
我正在尝试编写一个haskell程序,它接受一个字符串并返回一个只包含元音的新字符串,但是我收到了这个错误.
`Char' is applied to too many type arguments
In the type signature for `vowels': vowels :: Char a => a -> a
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
vowels :: (Char a) => a -> a
vowels str = [ x | x <- str, x `elem` ['A','E','I','U','Y','O']]
Run Code Online (Sandbox Code Playgroud)
你的类型签名应该是
vowels :: [Char] -> [Char]
vowels str = [ x | x <- str, x `elem` ['A','E','I','U','Y','O']]
Run Code Online (Sandbox Code Playgroud)
要么
vowels :: String -> String
Run Code Online (Sandbox Code Playgroud)
在您的代码中,(Char a) => ...表示该类型a应该是类型类的实例Char.但是,Char是一种数据类型,而不是类型类,这就是您收到错误的原因.由于您不需要任何类来实现此功能,因此您可以删除约束.