Lesson 32. The list monad and list comprehensions

After reading lesson 32, you’ll be able to

  • Use do-notation to generate lists
  • Filter results in do-notation by using guard
  • Further simplify do-notation with list comprehensions

At the end of the preceding lesson, you saw that List is an instance of Monad. You saw only a simple example of using List as a Monad to process a list of candidates.

Listing 32.1. The assessCandidateList function from the previous lesson
assessCandidateList :: [Candidate] -> [String]
assessCandidateList candidates = do
   candidate <- candidates                        1
   let passed = viable candidate                  2
   let statement = if passed                      3
                   then "passed"
                   else "failed"
   return statement                               4
  • 1 By using <- , you’re able to treat your list of candidates like ...

Get Get Programming with Haskell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.