SlideShare a Scribd company logo
Introduction to Functional
           Programming
             with Haskell and JavaScript
                      Will Kurt




Will Kurt, Creative Commons Attribution-ShareAlike 3.0
"A language that doesn't effect
     how you think about
  programming is not worth
          learning"
           --Alan Perlis
So what is Functional Programming?




 http://www.flickr.com/photos/foundphotoslj/466713478/
What does this mean?




http://www.flickr.com/photos/61417318@N00/4908148942/
No Side Effects!




http://www.flickr.com/photos/rka/1733453/
http://www.flickr.com/photos/23912576@N05/3056871500/
Haskell!




http://www.flickr.com/photos/micurs/4870514382/
JavaScript!!!




 http://www.flickr.com/photos/jurvetson/96972777/
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]
First class functions




http://www.flickr.com/photos/richardmoross/2211308689/
First Class Functions (Haskell)
add2 x = 2 + x
add3 x = 3 + x

> add2 5
7

> add3 5
8
First Class Functions (Haskell)
argIs2 func = (func) 2
argIs3 func = (func) 3

> argIs2(add2)
4
> argIs2(add3)
5
> argIs3(add2)
5
> argIs3(add3)
6
First Class Functions (JavaScript)


function argIs2(func){
  return func(2);
}
function argIs3(func){
  return func(3);
}
Lambda Functions




http://www.flickr.com/photos/jeremymates/2362399109/
Lambda Function (Haskell)


add4 = (x -> 4+x)


>argIs2((x -> 4+x))
6
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});
Lambda Function (JavaScript)


$.ajax({ url: "test.html", context: document.body, success: function()
{
     $(this).addClass("done");
    }});


$("#exec").click(function(){
 $("#results").prepend("<li>Normal Handler</li>");
});
Closures




http://www.flickr.com/photos/theredproject/3431459572/
Closures (Haskell)


add5 = (+5)
makeAdder val = (+val)
makeAdderWithLambda val = (x->x+val)

add6 = makeAdder 6
add7 = makeAdderWithLambda 7

> add5 5
10
> add6 5
11
> add7 5
12
Closures (JavaScript)


var makeAdder = function(val){
   return(
function(x){
  return val+x;
  }
   );
};

>var add6 = makeAdder(6);
>add6(5);
11
>add6(8);
14
Higher Order Functions




http://www.flickr.com/photos/73416633@N00/2425022159/
Map




http://www.flickr.com/photos/rosenkranz/3052214847/
Map (Haskell)
doubleAll xs = map (*2) xs
squareAll xs = map (^2) xs
squareAndAdd xs = map (x->x*x+x) xs
upperCase 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"
Map (Haskell)
doubleAllv2 = map (*2)
squareAllv2 = map (^2)
squareAndAddv2 = map (x->x*x+x)
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
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
Filter
Filter (Haskell)


evenList xs = filter even xs
mod17list xs = filter (== (`mod` 17) 0) xs
doubleEvens 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]
Filter (JavaScript)


var filter = function (test,xs) {
   return (
empty(xs) ? []:
test(first(xs)) ? build(first(xs),
          filter(test,rest(xs))) :
      filter(test,rest(xs)));
};
Foldl (Reduce)




http://en.wikipedia.org/wiki/File:Sermon_in_the_Deer_Park_depicted_at_Wat_Chedi_Liem-KayEss-1.jpeg
Foldl (Haskell)

mySum xs = foldl (+) 0 xs
myReverse xs = foldl (x y -> y:x) [] xs
sumOfSquares 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
Foldl (Haskell)

myReverse xs = foldl (x y -> y:x) [] xs
myReverse [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]
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;
  }
Foldl (JavaScript) Purely Functional


var foldl = function (fn, init, xs) {
   return(
      empty(xs) ? init:
      foldl(fn,
          fn(init, first(xs)),
          rest(xs))
   );
};
Function Currying
Currying (Haskell)


myAdd x y = x + y
add8 = myAdd 8
add9 = (+9)


> myAdd 8 9
17
> add8 9
17
> add9 8
17
Currying (JavaScript)

var curry = function (f,a) {
   return(
function(){
  var args = Array.prototype.slice.call(arguments);
  args.unshift(a);
  return f.apply(this, args);
}
   );
};
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))
  );
}
   );
};
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
Standard Deviation
1. calculate the arithmetic mean of the list

2. subtract the mean from all the numbers in the list
3. square the number in that list
4. calculate the sum of the list
5. divide the sum by length of list by 1
6. get the square root of this number
Standard Deviation
1. calculate the arithmetic mean of the list
mean xs = sum xs / fromIntegral(length xs)
2. subtract the mean from all the numbers in the list
deviations xs = map (x -> x - m ) xs
         where m = mean xs
3. square the numbers in that list
squareDeviations xs = map(^2) devs
            where devs = deviations xs
4. calculate the sum of the list
sumSquareDeviations xs = (sum .
squareDeviations) xs
5. divide the sum by length of list by 1
6. get the square root of this number
sd xs = sqrt $ sumSqDev / lsub1
 where sumSqDev = sumSquareDeviations xs
    lsub1 = fromIntegral $ ((-1+) . length)xs
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));
};
Standard Deviation (JavaScript)

var sumSqDeviations = compose(sum,squareDeviations);

var sd = function(xs){
   return Math.sqrt( (sumSqDeviations(xs)/(flength(xs)-1)));
};
Standard Deviation
Haskell
> sd [1,4,5,9,2,10]
3.65604522218567


