Appendix B. Characters, Strings, and Escaping Rules
This appendix covers the escaping rules used to represent non-ASCII characters in Haskell character and string literals. Haskell’s escaping rules follow the pattern established by the C programming language, but they expand considerably upon them.
Writing Character and String Literals
A single character is surrounded by ASCII single quotes, '
, and
has type Char:
ghci>
'c'
'c'ghci>
:type 'c'
'c' :: Char
A string literal is surrounded by
double quotes, "
, and
has type [Char] (more often written as
String):
ghci>
"a string literal"
"a string literal"ghci>
:type "a string literal"
"a string literal" :: [Char]
The double-quoted form of a string literal is just syntactic sugar for list notation:
ghci>
['a', ' ', 's', 't', 'r', 'i', 'n', 'g'] == "a string"
True
International Language Support
Haskell uses Unicode internally for its Char data type. Since String is just an alias for [Char] (which is a list of Chars), Unicode is also used to represent strings.
Different Haskell implementations place limitations on the character sets they can accept in source files. GHC allows source files to be written in the UTF-8 encoding of Unicode, so in a source file, you can use UTF-8 literals inside a character or string constant. Do be aware that if you use UTF-8, other Haskell implementations may not be able to parse your source files.
When you run the ghci interpreter interactively, it may not be able to deal with international characters in character or ...
Get Real World Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.