A list comprehension constructs a list by evaluating an expression for values produced by generators and retained by guards. section 5.4.1
Use comprehensions to express common list operations directly:
- The left side builds each result; the right side supplies generators and optional guards. section 5.4.1
- A guard skips values that do not satisfy its condition, combining map and filter behavior. section 5.4.1
- Multiple generators evaluate the expression over their Cartesian product. section 5.4.1
- Pattern matching in a generator discards values whose patterns fail, as in [x | Just x <- xs]. section 5.4.1
Generating word-ladder modifications
For a word, generate modified candidates by combining every replacement letter with every word position: modified = [x : delete y word | x <- ['a' .. 'z'], y <- word, x /= y] The generators provide replacement letters and existing characters; the guard prevents reusing the removed character. section 5.4.1
ghci> [ (x, y) | x <- [1,2], y <- ['a', 'b', 'c']] :: [(Int, Char)] [(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b'),(2,'c')]
Inspect a two-generator comprehension to see its Cartesian-product output. section 5.4.1