JavaScript
> sd([1,4,5,9,2,10]);
3.65604522218567
Who actually uses this stuff?




            http://cufp.org/
Ocaml
Introduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScript
more @ http://www.scala-lang.org/node/1658
Haskell
... continued




more @ http://haskell.org/haskellwiki/Haskell_in_industry
Questions?


email: wkurt@unr.edu

twitter: willkurt

More Related Content

PDF
Fp intro scala
ODP
2.3 implicits
PPT
Functional Patterns for the non-mathematician
PDF
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
PPTX
Millionways
PDF
Introduction to Scala
PDF
Short intro to ES6 Features
PDF
Brief tour of psp-std
Fp intro scala
2.3 implicits
Functional Patterns for the non-mathematician
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Millionways
Introduction to Scala
Short intro to ES6 Features
Brief tour of psp-std

What's hot (20)

PDF
String functions
PDF
Introduction to Functional Programming with Scala
PPTX
Oh Composable World!
PPT
Python 101 language features and functional programming
PPTX
Functional linear data structures in f#
PDF
FP in scalaで鍛える関数型脳
PPT
Functional programming with_scala
ODP
Collections In Scala
PPT
String slide
PDF
An Introduction to JavaScript: Week One
PDF
Closure, Higher-order function in Swift
PDF
Data Structures in javaScript 2015
PDF
Swift 함수 커링 사용하기
PPTX
Scala training workshop 02
PPTX
하스켈 프로그래밍 입문 4
PPTX
7. tuples, set &amp; dictionary
PDF
Scala parallel-collections
PPTX
하스켈 프로그래밍 입문 3
PPSX
Scala @ TomTom
String functions
Introduction to Functional Programming with Scala
Oh Composable World!
Python 101 language features and functional programming
Functional linear data structures in f#
FP in scalaで鍛える関数型脳
Functional programming with_scala
Collections In Scala
String slide
An Introduction to JavaScript: Week One
Closure, Higher-order function in Swift
Data Structures in javaScript 2015
Swift 함수 커링 사용하기
Scala training workshop 02
하스켈 프로그래밍 입문 4
7. tuples, set &amp; dictionary
Scala parallel-collections
하스켈 프로그래밍 입문 3
Scala @ TomTom
Ad

Viewers also liked (12)

PPT
How I got over my fear of teleportation
PPT
Intro to Functional Programming Workshop (code4lib)
PPTX
Gazing into the Unknown: The Fundamentals of Data Science
PDF
Functional Programming and Haskell - TWBR Away Day 2011
PDF
PPTX
Functional programming seminar (haskell)
PDF
Haskell for data science
PDF
Introduction to haskell
PDF
Beginning Haskell, Dive In, Its Not That Scary!
PDF
Functional programming with haskell
PDF
The other side of functional programming: Haskell for Erlang people
PDF
Haskell for the Real World
How I got over my fear of teleportation
Intro to Functional Programming Workshop (code4lib)
Gazing into the Unknown: The Fundamentals of Data Science
Functional Programming and Haskell - TWBR Away Day 2011
Functional programming seminar (haskell)
Haskell for data science
Introduction to haskell
Beginning Haskell, Dive In, Its Not That Scary!
Functional programming with haskell
The other side of functional programming: Haskell for Erlang people
Haskell for the Real World
Ad

Similar to Introduction to Functional Programming with Haskell and JavaScript (20)

PDF
Go Says WAT?
ODP
The secrets of inverse brogramming
PDF
Michal Malohlava presents: Open Source H2O and Scala
PDF
Spark workshop
KEY
関数潮流(Function Tendency)
PDF
Transducers in JavaScript
PDF
Sequence and Traverse - Part 2
PDF
From Javascript To Haskell
DOCX
Spark_Documentation_Template1
PDF
TI1220 Lecture 6: First-class Functions
PDF
Refactoring to Macros with Clojure
PDF
Chapter 04-discriminant analysis
PPTX
Thinking Functionally with JavaScript
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PPTX
Seminar PSU 10.10.2014 mme
PPTX
Rendering Art on the Web - A Performance compendium
PDF
A bit about Scala
PDF
Being functional in PHP (DPC 2016)
KEY
Five Languages in a Moment
PPTX
Es6 hackathon
Go Says WAT?
The secrets of inverse brogramming
Michal Malohlava presents: Open Source H2O and Scala
Spark workshop
関数潮流(Function Tendency)
Transducers in JavaScript
Sequence and Traverse - Part 2
From Javascript To Haskell
Spark_Documentation_Template1
TI1220 Lecture 6: First-class Functions
Refactoring to Macros with Clojure
Chapter 04-discriminant analysis
Thinking Functionally with JavaScript
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Seminar PSU 10.10.2014 mme
Rendering Art on the Web - A Performance compendium
A bit about Scala
Being functional in PHP (DPC 2016)
Five Languages in a Moment
Es6 hackathon

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
cuic standard and advanced reporting.pdf
PDF
KodekX | Application Modernization Development
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Sensors and Actuators in IoT Systems using pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Advanced methodologies resolving dimensionality complications for autism neur...
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.
Spectral efficient network and resource selection model in 5G networks
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
madgavkar20181017ppt McKinsey Presentation.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25 Week I
cuic standard and advanced reporting.pdf
KodekX | Application Modernization Development

Introduction to Functional Programming with Haskell and JavaScript