Making the Most of Lambdas
Presented by: Erik Meijer
At: QCon SF 2013
Defined by Alonzo Church
All FPLs can be constructed using this simple
Intro to Haskell
f is a function that takes an Int and returns an
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.
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.
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
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
add is a function that takes the tuple xy and
adds the first item in the tuple with the
second item in the tuple
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
Showing how the uncurry function works
with the addition operator
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
The second definition of the head function
shows how pattern matching can be used to
extract the second element in the list
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
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
In Java, checked exceptions makes the fact
that the function can throw an exception
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 (>>=)
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
Implementing the monadic interface (return
and bind) for the Maybe monad.
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.
The flatten function takes a list of lists of “a”
and flattens it to product a single list “a”
Both the sum and map functions are defined
with a base case (the empty list) and the
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