SlideShare a Scribd company logo
Making the Most of Lambdas
Presented by: Erik Meijer
On: 11/14/2013
At: QCon SF 2013
Lambda Calculus
Defined by Alonzo Church
All FPLs can be constructed using this simple
grammar
Intro to Haskell
Type Declarations
f is a function that takes an Int and returns an
Int.
g is a higher-order function that takes an Int
and a Bool and returns a Char.
Parens are right associative and optional.
Included for readability.
Function Application
Parens are left associative during execution.
Map type declaration
map is a higher-order function that takes
function which maps a parameter of the
generic type a to a generic type b and
returns a function that given a list of as
returns a list of bs
Note: use “:t map” in GHCi to get the type
declaration of the map function.
Map implementation
Given a function and an empty list, map
returns an empty list
Given a function and a non-empty list, map
applies the function on the head elements
and cons the resulting value on the list
generated by the recursive call on the tail
What are Types?
A Type simply defines a set of possible values
There’s no way to illustrate a higher-order
type, which is why you need a type
declaration to reason about it
Tuples
fst, aka first, is a function that takes a tuple
and returns the first item in the tuple
snd, aka second, is a function that takes a
tuple and returns the second item in the
tuple
add is a function that takes the tuple xy and
adds the first item in the tuple with the
second item in the tuple
Boom!
foo is a function that takes an a and is
supposed to return an a.
foo can produce an error. This is depicted by
the bottom symbol (upside-down T)
Let the types guide you
This exercise began with just the type
definitions of curry and uncurry. The point
was to show that the implementations can
be easily derived by just looking at the type
declarations.
Uncurry function
Showing how the uncurry function works
with the addition operator
Curry/Twice
Showing that curry is the opposite of uncurry
and how it works with fst and snd
twice is a higher-order function that takes a
function and returns function that when
supplied an argument, applies the function
twice on that argument
Pattern Matching
The second definition of the head function
shows how pattern matching can be used to
extract the second element in the list
Zip
Zip is a function that takes a tuple consisting
of a list of items of type “a”, and a list of
items of type “b”, and produces a list of
tuples consisting of item of type “a” and an
item of type “b”.
Zip stops producing tuples when either given
list has been exhausted.
Zip (with land mines)
The hypothetical version of zip continues
until both lists are exhausted.
Produces tuples with undefined items (land
mines) and would never work with
algorithms that leverage infinite sequences.
The Maybe Monad
A Maybe can be something or nothing.
It makes the situation of an undefined value
explicit. Which forces you to acknowledge
the possibility of an undefined value.
It’s useful in situations where the value of an
expression is undefined (like zip with land
mines)
Functions vs. Classes
The difference between functions and classes
is that classes can have more that 1 method
associated with them.
The IO Monad
It makes the situation of interacting with the
outside world (i.e. accepting input and
printing output) explicit.

In other PLs, the type of f would hide the fact
that there’s a side effect.
But in Haskell, this side effect becomes
explicit with the use of the IO monad
Side effects
In Java, checked exceptions makes the fact
that the function can throw an exception
explicit.
getChar and Bind
The getChar function returns an IO Char
because its retrieves input from the world
The value a is defined by 3 successive calls to
getChar bound together using the bind (>>=)
function
Bind
In this example we used the type declaration
of bind to guide it’s implementation
The Monadic Interface
The Monad interface consists of 2 methods:
return and bind.
The return function is like a constructor
The bind function is used to take an instance
and a function to construct a new instance
Maybe Defined
Implementing the monadic interface (return
and bind) for the Maybe monad.
Unsafe Maybe
Take a Maybe of generic type “a” and returns
an instance of the generic type “a”
It’s unsafe because it can generate an error
when the supplied Maybe is Nothing.
Flatten
The flatten function takes a list of lists of “a”
and flattens it to product a single list “a”
Duplication
Both the sum and map functions are defined
with a base case (the empty list) and the
recursive case.
Keeping it DRY
The fold functions (i.e. foldr and foldl)
eliminate the need of explicitly defining the
base and recursive cases.

The foldr function starts from the right, and
applies the given function to each item in the
list.

More Related Content

