Lambda Expressions

In this lesson, we introduce lambda expressions and show how to use them in conjunction with higher-order functions.

In the previous lesson, we defined the higher-order function applyTwice and then used it on other functions like next or double.

Press + to interact
applyTwice :: (Int -> Int) -> Int -> Int
applyTwice f = f . f
double :: Int -> Int
double x = 2 * x
next :: Int -> Int
next x = x + 1
main = do
print (applyTwice double 8)
print (applyTwice next 7)

It can be annoying to define simple functions like double or next, especially if we just want to use them once in a higher-order function call.

If we just want to use a simple function temporarily, we do not need to give it a name. Instead, we can create an anonymous function using a lambda expression.

Introduction to lambda expressions

Lambda expressions are named after a mathematical system called Lambda calculus, which is the ...