Data Char in Haskell

The Data.Char module does what its name suggests: it exports functions that deal with characters. But they’re also helpful when filtering and mapping over strings because they’re just lists of characters

ghci 1> import Data.Char

Predicates over characters :

Here are some of the exported predicates over characters (functions of type Char -> Bool):

  • isSpace isSpace checks whether a character is a white-space character; that includes spaces, tab characters, newlines etc.
  • isLower isLower checks whether a character is a lower-case character
  • isUpper isUpper checks whether a character is an upper-case character
  • isAlpha isAlpha checks whether a character is a letter
  • isAlphaNum isAlphaNum checks whether a character is a letter or a number
  • isDigit isDigit checks whether a character is a digit
  • isLetter isLetter checks whether a character is a letter
  • isNumber isNumber checks whether a character is numeric
  • isPunctuation isPunctuation checks whether a character is a punctuation
  • isSymbol isSymbol checks whether a character is a fancy mathematical or currency symbol

Most of the time, we use these to filter out strings.

ghci 2> all isAlphaNum "bobby283"

ghci 3> all isAlphaNum "eddy the fish!"


The Data.Char also exports a datatype GeneralCategory that’s like Ordering. The Ordering type can have a value of LT, EQ or GT – a sort of enumeration. It describes a few possible results that can arise from comparing two elements.

The GeneralCategory type is also an enumeration. It presents us with a few possible categories that a character can fall into. The main function for getting the general category of a character is generalCategory. Its type is listed below

ghci 4> : t generalCategory
generalCategory :: Char → GeneralCategory

ghci 5> generalCategory ’ ’

ghci 6> generalCategory ’A’

ghci 7> generalCategory ’a’

ghci 8> generalCategory ’.’

ghci 9> generalCategory ’9’

toUpper, toLower :

toUpper converts a character to an upper-case. Spaces, numbers, and the like remain unchanged.

ghci 10> map toUpper "wow that’s great r2-d2"

toLower converts a character to a lower-case.

ghci 11> map toLower "WOW THAT’S GREAT R2-D2"
"wow that’s great r2-d2"
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions