I want to create a function consonant which check, whether an char is an consonant(True) or a vowel(False). I try something like this:
consonant :: Char -> Bool
consonant x = if (x == 'a'|'e'|'i'|'o'|'u'|' ') then False else True
But it doesn't work, i think it is just a problem with the Syntax. I hope anyone can help me :) Thanks
The expression x == 'a'|'e'|'i'|'o'|'u'|' '
does not make sense, the pipe character (|
) is used for guards, or when defining several data constructors for a data type.
Replacing |
with (||) :: Bool -> Bool -> Bool
will not help either, since 'e'
is not a Bool
. We can however write several conditions:
consonant :: Char -> Bool
consonant x = not (x == 'a'|| x == 'e'|| x == 'i'|| x =='o'|| x =='u'|| x ==' ')
or we can make use of elem :: (Eq a, Foldable f) => a -> f a -> Bool
:
consonant :: Char -> Bool
consonant = not . flip elem "aeiou "
or we can make use of an operator sectioning as @leftroundabout suggests:
consonant :: Char -> Bool
consonant = not . (`elem` "aeiou ")
or do the "flipping" ourselves with a variable:
consonant :: Char -> Bool
consonant x = not (elem x "aeiou ")
Or we can make use notElem :: (Eq a, Foldable f) => a -> f a -> Bool
which negates the elem
:
consonant :: Char -> Bool
consonant = (`notElem` "aeiou ")