Intro to Functional Programming Workshop (code4lib)


Published on

Published in: Technology
1 Comment
  • slide 54 has an error:
    A passing test should result in the built(first(xs) ...)
    also the next answer (55) is wrong because of this (just flip the == to !=)
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Treats programming as the evaluation of mathematical processes Based on what's call the lambda calculus.  Mention this is turing complete way of representing a programing language with only anonymous function
  • But what does this mean? Every function returns a value, that's what functions do they map one value to another. given an input a function always returns the same value in general it's more about stating what your problem is.
  • The big this this all entails is there are no 'Side Effects' in functional programming. When you call a function nothing about the world changes, can you say this is true about OOP?  This is much more meaningful that it may seem at first. If a variable is declared (and the usually aren't) it can never change it's value. This is even true for something like 'i' in a 'for loop' or the value a while loop checks... so that mean there are not classic iterators: no for loops, and no while loops.
  • So why do all this? Number one reason by far for me is the really amazing abstractions you can come up with. For programming side effect free is an awesome guarantee There a lot of potential research that we can get some great and easy parallization from all this There are already functional features in languages like C#, python, ruby, LINQ. If you understand FP you can pick these things up a lot faster Big ideas are starting to come from the fp community.  When google release mapreduce, anyone with a background in lisp for functional programming would have instantly be able to guess roughly how the process worked.
  • So I'm going to talk about 2 languages tonight side by side. Haskell is an amazing language, but it has a reputation for being very difficult to learn.  As much as I would like to pretend that this is not the case, it really can be.  Tonight I'm sticking to just a small subset of haskells expressiveness to go over the essentials of functional programming.  Get started with haskell isn't really so bad once you know the basics and understand fp a little better.
  • Now JavaScript has the opposite problem. Most people view it as simply a mediocre clientside language. But it's actually an amazingly powerful language, and also probably the most functional of all mainstream languages. I want to show you that functional programming isn't really that hard, it's just different.  I'm willing to bet that most anyone in the room has already used some really powerful functional features of javascript without even knowing it.
  • Before we begin I want to just mentions lists a bit.  Functional programmin, with no standard loops, is very heavily based on recursion, and so it also relies heavily on lists. Here are some basic operations on a list that you'll need to be familiar with.  They go by many other names.
  • The first essential feature of functional programming, is what is refered to as a 'first class function' This is one of the easiest concepts to grasp, it seemly means that functions can be treated just like any other object, and most importantly that they can be passed just like arguments.
  • so first we're going to put together a simple function this one adds '2' to a  number and this one addes '3' this are both in haskell and should be very simple to understand.  any questions
  • Now we're going to make the opposite function, that is a function that takes a function and applies it to a predetermined argument. So arg is '2' takes whatever function you pass it and gives it the argument '2' <walk them through the output>
  • How many of you have ever written any javascript at all? Everyone that just raised there hands has used a lambda function. All a lambda function is is an unnamed or anonymous function.  I'll show you in a bit how common these are in javascript Lambda functions are the core of funtional programming, although the can be all but removed from some languages. Initially what they offer may seem of limited use, but as we go on you should see how the become pretty essential. If you're insane you can literarly usual solely lambda functions to do all of your computation, you can even implment addition and subtraction without numbers.  Hell you can even implment recursion in using only unnamed functions.
  • Now these examples are stupidly simple, but you'll see lambda popping up all over this presentaiton in various forms. so here we're defining a function, add4, as a lambda <walk through notation? Now also notice how we can pass this function as a first class function directly to our previous argIsX function
  • Here some everyday javascript, these snippets are just pulled straight from standard javascript tutorials.  You've probably written code like this if you've ever done any javascript.
  • Closures sound more complicated then they are.  Closures let us capture some variable in the scope of a function we return... Examples will do you much better than me describing it.
  • So here's add5 for reference. Now this pattern of addX is pretty obvious, there must be a way to abstract this away right? so we can make this functioncalled makeAdder that takes a values. here is the same code 2 different ways, in put cases we're returning a function that has this value we based enclosed in it. Now we can make adders more easily. So you can see in the about put the function we returned remembers what value we passed to it. This also demonstrates another important property of lambda functions.
  • okay so proper first class functions, lambdas and closure are the core of functional programming.  coupled with recursion you're pretty much done. However these are really just the primative of functional programming.  Next we're going to answer what's probably the biggest question you have.  Since we throw out all the iterators I know of, how the hell do I get any actual work done? That brings us to higher order functions, by definiton these are just functions that take functions as an argument, but there are some that form the basis of how you should think about problems in fp.
  • Map is the most obvious, it solve the problem of the vast majority of iteration that you do.   map takes a function and a list and returns a new list with that function applied to every item in it. From here on out I'm going to show you how higher order functions work in haskell, and how there implemented in javascript
  • this is actually a pretty easy concept. so in the first example double all takes a list, and the applys times 2 to each item, so it doubles every item in the list. square all does the same thing but squares them square and add just shows off how we can use our lambda again and upperCase takes a string an applies toUpper to each letter in that string, a string is afterall just a list of characters.
  • just a note on haskell here, you can actually leave out the arguments on both sides if we're missing an arguement we essentially create a closure waiting for that argument. We'll talk more about this later.
  • filter does what the name sounds like, it takes a function which performs a test and only returns values that pass.
  • so this first case selects every item that is even the next selects every one that is evenly divisible by 17  And this last is a little different.  This is a neat trick called function composition.  what we do is apply the rightmost function and then send it's value to the left most.  this makes it very easy to define complicated functions by composing them. explain output
  • Folds are very poweful, but also the most likely to give you a headache until you undertand them.  There are actually several types of folds, we're going to focus on what's call a left fold, foldl or often reduce (in ruby it's called inject). fold takes a function, and initial value, and a list of values and 'folds' all the values of that list into a single value.
  • The simpliest and most obious use of fold it to some values ina list, see here our function is 'add' and our intial value is 0. This reverse is actually a bit more tricky, I'll actually walk you through this. finally I'll show you some more function composition. we can define sumOfSquare 2 ways, the first more verbose maps square to a list, and the folds it just like sum, The second composes our sum and squareAll functions.
  • here I'll walk you through the recursion for the fold revers.
  • Many of you may know of Joel Spolsky, he's one of the people behind stack over flow A few years ago he wrote about the functional properties of javascript and gave this as his implementation of map. But we can see here that this implementation itself is not functional.  In some cases this is meaningless, but notice what happens to the array we're passing?   In a purely functional work the original array will ALWAYS be untouched, in this case the original array is deleted.
  • Once again Spolsky also implemented this, but once again it's not functional. However this time it is less damaging and probably more effiience.  Some languages let you choose when you want to be functional, choose wisely.
  • Finally we'll discuss function currying. what happens in any language you know when you call a function that takes 3 arguements with only 2 arguement? Error right?  Well not in haskell, in haskell you get a new function waiting for the 3rd arguement. let's look at some examples
  • here I'll defiine a new version of add that takes 2 arguments If I call it with only one then I get a function waiting for the next argument, you can see this is even easier than our earlier example that build closures
  • what's crazy is that you can actually implement this functionality in javascript
  • Intro to Functional Programming Workshop (code4lib)

    1. 1. var λ; Introduction to Functional Programming  with JavaScript (and a little Haskell) Will Kurt Will Kurt, Creative Commons Attribution-ShareAlike 3.0
    2. 2. "A language that doesn't effect how you think about programming is not worth learning" --Alan Perlis
    3. 3. So what is Functional Programming?
    4. 4. What does this mean? <ul><li> </li></ul>
    5. 5. No Side Effects! <ul><li> </li></ul>
    6. 6.
    7. 7. Haskell!
    8. 8. JavaScript!!!
    9. 9. Rules for Today λ
    10. 10. Lists!!! <ul><li>first( [1,2,3,4]) -> 1  (aka: car, head) </li></ul><ul><li>rest( [1,2,3,4] ) -> [2,3,4] (aka: cdr, tail) </li></ul><ul><li>empty( [1,2,3,4] ) -> false (aka: null?, nil? empty?,[]) </li></ul><ul><li>empty( [ ] ) -> true </li></ul><ul><li>build( 1 , [2,3,4]) -> [1,2,3,4]  (aka: cons, ':' ) </li></ul><ul><li>build( [1] , [2,3,4]) -> [[1],2,3,4] </li></ul>
    11. 11. Recursion! &quot;If you already know what recursion is, just remember the answer. Otherwise, find someone who is standing closer to Douglas Hofstadter than you are; then ask him or her what recursion is.&quot; -- Andrew Plotkin
    12. 12. Exercise set 1a 10 minutes
    13. 13. Answers to set 1a
    14. 14. problem 1 <ul><li>var fact = function(x) { </li></ul><ul><li>     return( </li></ul><ul><li>       (x===1) ? 1 : </li></ul><ul><li>       x*fact(x-1)    </li></ul><ul><li>     ); </li></ul><ul><li>} </li></ul><ul><li>function fact (x) { </li></ul><ul><li>     if(x==1){ </li></ul><ul><li>             return 1; </li></ul><ul><li>       } else { </li></ul><ul><li>         return x*fact2(x-1); </li></ul><ul><li>       } </li></ul><ul><li>} </li></ul>
    15. 15. problem 2 <ul><li>var fib = function(x){ </li></ul><ul><li>     return( </li></ul><ul><li>           (x==0) ? 0 : </li></ul><ul><li>           (x==1) ? 1 : </li></ul><ul><li>           fib(x-1)+fib(x-2) </li></ul><ul><li>     ); </li></ul><ul><li>} </li></ul>
    16. 16. problem 2 continued... <ul><li>var fib_answers = [] </li></ul><ul><li>fib_answers[0] = 0; </li></ul><ul><li>fib_answers[1] = 1; </li></ul><ul><li>var fibm = function (x) { </li></ul><ul><li>     return fib_mem(x,fib_answer); </li></ul><ul><li>} </li></ul><ul><li>var fib_mem = function(x,m){ </li></ul><ul><li>     if(m[x] != undefined){ </li></ul><ul><li>         return m[x]; </li></ul><ul><li>     } else { </li></ul><ul><li>         m[x] = fib_mem(x-1,m)+fib_mem(x-2,m); </li></ul><ul><li>          return m[x]; </li></ul><ul><li>     } </li></ul><ul><li>} </li></ul>
    17. 17. problem 2 last one I swear (also very functional)... <ul><li>var fib3wrapper = function(c){ </li></ul><ul><li>     return fib3(c,1,0); </li></ul><ul><li>} </li></ul><ul><li>var fib3 = function(c,last1,last2){ </li></ul><ul><li>     return( </li></ul><ul><li>             (c==2) ? last1+last2: </li></ul><ul><li>             fib3(c-1,last1+last2,last1) </li></ul><ul><li>     ); </li></ul><ul><li>} </li></ul>
    18. 18. Exercise set 1b 10 minutes
    19. 19. Answers to set 1b
    20. 20. problem 1 <ul><li>var length = function (xs) { </li></ul><ul><li>     return( </li></ul><ul><li>           empty (xs) ? 0 : </li></ul><ul><li>           1 + length( rest (xs)) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    21. 21. problem 2 <ul><li>var nth = function(n,xs){ </li></ul><ul><li>     return( </li></ul><ul><li>         empty(xs) ? [] : </li></ul><ul><li>         (n == 0) ? first(xs) : </li></ul><ul><li>         nth(n-1, rest(xs)) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    22. 22. problem 3 <ul><li>var removeAll = function (x, xs) { </li></ul><ul><li>     return( </li></ul><ul><li>         empty(xs) ?  [ ]  : </li></ul><ul><li>         (x == first(xs)) ?  removeAll(x,    </li></ul><ul><li>                                 rest(xs)) : </li></ul><ul><li>         build( first(xs), </li></ul><ul><li>               removeAll(x,rest(xs))) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    23. 23. First class functions
    24. 24. First Class Functions (Haskell) <ul><li>add2 x = 2 + x </li></ul><ul><li>add3 x = 3 + x </li></ul><ul><li>> add2 5 </li></ul><ul><li>7 </li></ul><ul><li>> add3 5 </li></ul><ul><li>8 </li></ul>
    25. 25. First Class Functions (Haskell) <ul><li>argIs2 func = (func) 2 </li></ul><ul><li>argIs3 func = (func) 3 </li></ul><ul><li>> argIs2(add2) </li></ul><ul><li>4 </li></ul><ul><li>> argIs2(add3) </li></ul><ul><li>5 </li></ul><ul><li>> argIs3(add2) </li></ul><ul><li>5 </li></ul><ul><li>> argIs3(add3) </li></ul><ul><li>6 </li></ul>
    26. 26. Lambda Functions <ul><li> </li></ul>
    27. 27. Lambda Function (Haskell) <ul><li>add4 = (x -> 4+x) </li></ul><ul><li>>argIs2( (x -> 4+x) ) </li></ul><ul><li>6 </li></ul>
    28. 28. Lambda Function (JavaScript) <ul><li>$.ajax({ url: &quot;test.html&quot;, context: document.body, success: function(){ </li></ul><ul><li>        $(this).addClass(&quot;done&quot;); </li></ul><ul><li>      } }); </li></ul><ul><li>$(&quot;#exec&quot;).click( function(){ </li></ul><ul><li>  $(&quot;#results&quot;).prepend(&quot;<li>Normal Handler</li>&quot;); </li></ul><ul><li>} ); </li></ul>
    29. 29. Exercise set 2 10 minutes
    30. 30. Answers to set 2
    31. 31. problem 1 <ul><li>var argIs2 = function(func) { </li></ul><ul><li>     return func(2); </li></ul><ul><li>} </li></ul>
    32. 32. problem 2 <ul><li>argIs2(function(x){ add(2,x);} ); </li></ul>
    33. 33. problem 3 <ul><li>var flip = function (func) { </li></ul><ul><li>       return( </li></ul><ul><li>                 function(x,y){ </li></ul><ul><li>                         func(y,x); </li></ul><ul><li>                 } </li></ul><ul><li>       ); </li></ul><ul><li>}; </li></ul>
    34. 34. Break! 5 minutes
    35. 35. Closures <ul><li> </li></ul>
    36. 36. Closures (Haskell) <ul><li>add5 = (+5) </li></ul><ul><li>makeAdder val = (+val) </li></ul><ul><li>makeAdderWithLambda val = (x->x+val) </li></ul><ul><li>add6 = makeAdder 6 </li></ul><ul><li>add7 = makeAdderWithLambda 7 </li></ul><ul><li>> add5 5 </li></ul><ul><li>10 </li></ul><ul><li>> add6 5 </li></ul><ul><li>11 </li></ul><ul><li>> add7 5 </li></ul><ul><li>12 </li></ul>
    37. 37. Exercise set 3 5 minutes
    38. 38. Answers to set 3
    39. 39. problem 1 <ul><li>var makeAdder = function (x) { </li></ul><ul><li>     return( </li></ul><ul><li>         function(y) { </li></ul><ul><li>                   return add(x,y); </li></ul><ul><li>               } </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    40. 40. problem 2 <ul><li>var apiClosure = function(apiKey){ </li></ul><ul><li>     return( </li></ul><ul><li>         function(x){ </li></ul><ul><li>             return apiSearch(apiKey,x); </li></ul><ul><li>         } </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    41. 41. problem 3 <ul><li>var compose = function (f1,f2) { </li></ul><ul><li>     return( </li></ul><ul><li>         function(x){ </li></ul><ul><li>             f1(f2(x)); </li></ul><ul><li>         } </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    42. 42. Higher Order Functions <ul><li> </li></ul>
    43. 43. Map <ul><li> </li></ul>
    44. 44. Map (Haskell) <ul><li>doubleAll xs = map (*2) xs </li></ul><ul><li>squareAll xs = map (^2) xs </li></ul><ul><li>squareAndAdd xs = map (x->x*x+x) xs </li></ul><ul><li>upperCase s = map toUpper s </li></ul><ul><li>> doubleAll [1,2,3,4] </li></ul><ul><li>[2,4,6,8] </li></ul><ul><li>> squareAll [1,2,3,4] </li></ul><ul><li>[1,4,9,16] </li></ul><ul><li>> squareAndAdd [1,2,3,4] </li></ul><ul><li>[2,6,12,20] </li></ul><ul><li>> upperCase &quot;doggie&quot; </li></ul><ul><li>&quot;DOGGIE&quot; </li></ul>
    45. 45. Map (Haskell) <ul><li>doubleAllv2 = map (*2) </li></ul><ul><li>squareAllv2 = map (^2) </li></ul><ul><li>squareAndAddv2 = map (x->x*x+x) </li></ul>
    46. 46. Exercise set 4a 5 minutes
    47. 47. Answers to set 4a
    48. 48. problem 1 <ul><li>var map = function (func, xs) { </li></ul><ul><li>     return( </li></ul><ul><li>         empty(xs) ?  [ ]  : </li></ul><ul><li>         build(  func (first(xs)), </li></ul><ul><li>                   map  (func,  rest (xs)))); </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    49. 49. problem 2 <ul><li>map(function(x){ </li></ul><ul><li>                 return x*x}, </li></ul><ul><li>         xs); </li></ul>
    50. 50. Filter
    51. 51. Filter (Haskell) <ul><li>evenList xs = filter even xs </li></ul><ul><li>mod17list xs = filter (== (`mod` 17) 0) xs </li></ul><ul><li>doubleEvens xs = (doubleAll . evenList) xs </li></ul><ul><li>> evenList [1,2,3,4,5,6] </li></ul><ul><li>[2,4,6] </li></ul><ul><li>> mod17list [1..100] </li></ul><ul><li>[17,34,51,68,85] </li></ul><ul><li>> doubleEvens [0,3..27] </li></ul><ul><li>[0,12,24,36,48] </li></ul>
    52. 52. Exercise set 4b 10 minutes
    53. 53. Answers to set 4b
    54. 54. problem 1 <ul><li>var filter = function (test,xs){ </li></ul><ul><li>     return( </li></ul><ul><li>         empty(xs) ? [ ] : </li></ul><ul><li>         test(first(xs)) ? filter(test, rest(xs)) : </li></ul><ul><li>         build(first(xs),filter(test,rest(xs))) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    55. 55. problem 2 <ul><li>var removeAll = function(x,xs){ </li></ul><ul><li>     return( </li></ul><ul><li>         filter( function(y){ </li></ul><ul><li>                           return y == x}, </li></ul><ul><li>                 xs ) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    56. 56. Foldl (Reduce)
    57. 57. Foldl (Haskell) <ul><li>mySum xs = foldl (+) 0 xs </li></ul><ul><li>sumOfSquares xs = foldl (+) 0 (map (^2) xs) </li></ul><ul><li>sumOfSquaresv2 = (mySum . squareAll) </li></ul><ul><li>> mySum [1..2000000] </li></ul><ul><li>2000001000000 </li></ul><ul><li>> sumOfSquares [1..10] </li></ul><ul><li>385 </li></ul><ul><li>> sumOfSquaresv2 [1..10] </li></ul><ul><li>385 </li></ul>
    58. 58. Foldl (Haskell) <ul><li>myReverse xs = foldl (x y -> y:x) [] xs </li></ul><ul><li>myReverse [1,2,3] </li></ul><ul><li>foldl (x y -> y:x) [] [1,2,3] </li></ul><ul><li>.. ([] 1 -> 1:[]) .. => [1] </li></ul><ul><li>foldl (x y -> y:x) [1] [2,3] </li></ul><ul><li>.. ([1] 2 -> 2:[1]) .. => [2,1] </li></ul>
    59. 59. Exercise set 4c 5 minutes
    60. 60. Answers to set 4c
    61. 61. problem 1 <ul><li>var foldl = function (step, init, xs) { </li></ul><ul><li>     return(  </li></ul><ul><li>         empty(xs) ?  init   : </li></ul><ul><li>         foldl( step , </li></ul><ul><li>                 step(init,first(xs)), </li></ul><ul><li>                 rest(xs) </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    62. 62. problem 2 <ul><li>var sum = function(xs) { </li></ul><ul><li>     return  foldl(add,0,xs); </li></ul><ul><li>}; </li></ul>
    63. 63. Map (JavaScript) 'The Spolsky Map' function spolsky_map(fn, a)     {         for (i = 0; i < a.length; i++)         {             a[i] = fn(a[i]) ;         }     } note the side effects
    64. 64. The Spolsky Reduce (foldl) function spolsky_reduce(fn, a, init)     {         var s = init;         for (i = 0; i < a.length; i++)             s = fn( s, a[i] );         return s;     }
    65. 65. Break 5 minutes
    66. 66. Exercise 5a 5 minutes
    67. 67. Answers to set 5a
    68. 68. problem 1 <ul><li>var last = compose(first,reverse); </li></ul>
    69. 69. problem 2 <ul><li>var reverse = function (xs) { </li></ul><ul><li>     return foldl(flip(build),[ ],xs); </li></ul><ul><li>}; </li></ul>
    70. 70. Standard Deviation <ul><li>1. calculate the arithmetic mean of the list </li></ul><ul><li>2. subtract the mean from all the numbers in the list </li></ul><ul><li>3. square the number in that list </li></ul><ul><li>4. calculate the sum of the list </li></ul><ul><li>5. divide the sum by length of list by 1 </li></ul><ul><li>6. get the square root of this number  </li></ul>
    71. 71. Standard Deviation <ul><li>1. calculate the arithmetic mean of the list </li></ul><ul><li>mean xs = sum xs / fromIntegral(length xs) </li></ul><ul><li>2. subtract the mean from all the numbers in the list </li></ul><ul><li>deviations xs = map (x -> x - m ) xs </li></ul><ul><li>                 where m = mean xs </li></ul><ul><li>3. square the numbers in that list </li></ul><ul><li>squareDeviations xs = map(^2) devs </li></ul><ul><li>                       where devs = deviations xs </li></ul><ul><li>4. calculate the sum of the list sumSquareDeviations xs = (sum . </li></ul><ul><li>squareDeviations) xs </li></ul><ul><li>5. divide the sum by length of list minus 1 </li></ul><ul><li>6. get the square root of this number  </li></ul><ul><li>sd xs = sqrt $ sumSqDev / lsub1 </li></ul><ul><li>  where sumSqDev = sumSquareDeviations xs </li></ul><ul><li>       lsub1 = fromIntegral $ ((-1+) . length)xs </li></ul>
    72. 72. Exercise 5b 10 minutes
    73. 73. Answers to set 5b
    74. 74. Standard Deviation (JavaScript) <ul><li>var mean = function (xs){ </li></ul><ul><li>     return(sum(xs)/flength(xs)); </li></ul><ul><li>}; </li></ul><ul><li>var deviations = function (xs) { </li></ul><ul><li>     var m = mean(xs);//we can remove this  </li></ul><ul><li>     return  map(function(x){return x-m;},xs); </li></ul><ul><li>}; </li></ul><ul><li>var squareDeviations = function(xs){ </li></ul><ul><li>     return  map(square,deviations(xs)); </li></ul><ul><li>}; </li></ul>
    75. 75. Standard Deviation (JavaScript) <ul><li>var sumSqDeviations = compose(sum,squareDeviations); </li></ul><ul><li>var sd = function(xs){ </li></ul><ul><li>     return  Math.sqrt(     (sumSqDeviations(xs)/(length(xs)-1))); </li></ul><ul><li>}; </li></ul>
    76. 76. Standard Deviation <ul><li>Haskell </li></ul><ul><li>> sd [1,4,5,9,2,10] </li></ul><ul><li>3.65604522218567 </li></ul><ul><li>JavaScript </li></ul><ul><li>> sd([1,4,5,9,2,10]); </li></ul><ul><li>3.65604522218567 </li></ul>
    77. 77. Function Currying
    78. 78. Currying (Haskell) <ul><li>myAdd x y = x + y </li></ul><ul><li>add8 = myAdd 8 </li></ul><ul><li>add9  = (+9) </li></ul><ul><li>> myAdd 8 9 </li></ul><ul><li>17 </li></ul><ul><li>> add8 9 </li></ul><ul><li>17 </li></ul><ul><li>> add9 8 </li></ul><ul><li>17 </li></ul>
    79. 79. Currying (JavaScript) <ul><li>var curry = function (f,a) { </li></ul><ul><li>     return( </li></ul><ul><li>function(){ </li></ul><ul><li>     var args =;   </li></ul><ul><li>     args.unshift(a); </li></ul><ul><li>     return f.apply(this, args); </li></ul><ul><li>} </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    80. 80. Currying (JavaScript) purely functional <ul><li>var curry = function (f,a) { </li></ul><ul><li>     return( </li></ul><ul><li>function(){ </li></ul><ul><li>    return( </li></ul><ul><li>( function( args ){ </li></ul><ul><li>    return f.apply(this, build(a,args)); </li></ul><ul><li>} ) </li></ul><ul><li>( </li></ul><ul><li>    ); </li></ul><ul><li>} </li></ul><ul><li>     ); </li></ul><ul><li>}; </li></ul>
    81. 81. Currying (JavaScript)  <ul><li>var add_three = function(a,b,c){ </li></ul><ul><li>     return a+b+c; </li></ul><ul><li>}; </li></ul><ul><li>>f1 = curry(add_three,1); </li></ul><ul><li>>f1(2,3) </li></ul><ul><li>6 </li></ul><ul><li>>f2 = curry(curry(add_three,1),2); </li></ul><ul><li>>f2(3) </li></ul><ul><li>6 </li></ul>
    82. 82. Exercise 6 10 minutes
    83. 83. Answers to set 6
    84. 84. problem 1 <ul><li>var makeAdder = curry(add,x); </li></ul><ul><li>var apiClosure = curry(apiSearch,apiKey); </li></ul>
    85. 85. problem 2 <ul><li>var unrealisticFunction = function (a, b){ </li></ul><ul><li>     (function(c){ </li></ul><ul><li>         return((function(d){ </li></ul><ul><li>                                 if(c<d){ </li></ul><ul><li>                                           return c+d; </li></ul><ul><li>                                           }else{ </li></ul><ul><li>                                           return c*d; </li></ul><ul><li>                                           } </li></ul><ul><li>                     };)(c+a) </li></ul><ul><li>         ); </li></ul><ul><li>     })(a*b) </li></ul><ul><li>}; </li></ul>
    86. 86. Thanks! email: twitter: willkurt
    87. 87. Still have time? awesome!
    88. 88. Haskell vs JavaScript
    89. 89. Types!!! <ul><li>areaOfTrapezoid :: Float -> Float -> Float -> Float </li></ul><ul><li>areaOfTrapezoid h b1 b2 = 0.5*h*(b1+b2) </li></ul><ul><li>evenList :: (Integral a) => [a] -> [a] </li></ul><ul><li>evenList xs  = filter even xs </li></ul>
    90. 90. Lazy Evaluation!!! <ul><li>  [2,4..] !! 108 </li></ul><ul><li>> 218 </li></ul>
    91. 91. Pattern Matching <ul><li>patternMatch [a,b,c] = b ++ c ++ a </li></ul><ul><li>patternMatch [a,b] = a ++ b </li></ul><ul><li>patternMatch [] = &quot;no list&quot; </li></ul><ul><li>patternMatch as = &quot;a larger list&quot; </li></ul>
    92. 92. Pattern Matching <ul><li>patternMatch [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;] </li></ul><ul><li>>&quot;bca&quot; </li></ul><ul><li>patternMatch [&quot;a&quot;,&quot;b&quot;] </li></ul><ul><li>>&quot;ab&quot; </li></ul><ul><li>patternMatch [ ] </li></ul><ul><li>>&quot;no list&quot; </li></ul><ul><li>patternMatch [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;] </li></ul><ul><li>>&quot;a larger list&quot; </li></ul>
    93. 93. Who actually uses this stuff?
    94. 94. Ocaml
    95. 97. more @
    96. 98. Haskell
    97. 99. ... continued more @