📘
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. Higher-order Functions

The filter Function

The filter function is another higher-order function for working with lists. As the name suggests, it is used for filtering lists by selecting only elements that satisfy a predicate defined by the function passed in. Like map, we could also define filter using a list comprehension:

filter :: (a -> Bool) -> [a] -> [a]
filter f xs = [x | x <- xs, f x]

Unlike map, the filter function must receive a function that returns a boolean as its argument, and the resulting list from filter will always be of the same type as the list we passed in because we are only selecting elements from that list, rather than generating new ones. We can filter a list using our squareGt100 function from before in order to get the elements for which squareGt100 returns True:

ghci> filter squareGt100 [7..12]
[11,12]

Here are some other examples of using filter with other pre-defined functions:

ghci> filter odd [1..5] -- get all odd numbers from a list
[1,3,5]

ghci> filter (\x -> length x > 2) ["a", "abc"] -- elements with length greater than 2
["abc"]

ghci> filter (\(x:xs) -> x == 'a') ["cardano", "ada"] -- elements staring with 'a'
["ada"]

PreviousThe map FunctionNextIntroduction

Last updated 2 years ago

Was this helpful?