liveBook · Manning

Map

As explained in Learn Haskell by Example

map is a standard Haskell function that applies a function independently to every element of a list and returns the resulting list.

`map` applies one function independently to every element of a list and returns a new list of the results, in the same order. Supply the element transformation first and the list second. It is useful whenever the same change must be made across every character, number, or other list element. section 2.5.3 section 2.6

Instead of writing explicit recursion for a routine transformation, define the operation for one element and let `map` apply it throughout the list. This keeps the operation focused: character rotation stays a character-level function, while `map` handles the whole `String`. section 2.5.3 section 4.3.3

Rotate every character in a message

A Caesar cipher rotates one `Char` at a time with `rotChar`. Mapping that function over a message produces the transformed `String`; `rot13` then specializes the offset to 13. `caesar n message = map (\ch -> rotChar n ch) message` This separation makes the per-character rule reusable while the list traversal remains concise. section 2.5.3

Keep input and transformation separate

A program can read a line with an IO action, then use pure code to transform every character before printing it: `putStrLn (map toUpper line)` Here `map toUpper` performs the uppercase conversion without side effects; input and output remain at the program boundary. section 3.2.1

Common patterns include:

  • Transform a `String` character by character, such as converting it to uppercase or rotating letters for a cipher. section 2.5.3 section 3.2.1
  • Compose transformations over a list, such as mapping `show` and then `length` before taking `maximum`. section 4.3.3
  • Choose `zipWith` rather than `map` when a transformation must combine corresponding elements from two lists. section 4.3.3
Listing 2.11 Functions that perform Caesar’s cipher and ROT13
caesar :: Int -> String -> String
caesar n message = map (\ch -> rotChar n ch) message    #1

rot13 :: String -> String
rot13 message = caesar 13 message    #2

Inspect a complete `map` use in which a character transformation becomes a reusable Caesar cipher over an entire message. section 2.5.3

How Learn Haskell by Example covers it

How deep it goes
Defines ordered element-wise list transformation and demonstrates composition, while deferring any implementation of `map` itself.
Examples
Uses Caesar and ROT13 ciphers, uppercase console input, and nested `show` and `length` transformations for line numbering.
What it assumes
Builds on functions and lists, using strings as character lists without assuming prior functional-programming knowledge.
What you can do after
Apply it via Haskell's `map` to transform every element of a list while keeping element logic separate.
How it connects
Connects `map` with pure functions, IO actions, parametric polymorphism, `maximum`, and `zipWith`.

Also explained in

Compare how 8 books explain map →

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.