Introduction to Functional Programming with Haskell and JavaScript
-
Upload
will-kurt -
Category
Technology
-
view
4.509 -
download
2
description
Transcript of Introduction to Functional Programming with Haskell and JavaScript
![Page 1: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/1.jpg)
Introduction to Functional Programming
with Haskell and JavaScriptWill Kurt
Will Kurt, Creative Commons Attribution-ShareAlike 3.0
![Page 2: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/2.jpg)
"A language that doesn't effect how you think about
programming is not worth learning"
--Alan Perlis
![Page 3: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/3.jpg)
So what is Functional Programming?
http://www.flickr.com/photos/foundphotoslj/466713478/
![Page 4: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/4.jpg)
What does this mean?
http://www.flickr.com/photos/61417318@N00/4908148942/
![Page 5: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/5.jpg)
No Side Effects!
http://www.flickr.com/photos/rka/1733453/
![Page 6: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/6.jpg)
http://www.flickr.com/photos/23912576@N05/3056871500/
![Page 7: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/7.jpg)
Haskell!
http://www.flickr.com/photos/micurs/4870514382/
![Page 8: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/8.jpg)
JavaScript!!!
http://www.flickr.com/photos/jurvetson/96972777/
![Page 9: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/9.jpg)
Lists!!!
first [1,2,3,4] -> 1 (aka: car, head)
rest [1,2,3,4] -> [2,3,4] (aka: cdr, tail)
empty [1,2,3,4] -> false (aka: null?, nil? empty?,[])
empty [] -> true
build 1 , [2,3,4] -> [1,2,3,4] (aka: cons, ':' )
build [1] , [2,3,4] -> [[1],2,3,4]
![Page 10: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/10.jpg)
First class functions
http://www.flickr.com/photos/richardmoross/2211308689/
![Page 11: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/11.jpg)
First Class Functions (Haskell)add2 x = 2 + xadd3 x = 3 + x
> add2 57
> add3 58
![Page 12: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/12.jpg)
First Class Functions (Haskell)argIs2 func = (func) 2argIs3 func = (func) 3
> argIs2(add2)4> argIs2(add3)5> argIs3(add2)5> argIs3(add3)6
![Page 13: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/13.jpg)
First Class Functions (JavaScript)
function argIs2(func){ return func(2);}function argIs3(func){ return func(3);}
![Page 14: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/14.jpg)
Lambda Functions
http://www.flickr.com/photos/jeremymates/2362399109/
![Page 15: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/15.jpg)
Lambda Function (Haskell)
add4 = (\x -> 4+x)
>argIs2((\x -> 4+x))6
![Page 16: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/16.jpg)
Lambda Function (JavaScript)
var add4 = function(x){ return x+2;}
var example = argIs2(function(x){return x+2});
var exampple2 = argIs2(function(x){return x+1000});
![Page 17: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/17.jpg)
Lambda Function (JavaScript)
$.ajax({ url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); }});
$("#exec").click(function(){ $("#results").prepend("<li>Normal Handler</li>");});
![Page 18: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/18.jpg)
Closures
http://www.flickr.com/photos/theredproject/3431459572/
![Page 19: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/19.jpg)
Closures (Haskell)
add5 = (+5)makeAdder val = (+val)makeAdderWithLambda val = (\x->x+val)
add6 = makeAdder 6add7 = makeAdderWithLambda 7
> add5 510> add6 511> add7 512
![Page 20: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/20.jpg)
Closures (JavaScript)
var makeAdder = function(val){ return(function(x){ return val+x; } );};
>var add6 = makeAdder(6);>add6(5);11>add6(8);14
![Page 21: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/21.jpg)
Higher Order Functions
http://www.flickr.com/photos/73416633@N00/2425022159/
![Page 22: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/22.jpg)
Map
http://www.flickr.com/photos/rosenkranz/3052214847/
![Page 23: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/23.jpg)
Map (Haskell)
doubleAll xs = map (*2) xssquareAll xs = map (^2) xssquareAndAdd xs = map (\x->x*x+x) xsupperCase s = map toUpper s
> doubleAll [1,2,3,4][2,4,6,8]> squareAll [1,2,3,4][1,4,9,16]> squareAndAdd [1,2,3,4][2,6,12,20]> upperCase "doggie""DOGGIE"
![Page 24: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/24.jpg)
Map (Haskell)
doubleAllv2 = map (*2)squareAllv2 = map (^2)squareAndAddv2 = map (\x->x*x+x)
![Page 25: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/25.jpg)
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
![Page 26: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/26.jpg)
Map (JavaScript) Purely functional version
var map = function (func,xs) { return (empty(xs) ? [] :build(func(first(xs)), map(func,rest(xs))));
};
note: xs and the returned list are distinct
![Page 27: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/27.jpg)
Filter
![Page 28: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/28.jpg)
Filter (Haskell)
evenList xs = filter even xsmod17list xs = filter (== (`mod` 17) 0) xsdoubleEvens xs = (doubleAll . evenList) xs
> evenList [1,2,3,4,5,6][2,4,6]> mod17list [1..100][17,34,51,68,85]> doubleEvens [0,3..27][0,12,24,36,48]
![Page 29: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/29.jpg)
Filter (JavaScript)
var filter = function (test,xs) { return (empty(xs) ? []:test(first(xs)) ? build(first(xs), filter(test,rest(xs))) : filter(test,rest(xs))); };
![Page 30: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/30.jpg)
Foldl (Reduce)
http://en.wikipedia.org/wiki/File:Sermon_in_the_Deer_Park_depicted_at_Wat_Chedi_Liem-KayEss-1.jpeg
![Page 31: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/31.jpg)
Foldl (Haskell)
mySum xs = foldl (+) 0 xsmyReverse xs = foldl (\x y -> y:x) [] xssumOfSquares xs = foldl (+) 0 (map (^2) xs)sumOfSquaresv2 = (mySum . squareAll)
> mySum [1..2000000]2000001000000> myReverse [4,16..200][196,184,172,160,148,136,124,112,100,88,76,64,52,40,28,16,4]> sumOfSquares [1..10]385> sumOfSquaresv2 [1..10]385
![Page 32: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/32.jpg)
Foldl (Haskell)
myReverse xs = foldl (\x y -> y:x) [] xsmyReverse [1,2,3]
foldl (\x y -> y:x) [] [1,2,3]
.. (\[] 1 -> 1:[]) .. => [1]
foldl (\x y -> y:x) [1] [2,3]
.. (\[1] 2 -> 2:[1]) .. => [2,1]
![Page 33: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/33.jpg)
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; }
![Page 34: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/34.jpg)
Foldl (JavaScript) Purely Functional
var foldl = function (fn, init, xs) { return( empty(xs) ? init: foldl(fn, fn(init, first(xs)), rest(xs)) );};
![Page 35: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/35.jpg)
Function Currying
![Page 36: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/36.jpg)
Currying (Haskell)
myAdd x y = x + yadd8 = myAdd 8add9 = (+9)
> myAdd 8 917> add8 917> add9 817
![Page 37: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/37.jpg)
Currying (JavaScript)
var curry = function (f,a) { return(function(){ var args = Array.prototype.slice.call(arguments); args.unshift(a); return f.apply(this, args);} );};
![Page 38: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/38.jpg)
Currying (JavaScript) purely functional
var curry = function (f,a) { return(function(){ return((function(args){ return f.apply(this, build(a,args));})(Array.prototype.slice.call(arguments)) );} );};
![Page 39: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/39.jpg)
Currying (JavaScript)
var add_three = function(a,b,c){ return a+b+c;};
>f1 = curry(add_three,1);>f1(2,3)6
>f2 = curry(curry(add_three,1),2);>f2(3)6
![Page 40: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/40.jpg)
Standard Deviation
1. calculate the arithmetic mean of the list
2. subtract the mean from all the numbers in the list3. square the number in that list4. calculate the sum of the list5. divide the sum by length of list by 16. get the square root of this number
![Page 41: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/41.jpg)
Standard Deviation1. calculate the arithmetic mean of the listmean xs = sum xs / fromIntegral(length xs)2. subtract the mean from all the numbers in the listdeviations xs = map (\x -> x - m ) xs where m = mean xs3. square the numbers in that listsquareDeviations xs = map(^2) devs where devs = deviations xs4. calculate the sum of the listsumSquareDeviations xs = (sum .squareDeviations) xs5. divide the sum by length of list by 16. get the square root of this number sd xs = sqrt $ sumSqDev / lsub1 where sumSqDev = sumSquareDeviations xs lsub1 = fromIntegral $ ((-1+) . length)xs
![Page 42: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/42.jpg)
Standard Deviation (JavaScript)
var mean = function (xs){ return(sum(xs)/flength(xs));};
var deviations = function (xs) { var m = mean(xs);//we can remove this return map(function(x){return x-m;},xs);};
var squareDeviations = function(xs){ return map(square,deviations(xs));};
![Page 43: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/43.jpg)
Standard Deviation (JavaScript)
var sumSqDeviations = compose(sum,squareDeviations);
var sd = function(xs){ return Math.sqrt( (sumSqDeviations(xs)/(flength(xs)-1)));};
![Page 44: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/44.jpg)
Standard Deviation
Haskell> sd [1,4,5,9,2,10]3.65604522218567
JavaScript> sd([1,4,5,9,2,10]);3.65604522218567
![Page 45: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/45.jpg)
Who actually uses this stuff?
http://cufp.org/
![Page 46: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/46.jpg)
Ocaml
![Page 47: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/47.jpg)
![Page 48: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/48.jpg)
![Page 49: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/49.jpg)
more @ http://www.scala-lang.org/node/1658
![Page 50: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/50.jpg)
Haskell
![Page 51: Introduction to Functional Programming with Haskell and JavaScript](https://reader034.fdocuments.net/reader034/viewer/2022052301/55494084b4c905054d8b5025/html5/thumbnails/51.jpg)
... continued
more @ http://haskell.org/haskellwiki/Haskell_in_industry