質問

I'm trying to write a haskell program that takes in a string and returns a new string that only contains the vowels but I'm getting this error.

`Char' is applied to too many type arguments
In the type signature for `vowels': vowels :: Char a => a -> a

Here's my code:

vowels :: (Char a) => a -> a
vowels str = [ x | x <- str, x `elem` ['A','E','I','U','Y','O']]
役に立ちましたか?

解決

Your type signature should be

vowels :: [Char] -> [Char]
vowels str = [ x | x <- str, x `elem` ['A','E','I','U','Y','O']]

or

vowels :: String -> String

In your code, (Char a) => ... indicates that the type a should be an instance of the typeclass Char. However, Char is a data type, not a type class which is why you get the error. Since you don't need any classes to implement this function you can just remove the constraint.

他のヒント

Is this what you're trying to achieve?

vowels :: String -> [Char]
vowels = let
             y =  ['A','a','E','e','I','i','U','u','Y','y','O',o']
         in filter.(\x -> elem x y)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top