SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in cDavid Livingston J
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in cDavid Livingston J
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive WrappersBharat17485
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04IIUM
 
125 arc, irc, and derivative
125  arc, irc, and derivative125  arc, irc, and derivative
125 arc, irc, and derivativeJeneva Clark
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationIIUM
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05IIUM
 
PHP = PHunctional Programming
PHP = PHunctional ProgrammingPHP = PHunctional Programming
PHP = PHunctional ProgrammingLuis Atencio
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.comDavisMurphyB81
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Tiểu Hổ
 
Swift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionSwift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionKwang Woo NAM
 
Managing I/O & String function in C
Managing I/O & String function in CManaging I/O & String function in C
Managing I/O & String function in CAbinaya B
 
03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressionsStoian Kirov
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1Philip Schwarz
 
Day 3 of C++ Boot Camp - C++11 Language Deep Dive
Day 3 of C++ Boot Camp - C++11 Language Deep DiveDay 3 of C++ Boot Camp - C++11 Language Deep Dive
Day 3 of C++ Boot Camp - C++11 Language Deep DiveJim McKeeth
 

Tendances (20)

Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Primitive Wrappers
Primitive WrappersPrimitive Wrappers
Primitive Wrappers
 
Functions
FunctionsFunctions
Functions
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
125 arc, irc, and derivative
125  arc, irc, and derivative125  arc, irc, and derivative
125 arc, irc, and derivative
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declaration
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 
PHP = PHunctional Programming
PHP = PHunctional ProgrammingPHP = PHunctional Programming
PHP = PHunctional Programming
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
C string
C stringC string
C string
 
Swift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : CollectionSwift 3 Programming for iOS : Collection
Swift 3 Programming for iOS : Collection
 
Managing I/O & String function in C
Managing I/O & String function in CManaging I/O & String function in C
Managing I/O & String function in C
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressions
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
 
Day 3 of C++ Boot Camp - C++11 Language Deep Dive
Day 3 of C++ Boot Camp - C++11 Language Deep DiveDay 3 of C++ Boot Camp - C++11 Language Deep Dive
Day 3 of C++ Boot Camp - C++11 Language Deep Dive
 

Similaire à Making the most of lambdas

Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitkbenevolent001
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functionsPhilip Schwarz
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...Philip Schwarz
 
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 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts Pavan Babu .G
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersChris
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfRITHIKA R S
 

Similaire à Making the most of lambdas (20)

Abdullah alotaibi functions
Abdullah alotaibi functionsAbdullah alotaibi functions
Abdullah alotaibi functions
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitk
 
Writer Monad for logging execution of functions
Writer Monad for logging execution of functionsWriter Monad for logging execution of functions
Writer Monad for logging execution of functions
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
MTL Versus Free
MTL Versus FreeMTL Versus Free
MTL Versus Free
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Function overloading
Function overloadingFunction overloading
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 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
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 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
 
Function in C++
Function in C++Function in C++
Function in C++
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 

Dernier

A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.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.