HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerio-0.1.22.0Haskell2010

RIO.Char

Unicode Char. Import as:

import qualified RIO.Char as C

This module does not export any partial functions. For those, see RIO.Char.Partial

  • 2 types
  • 28 values
  • Packagerio-0.1.22.0
  • Exports30
  • LanguageHaskell2010
  • LicenceMIT
  • SourceChar.hs
datadata Char
#

The character type Char represents Unicode codespace and its elements are code points as in definitions D9 and D10 of the Unicode Standard.

Character literals in Haskell are single-quoted: 'Q', 'Я' or 'Ω'. To represent a single quote itself use '\'', and to represent a backslash use '\\'. The full grammar can be found in the section 2.6 of the Haskell 2010 Language Report.

To specify a character by its code point one can use decimal, hexadecimal or octal notation: '\65', '\x41' and '\o101' are all alternative forms of 'A'. The largest code point is '\x10ffff'.

There is a special escape syntax for ASCII control characters:

Escape

Alternatives

Meaning

'\NUL'

'\0'

null character

'\SOH'

'\1'

start of heading

'\STX'

'\2'

start of text

'\ETX'

'\3'

end of text

'\EOT'

'\4'

end of transmission

'\ENQ'

'\5'

enquiry

'\ACK'

'\6'

acknowledge

'\BEL'

'\7'

,

'\a'

bell (alert)

'\BS'

'\8'

,

'\b'

backspace

'\HT'

'\9'

,

'\t'

horizontal tab

'\LF'

'\10'

,

'\n'

line feed (new line)

'\VT'

'\11'

,

'\v'

vertical tab

'\FF'

'\12'

,

'\f'

form feed

'\CR'

'\13'

,

'\r'

carriage return

'\SO'

'\14'

shift out

'\SI'

'\15'

shift in

'\DLE'

'\16'

data link escape

'\DC1'

'\17'

device control 1

'\DC2'

'\18'

device control 2

'\DC3'

'\19'

device control 3

'\DC4'

'\20'

device control 4

'\NAK'

'\21'

negative acknowledge

'\SYN'

'\22'

synchronous idle

'\ETB'

'\23'

end of transmission block

'\CAN'

'\24'

cancel

'\EM'

'\25'

end of medium

'\SUB'

'\26'

substitute

'\ESC'

'\27'

escape

'\FS'

'\28'

file separator

'\GS'

'\29'

group separator

'\RS'

'\30'

record separator

'\US'

'\31'

unit separator

'\SP'

'\32'

,

' '

space

'\DEL'

'\127'

delete

Data.Char provides utilities to work with Char.

Instances41Bounded, Enum, Data, Read, Ix, Storable, …

Character classification

16 declarations

Unicode characters are divided into letters, Data.Char.numbers, marks, punctuation, Data.Char.symbols, separators (including spaces) and others (including control characters).

valueisControl :: Char -> Bool
#

Selects control characters, which are the non-printing characters of the Latin-1 subset of Unicode.

valueisSpace :: Char -> Bool
#

Returns True for any Unicode space character, and the control characters \t, \n, \r, \f, \v.

valueisLower :: Char -> Bool
#

Selects lower-case alphabetic Unicode characters (letters).

Note: this predicate does not work for letter-like characters such as: 'ⓐ' (U+24D0 circled Latin small letter a) and 'ⅳ' (U+2173 small Roman numeral four). This is due to selecting only characters with the GeneralCategory LowercaseLetter.

See isLowerCase for a more intuitive predicate.

valueisUpper :: Char -> Bool
#

Selects upper-case or title-case alphabetic Unicode characters (letters). Title case is used by a small number of letter ligatures like the single-character form of Lj.

Note: this predicate does not work for letter-like characters such as: 'Ⓐ' (U+24B6 circled Latin capital letter A) and 'Ⅳ' (U+2163 Roman numeral four). This is due to selecting only characters with the GeneralCategory UppercaseLetter or TitlecaseLetter.

See isUpperCase for a more intuitive predicate. Note that unlike isUpperCase, isUpper does select title-case characters such as 'Dž' (U+01C5 Latin capital letter d with small letter z with caron) or 'ᾯ' (U+1FAF Greek capital letter omega with dasia and perispomeni and prosgegrammeni).

valueisAlpha :: Char -> Bool
#

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 isLetter.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Letter".

valueisAlphaNum :: Char -> Bool
#

Selects alphabetic or numeric Unicode characters.

Note that numeric digits outside the ASCII range, as well as numeric characters which aren't digits, are selected by this function but not by isDigit. Such characters may be part of identifiers but are not used by the printer and reader to represent numbers, e.g., Roman numerals like V, full-width digits like '1' (aka '65297').

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

valueisHexDigit :: Char -> Bool
#

Selects ASCII hexadecimal digits, i.e. '0'..'9', 'a'..'f', 'A'..'F'.

valueisLetter :: Char -> Bool
#

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 isAlpha.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Letter".

Examples

Basic usage:

Example7 expressions
isLetter 'a'TrueisLetter 'A'TrueisLetter 'λ'TrueisLetter '0'FalseisLetter '%'FalseisLetter '♥'FalseisLetter '\31'False

Ensure that isLetter and isAlpha are equivalent.

Example4 expressions
let chars = [(chr 0)..]let letters = map isLetter charslet alphas = map isAlpha charsletters == alphasTrue
valueisMark :: Char -> Bool
#

Selects Unicode mark characters, for example accents and the like, which combine with preceding characters.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Mark".

