سؤال

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