PDF
Natural Transformations
PDF
Applicative Functor
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
PDF
Definitions of Functional Programming
PDF
Write a C function that appends one string to another. Function name: strinqA...
PPTX
Parametricity
PPTX
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
PDF
CallSharp: Automatic Input/Output Matching in .NET
Natural Transformations
Applicative Functor
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Definitions of Functional Programming
Write a C function that appends one string to another. Function name: strinqA...
Parametricity
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
CallSharp: Automatic Input/Output Matching in .NET

What's hot (20)

PPT
Frequently asked questions in c
PPT
Frequently asked questions in c
PPT
Primitive Wrappers
PPTX
Functions
PPT
Csc1100 lecture04 ch04
PPT
125 arc, irc, and derivative
PPTX
Csc1100 lecture02 ch02-datatype_declaration
PPT
Csc1100 lecture05 ch05
PPTX
PHP = PHunctional Programming
PPT
Cd2 [autosaved]
DOCX
Ecet 370 Education Organization -- snaptutorial.com
PPT
Hub102 - JS - Lesson3
PPTX
PDF
Swift 3 Programming for iOS : Collection
PPTX
Managing I/O & String function in C
PPTX
Computer programming 2 Lesson 10
PPTX
03. operators and-expressions
PDF
Functional Effects - Part 1
PPTX
Polymorphismupload
PDF
Day 3 of C++ Boot Camp - C++11 Language Deep Dive
Frequently asked questions in c
Frequently asked questions in c
Primitive Wrappers
Functions
Csc1100 lecture04 ch04
125 arc, irc, and derivative
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture05 ch05
PHP = PHunctional Programming
Cd2 [autosaved]
Ecet 370 Education Organization -- snaptutorial.com
Hub102 - JS - Lesson3
Swift 3 Programming for iOS : Collection
Managing I/O & String function in C
Computer programming 2 Lesson 10
03. operators and-expressions
Functional Effects - Part 1
Polymorphismupload
Day 3 of C++ Boot Camp - C++11 Language Deep Dive
Ad

Similar to Making the most of lambdas (20)

PPTX
Abdullah alotaibi functions
PDF
Scala categorytheory
PDF
Scala categorytheory
PDF
Functional programming using haskell notes iitk
PPTX
Writer Monad for logging execution of functions
PPTX
Amit user defined functions xi (2)
PPTX
Lecture 14. Lamda, filter, map, zip.pptx
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
PPT
Lecture 4
PDF
MTL Versus Free
PDF
The aggregate function - from sequential and parallel folds to parallel aggre...
PPTX
Function overloading
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
PPT
JavaScript - Programming Languages course
PPT
Phyton Learning extracts
PPTX
namma_kalvi_12th_computer_science_chapter_1_ppt_220184.pptx
PDF
[ITP - Lecture 12] Functions in C/C++
PPTX
Functional Programming Concepts for Imperative Programmers
PDF
Function in C++
Abdullah alotaibi functions
Scala categorytheory
Scala categorytheory
Functional programming using haskell notes iitk
Writer Monad for logging execution of functions
Amit user defined functions xi (2)
Lecture 14. Lamda, filter, map, zip.pptx
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Lecture 4
MTL Versus Free
The aggregate function - from sequential and parallel folds to parallel aggre...
Function overloading
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
JavaScript - Programming Languages course
Phyton Learning extracts
namma_kalvi_12th_computer_science_chapter_1_ppt_220184.pptx
[ITP - Lecture 12] Functions in C/C++
Functional Programming Concepts for Imperative Programmers
Function in C++
Ad

Recently uploaded (20)

PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Chapter 5: Probability Theory and Statistics
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mushroom cultivation and it's methods.pdf
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Hybrid model detection and classification of lung cancer
PDF
project resource management chapter-09.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
TLE Review Electricity (Electricity).pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 5: Probability Theory and Statistics
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Mushroom cultivation and it's methods.pdf
Hindi spoken digit analysis for native and non-native speakers
Encapsulation_ Review paper, used for researhc scholars
Hybrid model detection and classification of lung cancer
project resource management chapter-09.pdf
cloud_computing_Infrastucture_as_cloud_p
NewMind AI Weekly Chronicles - August'25-Week II
DP Operators-handbook-extract for the Mautical Institute
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TLE Review Electricity (Electricity).pptx

