📘
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq – Equality Types
      • Ord – ordered types
      • Show – Showable Types
      • Read – readable types
      • Num – Numeric Types
      • Integral – Integral Types
      • Fractional – Fractional Types
      • Enum – Enumeration Types
    • Derived Instances
    • Exercise – Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. List Comprehensions

List Comprehensions

In this chapter, we introduce list comprehensions, which are used to create new lists from existing ones. The term comes from mathematics, where set comprehension is used to describe a set by enumerating its elements and/or stating conditions its members must satisfy. In Haskell, the syntax is the following:

[ <GENERATOR> | <ELEMENT> <- <LIST>, <GUARD> ]

The GENERATOR is an expression that specifies how the elements of the new lists should be calculated, the ELEMENT is an element from the specified existing LIST, and the GUARD is an optional condition we can set that an ELEMENT must satisfy to end up in the new list we are creating via the GENERATOR expression. The whole line can be read as "create a list by applying the GENERATOR for each ELEMENT of the LIST that meets the criteria set by the GUARD". Let's take a look at an example list comprehension that creates a list of only even numbers:

ghci> [x | x <- [1..10], even x]
[2,4,6,8,10]

The above can be read as "create a list of all numbers x such that x is an element of the list [1..10] and x is an even number". This simply puts x into a new list if it meets the guard criteria, but we could also apply function(s) to x:

ghci> [x * 2 | x <- [1..10], even x]
[4,8,12,16,20]

We do not even have to use x for our generator, while we can still use it for the guard expression:

ghci> ["even!" | x <- [1..10], even x]
["even!","even!","even!","even!","even!"]

We can also specify multiple lists as well as multiple guards in list comprehensions. In the case of multiple lists, the deeper nested list (the one specified last) is iterated through for each element of the previous list(s):

ghci> [ (x, y) | x <- [1..3], y <- ['a'..'c'] ]
[
    (1,'a'),(1,'b'),(1,'c'),
    (2,'a'),(2,'b'),(2,'c'),
    (3,'a'),(3,'b'),(3,'c')
]

In this case, the list ['a'..'b'] is iterated through three times, once for each element in the first list [1..3].

PreviousFunction OperatorsNextIntroduction

Last updated 2 years ago

Was this helpful?