Lambda Expressions
In this lesson, we introduce lambda expressions and show how to use them in conjunction with higher-order functions.
We'll cover the following...
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 -> IntapplyTwice f = f . fdouble :: Int -> Intdouble x = 2 * xnext :: Int -> Intnext x = x + 1main = doprint (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 ...