🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell Characters

A character is a single letter, digit, or symbol. Haskell calls the type of one character Char.

succ gives the next character

succ moves one step forward in the character order. The character after m is n.

main :: IO ()
main = print (succ 'm')

fromEnum gives the code number

Every character has a code number. The code number of A is 65.

main :: IO ()
main = print (fromEnum 'A')

toEnum gives the character for a code

toEnum turns a code number back into a character. This program checks the character for 97 against a.

main :: IO ()
main = print (toEnum 97 == 'a')

Example

main :: IO ()
main = print (fromEnum (succ 'A'))

Key points

  • Char: Char is the type of a single character.
  • Single quotes: a Char value is written between single quotes.
  • succ: succ gives the next character in the character order.
  • pred: pred gives the previous character in the character order.
  • fromEnum: fromEnum gives the code number of a character.
  • toEnum: toEnum gives the character for a code number when a character is expected.
  • Comparing: characters are compared in code number order.

In this example, succ gives the character after A, which is B, and fromEnum then gives the code number of B, which is 66.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

What does print (pred 'd') print?