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

Lambda functions

`"\"`

(similar to the Greek letter lambda β Ξ»), and instead of the equality symbol, we use the function arrow `"->"`

:1

\<ARGUMENT1> <ARGUMENT2> -> <FUNCTION BODY>

Copied!

We can directly use lambda functions just like any other function, so here is how we would use our

`triple`

function as a lambda function:1

ghci> (\x -> x * 3) 4

2

12

Copied!

Lambda functions are very useful for functions that are only used locally because we can simplify our code. For example, our previously defined function

`trackScore`

could be improved by using a lambda function to calculate the score:1

trackScore4 :: Float -> Float -> String

2

trackScore4 time avgTime

3

| time < avgTime = "Great! Your time is " ++ show (score) ++ "

4

seconds below average!"

5

| time > avgTime = "Your time is " ++ show (score) ++ " seconds

6

above average."

7

| otherwise = "Your time is on par with the average time!"

8

where

9

score = (\x y -> abs (x - y)) time avgTime

Copied!

β

Copy link