making the most of lambdas

27
Making the Most of Lambdas Presented by: Erik Meijer On: 11/14/2013 At: QCon SF 2013

Upload: robert-zych

Post on 29-Aug-2014

127 views

Category:

Technology


8 download

DESCRIPTION

 

TRANSCRIPT

  • 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 Theres 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. Its 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 theres 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 its 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 Its 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.