HPM Education - Haskell

Searchβ¦

Introduction

Types in Haskell

Defining Functions / Working with Functions

List Comprehensions

Higher-order Functions

Cutom Types

Interactive Programming

Functors, Applicatives and Monads

Introduction

A **higher-order function** in Haskell is a function that either takes in a function as its argument or returns a function as its result. We have already seen how functions return other functions as their results when we introduced curried functions, so we will focus on **functions that take other functions as arguments** in this chapter. First, let's look at a simple example of a higher-order function that takes in a function and applies it twice to an argument:

1

applyTwice :: (a -> a) -> a -> a

2

applyTwice f x = f (f x)

3

β

4

ghci> applyTwice (++ "two ") "one "

5

"one two two "

Copied!

The function we passed in

`(++ "two ")`

simply appends the string `"two "`

to the argument passed in (in this case, it must be a string), and it is applied twice when the higher-order function `applyTwice`

is called.Now let's take a closer look at two higher-order functions that are defined in the prelude for working with lists,

`map`

and `filter`

.Copy link