liveBook · Manning

String

As explained in Learn Haskell by Example

A string is an ordered sequence of characters represented as a value in a programming language.

A String represents a message as characters. In Haskell, it is a type synonym for a list of Char values: type String = [Char]. Quoted text is convenient syntax for that character list, so "Hello World" and ['H','e','l','l','o',' ','W','o','r','l','d'] are identical values. section 2.3.1

Write text as a String

Use quotes for readable text while retaining list behavior. A String can be printed as text, but its characters can also be handled individually when an algorithm needs to inspect or transform them. section 2.3.1

Because String is a list, familiar list operations apply directly:

  • Append strings with (++), such as "Hello" ++ " " ++ "World!". section 2.3.3
  • Use type synonyms to document intent: Alphabet can also mean [Char], even though it has the same underlying type as String. section 2.3.1
  • Create character ranges for alphabets and digits, such as ['a' .. 'z'] or ['0' .. '9']. section 2.3.1

Strings support character-by-character transformations. For a Caesar cipher, transform the first character, recursively transform the remaining characters, and construct a new String. Immutability means the original value is not changed; the result is a newly built list. section 2.4 section 2.5

Rotate every character

The function caesar :: Int -> String -> String handles [] as empty and x : xs by placing rotChar n x before the recursively transformed tail. Rotating "Caesars Cipher!" by 13 produces "Pnrfnef Pvcure!". section 2.5

String is useful for simple text processing, but it can become a performance cost for large inputs: it is a heap-allocated linked list of Char values. When profiling shows string handling matters, Text and ByteString offer packed alternatives; choose algorithms and data structures before changing types. section 6.3.2

string1 :: String
string1 = "Hello World"

string2 :: String
string2 = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

Compare quoted String syntax with its explicit list-of-Char form. section 2.3.1

How Learn Haskell by Example covers it

How deep it goes
Defines String as [Char], demonstrates literals and list operations, then implements recursive character-by-character transformation.
Examples
Uses "Hello World", Latin alphabet ranges, concatenation, and a ROT13 Caesar cipher.
What it assumes
Assumes no prior functional-language experience while introducing Haskell syntax, common types, and immutable transformations.
What you can do after
Build a Caesar cipher that creates a transformed String from each input character.
How it connects
Connects String to Char, lists, type synonyms, immutability, recursion, Text, and ByteString.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.