functional programming basics
TRANSCRIPT
![Page 1: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/1.jpg)
______ _ _ _ | ____| | | (_) | | | |__ _ _ _ __ ___| |_ _ ___ _ __ __ _| | | __| | | | '_ \ / __| __| |/ _ \| '_ \ / _` | | | | | |_| | | | | (__| |_| | (_) | | | | (_| | | |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|\__,_|_| _____ _ | __ \ (_) | |__) | __ ___ __ _ _ __ __ _ _ __ ___ _ __ ___ _ _ __ __ _ | ___/ '__/ _ \ / _` | '__/ _` | '_ ` _ \| '_ ` _ \| | '_ \ / _` | | | | | | (_) | (_| | | | (_| | | | | | | | | | | | | | | | (_| | |_| |_| \___/ \__, |_| \__,_|_| |_| |_|_| |_| |_|_|_| |_|\__, | __/ | __/ | |___/ |___/
____ __ __ __ __ __ ____ __ _ _ __ ____ __ ____ __ __ _ ( _ \ / _\ ( ) / _\ _( )( ) ( \ / _\ ( \/ ) / \( \ / _\ ( _ \ / _\ ( ( \ ) _ (/ \/ (_/\/ \/ \) \ )( ) D (/ \/ \/ \( O )) D (/ \ ) // \/ / (____/\_/\_/\____/\_/\_/\____/(__) (____/\_/\_/\_)(_/ \__/(____/\_/\_/(__\_)\_/\_/\_)__)
![Page 2: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/2.jpg)
http://eli.thegreenplace.net/wp-content/uploads/2008/05/tc_000.png
![Page 3: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/3.jpg)
http://sites.google.com/site/andrewsporfotilio/_/rsrc/1226818616654/Home/math_400.jpg
![Page 4: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/4.jpg)
Functional Programming is a style whose
underlying model of computation is the function.
f(x)
![Page 5: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/5.jpg)
Referential Transparency Higher Order functions Lazy Evaluation Pattern Matching
http://picasaweb.google.com/damodaran.balaji/DevCampChennaiJuly2010#5493004664487949618
![Page 6: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/6.jpg)
Lambda Calculus Continuations Monads Type Inference
![Page 7: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/7.jpg)
Referential Transparency Higher Order functions Lazy Evaluation Pattern Matching
![Page 8: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/8.jpg)
Referential Transparency “Equals can be replaced
with equals”
http://2.bp.blogspot.com/_DS3VD3b2euI/TRIcIUzduDI/AAAAAAAAAGA/Xp2E6kPxZCc/s1600/Pascal+the+chameleon+from+tangled+rapunzel+disney+cartoon.jpg
![Page 9: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/9.jpg)
Referential Transparency “Equals can be replaced with equals”
f(x) = g(x) + 5 h(x) = x * f(x)
![Page 10: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/10.jpg)
Referential Transparency “Equals can be replaced with equals”
f(x) = g(x) + 5 h(x) = x * f(x) h(x) = x * (g(x) + 5)
![Page 11: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/11.jpg)
public class ReferentiallyOpaque { private int x = 0; static int f(int y) { return x += y; } public static void main(String[] args) { ReferentiallyOpaque ro = new ReferentiallyOpaque(); System.out.println(ro.f(5)); System.out.println(ro.f(5)); } }
![Page 12: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/12.jpg)
public class ReferentiallyOpaque { private int x = 0;; static int f(int y) { return x += y; } public static void main(String[] args) { ReferentiallyOpaque ro = new ReferentiallyOpaque(); System.out.println(ro.f(5)); System.out.println(ro.f(5)); } }
5 10
![Page 13: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/13.jpg)
public class Example { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("Hello World"); System.out.println(buffer.reverse()); System.out.println(buffer.reverse()); } }
![Page 14: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/14.jpg)
public class Example { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("Hello World"); System.out.println(buffer.reverse()); System.out.println(buffer.reverse()); } }
dlroW olleH Hello World
![Page 15: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/15.jpg)
public class Example { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("Hello World"); StringBuffer s1 = buffer.reverse(); StringBuffer s2 = buffer.reverse(); System.out.println(s1 + " " + s2); } }
![Page 16: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/16.jpg)
public class Example { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("Hello World"); StringBuffer s1 = buffer.reverse(); StringBuffer s2 = buffer.reverse(); System.out.println(s1 + " " + s2); } }
Hello World Hello World
![Page 17: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/17.jpg)
Varying Variables Global State
![Page 18: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/18.jpg)
http://paulafanclub.com/wp-content/uploads/2010/12/confused.jpg
• Looping • File I/O • Modify Data • Do more than one thing?
![Page 19: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/19.jpg)
Looping
static int factorial(int number) { int f = 1; for (int i = number; i > 0; i--) f = f * i; return f; }
![Page 20: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/20.jpg)
http://www.shrink4men.com/wp-content/uploads/2010/11/dog-chasing-its-tail.jpg
Recursion
Looping
static int factorial(int number) { return (number == 1) ? number : number * factorial(number - 1); }
static int factorial(int number) { int f = 1; for (int i = number; i > 0; i--) f = f * i; return f; }
![Page 21: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/21.jpg)
Modifying data
static List<Integer> addToList(List<Integer> integers, int a) { integers.add(a); return integers; }
Mutable data structures
![Page 22: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/22.jpg)
Modifying data
static List<Integer> addToList(List<Integer> integers, int a) { integers.add(a); return integers; }
let addToList list a = list @ [a]
> let g = [1;2;3;];; val g : int list = [1; 2; 3] > addToList g 4;; val it : int list = [1; 2; 3; 4] > g;; val it : int list = [1; 2; 3] >
Immutable
http://4.bp.blogspot.com/_RpRScqAI8e4/SbNPgumsqPI/AAAAAAAAAK4/ZK8ZsfKJCmQ/s400/wolverine+hugh+jackman.jpg
![Page 23: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/23.jpg)
Referential Transparency Higher Order functions Lazy Evaluation Pattern Matching
![Page 24: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/24.jpg)
Higher Order functions “Functions as first class values”
http://www.dreamstime.com/looking-up-to-the-sky-thumb4655038.jpg
![Page 25: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/25.jpg)
Higher Order functions “Functions that can be passed as arguments”
def square(x): return x * x def add(function, x): return function(x) + function(x) print add(square, 5)
![Page 26: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/26.jpg)
Higher Order functions “Functions that can be returned as results”
function addNumber(x) { return function(y) { return x + y } } var add4With = addNumber(4) var add5With = addNumber(5) add4with(8) add5with(8) 12 13
![Page 27: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/27.jpg)
Higher Order functions “Functions that can be returned as results”
function addNumber(x) { return function(y) { return x + y } } var add4With = addNumber(4) var add5With = addNumber(5) add4with(8) add5with(8) 12 13
Lambda Functions
![Page 28: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/28.jpg)
Higher Order functions “Functions that can be returned as results”
function addNumber(x) { return function(y) { return x + y } } var add4With = addNumber(4) var add5With = addNumber(5) add4with(8) add5with(8) 12 13
Lambda Functions
Functions stored in data structures
![Page 29: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/29.jpg)
Higher Order functions “Currying”
let add x y = x + y val add : int -> int -> int > let add5 = add 5;; val add5 : (int -> int) > add5 4;; val it : int = 9 >
![Page 30: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/30.jpg)
Referential Transparency Higher Order functions Lazy Evaluation Pattern Matching
![Page 31: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/31.jpg)
Lazy Evaluation “Arguments in a function call are evaluated at most once.”
http://www.arngren.dk/image/Wallpaper/Comic/Calvin%20Hobbes/Sleeping1024.jpg
![Page 32: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/32.jpg)
Lazy Evaluation “Ability to invoke expressions only when its value is needed.”
static void Main(string[] args) { var lazy = new Lazy<int>(() => { Console.WriteLine("calculating..."); return 6 * 7; }); Console.WriteLine(lazy.Value); Console.WriteLine(lazy.Value); }
![Page 33: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/33.jpg)
Lazy Evaluation “Ability to invoke expressions only when its value is needed.”
static void Main(string[] args) { var lazy = new Lazy<int>(() => { Console.WriteLine("calculating..."); return 6 * 7; }); Console.WriteLine(lazy.Value); Console.WriteLine(lazy.Value); }
calculating... 42 42
![Page 34: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/34.jpg)
Lazy Evaluation “Memoization”
function Lazy_Memoized(def) { var cache = []; return function(i) { return (i in cache) ? cache[i] : (cache[i] = def.call(arguments.callee, i)); }; } var factorial = new Lazy_Memoized(function(i) { return i <= 1 ? i : i * this(i - 1); }); factorial(6)
![Page 35: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/35.jpg)
Lazy Evaluation “Ability to create infinite sequence in data structures”
http://softwarezku.co.cc/wp-content/uploads/2010/09/Visual-Studio-2010-Logo.png
![Page 36: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/36.jpg)
Referential Transparency Higher Order functions Lazy Evaluation Pattern Matching
![Page 37: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/37.jpg)
Pattern Matching “Writing several equations defining the same function”
http://softwareandfinance.com/images/vcpp-trig1.jpg
![Page 38: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/38.jpg)
f(x) = 1, when x = 0 = 3x + ex, when x > 0
Pattern Matching A typical function definition
![Page 39: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/39.jpg)
Pattern Matching A typical function definition
fac :: Integer -> Integer fac 0 = 1 fac n | n > 0 = n * fac(n-1)
let rec factorial = function | 0 -> 1 | n -> n * factorial(n - 1)
![Page 40: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/40.jpg)
![Page 41: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/41.jpg)
![Page 42: Functional programming basics](https://reader033.vdocuments.site/reader033/viewer/2022052823/5550f34fb4c905417d8b554e/html5/thumbnails/42.jpg)
References
• Concepts, Evolution and Application of Functional Programming Languages – Paul Hudak
• http://msdn.microsoft.com/en-us/library/dd547125.aspx - Pattern Matching in F#
• http://appden.com/javascript/lazy-list-comprehensions-in-javascript-a-lazy-evalution/
• http://images.google.com
• http://en.wikipedia.org
• http://www.haskell.org/haskellwiki/Haskell
• http://homepages.inf.ed.ac.uk/wadler/