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 `"->"`

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

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:ghci> (\x -> x * 3) 4

12

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:trackScore4 :: Float -> Float -> String

trackScore4 time avgTime

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

seconds below average!"

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

above average."

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

where

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

β

Copy link