Search code examples
haskellfunctional-programmingcharacteralphabetletter

String contains only the alphabet in haskell


I want to make a functions which decides wheter the character is a letter in the alphabet in Haskell

isLetter :: Char -> Bool

So the outcome may look like this: if I type isLetter 'c', the outcome will be True, and if it's isLetter ' ', then the outcome is False.


Solution

  • You can define this function yourself,

    isLetter :: Char -> Bool
    isLetter 'A' = True
    isLetter 'B' = True
    ...
    isLetter 'Z' = True
    

    But what about lowercase letters? What about foreign letters like 'ß', 'ü', 'Ф', 'Й' or 'Æ' and their uppercase/lowercase variants? Because Unicode is so complex, for any production-level use you should depend on already built library functions. When you don't know what they're called, go to Hoogle and search for "Char -> Bool" and you'll often be pointed to a library that contains exactly what you want. :-)

    For example, the documentation for Data.Char.isAlpha says:

    Selects alphabetic Unicode characters (lower-case, upper-case and title-case letters, plus letters of caseless scripts and modifiers letters). This function is equivalent to Data.Char.isLetter.

    I bet you didn't think of title-case letters, letters of caseless scripts and modifier letters! (I didn't.)

    I didn't even know modifier letters existed. Apparently, 'ˀ' (glottal stop) is a letter used by the Squamish people of southwestern British Columbia, Canada. There are Canadian road signs out there that your function isn't taking into account!

    Are you gonna be the guy who tells Ḇøᵇ that he can't get on the plane?! :Đ