Examples

Basic usage:

Example2 expressions
isMark 'a'FalseisMark '0'False

Combining marks such as accent characters usually need to follow another character before they become printable:

Example1 expression
map isMark "ò"[False,True]

Puns are not necessarily supported:

Example1 expression
isMark '✓'False
valueisNumber :: Char -> Bool
#

Selects Unicode numeric characters, including digits from various scripts, Roman numerals, et cetera.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Number".

Examples

Basic usage:

Example3 expressions
isNumber 'a'FalseisNumber '%'FalseisNumber '3'True

ASCII '0' through '9' are all numbers:

Example1 expression
and $ map isNumber ['0'..'9']True

Unicode Roman numerals are "numbers" as well:

Example1 expression
isNumber 'Ⅸ'True
valueisPunctuation :: Char -> Bool
#

Selects Unicode punctuation characters, including various kinds of connectors, brackets and quotes.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Punctuation".

Examples

Basic usage:

Example6 expressions
isPunctuation 'a'FalseisPunctuation '7'FalseisPunctuation '♥'FalseisPunctuation '"'TrueisPunctuation '?'TrueisPunctuation '—'True
valueisSymbol :: Char -> Bool
#

Selects Unicode symbol characters, including mathematical and currency symbols.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Symbol".

Examples

Basic usage:

Example3 expressions
isSymbol 'a'FalseisSymbol '6'FalseisSymbol '='True

The definition of "math symbol" may be a little counter-intuitive depending on one's background:

Example2 expressions
isSymbol '+'TrueisSymbol '-'False
valueisSeparator :: Char -> Bool
#

Selects Unicode space and separator characters.

This function returns True if its argument has one of the following GeneralCategorys, or False otherwise:

These classes are defined in the Unicode Character Database, part of the Unicode standard. The same document defines what is and is not a "Separator".

Examples

Basic usage:

Example3 expressions
isSeparator 'a'FalseisSeparator '6'FalseisSeparator ' 'True

Warning: newlines and tab characters are not considered separators.

Example2 expressions
isSeparator '\n'FalseisSeparator '\t'False

But some more exotic characters are (like HTML's  ):

Example1 expression
isSeparator '\160'True

Subranges

valueisAscii :: Char -> Bool
#

Selects the first 128 characters of the Unicode character set, corresponding to the ASCII character set.

valueisLatin1 :: Char -> Bool
#

Selects the first 256 characters of the Unicode character set, corresponding to the ISO 8859-1 (Latin-1) character set.

Unicode general categories

datadata GeneralCategory
#

Unicode General Categories (column 2 of the UnicodeData table) in the order they are listed in the Unicode standard (the Unicode Character Database, in particular).

Examples

Basic usage:

Example1 expression
:t OtherLetterOtherLetter :: GeneralCategory

Eq instance:

Example2 expressions
UppercaseLetter == UppercaseLetterTrueUppercaseLetter == LowercaseLetterFalse

Ord instance:

Example1 expression
NonSpacingMark <= MathSymbolTrue

Enum instance:

Example1 expression
enumFromTo ModifierLetter SpacingCombiningMark[ModifierLetter,OtherLetter,NonSpacingMark,SpacingCombiningMark]

Text.Read.Read instance:

Example2 expressions
read "DashPunctuation" :: GeneralCategoryDashPunctuationread "17" :: GeneralCategory*** Exception: Prelude.read: no parse

Show instance:

Example1 expression
show EnclosingMark"EnclosingMark"

Bounded instance:

Example2 expressions
minBound :: GeneralCategoryUppercaseLettermaxBound :: GeneralCategoryNotAssigned

Ix instance:

Example3 expressions
import GHC.Internal.Data.Ix ( index )index (OtherLetter,Control) FinalQuote12index (OtherLetter,Control) Format*** Exception: Error in array index

Constructors

Instances9Bounded, Enum, Eq, Ord, Read, Show, …

The Unicode general category of the character. This relies on the Enum instance of GeneralCategory, which must remain in the same order as the categories are presented in the Unicode standard.

Examples

Basic usage:

Example7 expressions
generalCategory 'a'LowercaseLettergeneralCategory 'A'UppercaseLettergeneralCategory '0'DecimalNumbergeneralCategory '%'OtherPunctuationgeneralCategory '♥'OtherSymbolgeneralCategory '\31'ControlgeneralCategory ' 'Space

Case conversion

3 declarations
valuetoUpper :: Char -> Char
#

Convert a letter to the corresponding upper-case letter, if any. Any other character is returned unchanged.

valuetoLower :: Char -> Char
#

Convert a letter to the corresponding lower-case letter, if any. Any other character is returned unchanged.

valuetoTitle :: Char -> Char
#

Convert a letter to the corresponding title-case or upper-case letter, if any. (Title case differs from upper case only for a small number of ligature letters.) Any other character is returned unchanged.

Numeric representations

1 declaration

String representations

3 declarations
valueshowLitChar :: Char -> ShowS
#

Convert a character to a string using only printable characters, using Haskell source-language escape conventions. For example:

showLitChar '\n' s  =  "\\n" ++ s
valuelexLitChar :: ReadS String
#

Read a string representation of a character, using Haskell source-language escape conventions. For example:

lexLitChar  "\\nHello"  =  [("\\n", "Hello")]
valuereadLitChar :: ReadS Char
#

Read a string representation of a character, using Haskell source-language escape conventions, and convert it to the character that it encodes. For example:

readLitChar "\\nHello"  =  [('\n', "Hello")]