📘
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 map Function

The map function takes in a function and a list, and applies the given function to each element of that list. As we have seen how list comprehensions work in the previous chapter, we could define map as:

map :: (a -> b) -> [a] -> [b]
map f xs = [f x | x <- xs]

Note that the type variables a and b in the function definition could represent the same type, but this definition gives us flexibility so that a function passed in that takes in one type (a) can return another type (b), in which case we end up with a list of the type [b]. For example, we can pass our function squareGt100 to a list of numbers and end up with a list of booleans:

ghci> map squareGt100 [7..12]
[False, False, False, False, True, True]

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

ghci> map (* 2) [1..5] -- multiply each number in the list by 2
[2, 4, 6, 8, 10]

ghci> map not [True, False] -- not function reverses the boolean value
[False, True]

ghci> map reverse ["Cardano", "ADA"] -- reverse a given list (strings are lists of chars)
["onadraC","ADA"]

ghci> map ("Hi, " ++) ["Joe", "Jan"]
["Hi, Joe","Hi, Jan"]

PreviousIntroductionNextThe filter Function

Last updated 2 years ago

Was this helpful?