Data.Char

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 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"
True

ghci 3> all isAlphaNum "eddy the fish!"
False

GeneralCategory

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 ’ ’
Space


ghci 6> generalCategory ’A’
UppercaseLetter


ghci 7> generalCategory ’a’
LowercaseLetter


ghci 8> generalCategory ’.’
OtherPunctuation


ghci 9> generalCategory ’9’
DecimalNumber

toUpper, toLower :

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


ghci 10> map toUpper "wow that’s great r2-d2"
"WOW THAT’S GREAT R2-D2"

toLower converts a character to lower-case.


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

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : I am starting online training course for Protractor with Typescript from 15th November 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn then you can join the course by sending email to chercher.tech@gmail.com

or Register below


Subscribe
 
Join My Facebook Group
Join Group