Upcoming SlideShare
×

# Making the most of lambdas

423 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Making the most of lambdas

1. 1. Making the Most of Lambdas Presented by: Erik Meijer On: 11/14/2013 At: QCon SF 2013
2. 2. Lambda Calculus Defined by Alonzo Church All FPLs can be constructed using this simple grammar Intro to Haskell
3. 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. 4. Function Application Parens are left associative during execution.
5. 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. 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. 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. 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. 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. 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. 11. Uncurry function Showing how the uncurry function works with the addition operator
12. 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. 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. 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. 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. 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. 17. Functions vs. Classes The difference between functions and classes is that classes can have more that 1 method associated with them.
18. 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. 19. Side effects In Java, checked exceptions makes the fact that the function can throw an exception explicit.
20. 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. 21. Bind In this example we used the type declaration of bind to guide it’s implementation
22. 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. 23. Maybe Defined Implementing the monadic interface (return and bind) for the Maybe monad.
24. 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. 25. Flatten The flatten function takes a list of lists of “a” and flattens it to product a single list “a”
26. 26. Duplication Both the sum and map functions are defined with a base case (the empty list) and the recursive case.
27. 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.