Making the most of lambdas

  • 1. Making the Most of Lambdas Presented by: Erik Meijer On: 11/14/2013 At: QCon SF 2013
  • 2. Lambda Calculus Defined by Alonzo Church All FPLs can be constructed using this simple grammar Intro to Haskell
  • 3. Type Declarations f is a function that takes an Int and returns an Int. g is a higher-order function that takes an Int and a Bool and returns a Char. Parens are right associative and optional. Included for readability.
  • 4. Function Application Parens are left associative during execution.
  • 5. Map type declaration map is a higher-order function that takes function which maps a parameter of the generic type a to a generic type b and returns a function that given a list of as returns a list of bs Note: use “:t map” in GHCi to get the type declaration of the map function.
  • 6. Map implementation Given a function and an empty list, map returns an empty list Given a function and a non-empty list, map applies the function on the head elements and cons the resulting value on the list generated by the recursive call on the tail
  • 7. What are Types? A Type simply defines a set of possible values There’s no way to illustrate a higher-order type, which is why you need a type declaration to reason about it
  • 8. Tuples fst, aka first, is a function that takes a tuple and returns the first item in the tuple snd, aka second, is a function that takes a tuple and returns the second item in the tuple add is a function that takes the tuple xy and adds the first item in the tuple with the second item in the tuple
  • 9. Boom! foo is a function that takes an a and is supposed to return an a. foo can produce an error. This is depicted by the bottom symbol (upside-down T)
  • 10. Let the types guide you This exercise began with just the type definitions of curry and uncurry. The point was to show that the implementations can be easily derived by just looking at the type declarations.
  • 11. Uncurry function Showing how the uncurry function works with the addition operator
  • 12. Curry/Twice Showing that curry is the opposite of uncurry and how it works with fst and snd twice is a higher-order function that takes a function and returns function that when supplied an argument, applies the function twice on that argument
  • 13. Pattern Matching The second definition of the head function shows how pattern matching can be used to extract the second element in the list
  • 14. Zip Zip is a function that takes a tuple consisting of a list of items of type “a”, and a list of items of type “b”, and produces a list of tuples consisting of item of type “a” and an item of type “b”. Zip stops producing tuples when either given list has been exhausted.
  • 15. Zip (with land mines) The hypothetical version of zip continues until both lists are exhausted. Produces tuples with undefined items (land mines) and would never work with algorithms that leverage infinite sequences.
  • 16. The Maybe Monad A Maybe can be something or nothing. It makes the situation of an undefined value explicit. Which forces you to acknowledge the possibility of an undefined value. It’s useful in situations where the value of an expression is undefined (like zip with land mines)
  • 17. Functions vs. Classes The difference between functions and classes is that classes can have more that 1 method associated with them.
  • 18. The IO Monad It makes the situation of interacting with the outside world (i.e. accepting input and printing output) explicit. In other PLs, the type of f would hide the fact that there’s a side effect. But in Haskell, this side effect becomes explicit with the use of the IO monad
  • 19. Side effects In Java, checked exceptions makes the fact that the function can throw an exception explicit.
  • 20. getChar and Bind The getChar function returns an IO Char because its retrieves input from the world The value a is defined by 3 successive calls to getChar bound together using the bind (>>=) function
  • 21. Bind In this example we used the type declaration of bind to guide it’s implementation
  • 22. The Monadic Interface The Monad interface consists of 2 methods: return and bind. The return function is like a constructor The bind function is used to take an instance and a function to construct a new instance
  • 23. Maybe Defined Implementing the monadic interface (return and bind) for the Maybe monad.
  • 24. Unsafe Maybe Take a Maybe of generic type “a” and returns an instance of the generic type “a” It’s unsafe because it can generate an error when the supplied Maybe is Nothing.
  • 25. Flatten The flatten function takes a list of lists of “a” and flattens it to product a single list “a”
  • 26. Duplication Both the sum and map functions are defined with a base case (the empty list) and the recursive case.
  • 27. Keeping it DRY The fold functions (i.e. foldr and foldl) eliminate the need of explicitly defining the base and recursive cases. The foldr function starts from the right, and applies the given function to each item in the list.