functional programming in f sharp
DESCRIPTION
A presentation about functional programming in general and F# in particular.TRANSCRIPT
![Page 1: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/1.jpg)
Functional programming in F#
The Why, What and How
![Page 2: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/2.jpg)
Back in the days...
λ LISPISWIM
APLFP
ML
1958 1966
1962
1977
1973
Haskell1988Scala2003Ocaml
1996
F#2005
Clojure2007
1930-ish
Erlang1986
![Page 3: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/3.jpg)
Some definitions
ProgramData - Logic - Control
DeclarativeLogic - Referential transparency
ImperativeLogic - Control - Side effects
![Page 4: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/4.jpg)
Functional programming language
First classHigher-Order Functions
Immutability
Declarative
λ
![Page 5: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/5.jpg)
The PAIN of imperativeness
Screwupability
Shared mutable states
![Page 6: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/6.jpg)
Why not functional programming?
• Too low dev replacability• Legacy code not functional• Bad interop, few libs• Devs find it too hard• Customers don’t care
anyway
![Page 7: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/7.jpg)
Why Functional Programming?
let sum lst = foldr (+) lst 0let product lst = foldr (*) lst 1let append l1 l2 = foldr cons l1 l2let length lst = foldr (+) lst 0let map f lst = foldr (cons << f) lst []
![Page 8: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/8.jpg)
FP demand UK
ScalaClojure
F# Erlang
![Page 9: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/9.jpg)
Real-world uses
F# Svea Ekonomi, Microsoft (Halo), Credit Suisse, Prover
Haskell AT & T, ScriveErlang Ericsson, Klarna, Basho, FacebookLisp Yahoo StoreScala Twitter, LinkedIn
![Page 10: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/10.jpg)
Why F#?
Functional + OO + .NET + Open Source =>The Most Powerful
Language In TheWORLD!
Scala-crowd goes: O rly?Lisp-crowd goes: F-what?
![Page 11: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/11.jpg)
F#
“F# is a practical, functional-first languagethat lets you write simple codeto solve complex problems"
![Page 12: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/12.jpg)
Complex Simplestatic Tree<KeyValuePair<A, bool>> DiffTree<A>(Tree<A> tree, Tree<A> tree2){ return XFoldTree( (A x, Func<Tree<A>, Tree<KeyValuePair<A, bool>>> l,
Func<Tree<A>, Tree<KeyValuePair<A, bool>>> r, Tree<A> t)
=> (Tree<A> t2) => Node(new KeyValuePair<A, bool>(t2.Data,
ReferenceEquals(t, t2)), l(t2.Left), r(t2.Right)), x1 => y => null, tree)(tree2);}
![Page 13: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/13.jpg)
Tree<KeyValuePair<A, bool>> DiffTree<A>(Tree<A> tree, Tree<A> tree2)
{ XFoldTree(
(A x, Func<Tree<A>, Tree<KeyValuePair<A, bool>>> l, Func<Tree<A>, Tree<KeyValuePair<A, bool>>> r, Tree<A> t)
=> (Tree<A> t2) => Node(new KeyValuePair<A, bool>(t2.Data,
ReferenceEquals(t, t2)), l(t2.Left), r(t2.Right)), x1 => y => null, tree)(tree2);}
Complex Simple
![Page 14: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/14.jpg)
DiffTree ( tree, tree2)
{ XFoldTree(
( x, l, r,
t) => (Tree<A> t2) => Node(new KeyValuePair<A, bool>(t2.Data,
ReferenceEquals(t, t2)), l(t2.Left), r(t2.Right)), x1 => y => null, tree)(tree2);}
Complex Simple
![Page 15: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/15.jpg)
DiffTree ( tree, tree2)
{ XFoldTree(
( x, l, r,
t, t2) => Node(new KeyValuePair<A, bool>(t2.Data,
ReferenceEquals(t, t2)), l(t2.Left), r(t2.Right)), x1 => y => null, tree)(tree2);}
Complex Simple
![Page 16: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/16.jpg)
DiffTree ( tree, tree2)
{ XFoldTree(
( x, l, r,
t, t2) =>
let (Node(x2,l2,r2)) = t2 Node((x2,t=t2), l l2, r r2)) (fun _ _ -> Leaf) tree tree2;}
Complex Simple
![Page 17: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/17.jpg)
DiffTree ( tree, tree2)
XFoldTree( x, l,
r, t, t2
=> let (Node(x2,l2,r2)) = t2
Node((x2,t=t2), l l2, r r2)) (fun _ _ -> Leaf) tree tree2
Complex Simple
![Page 18: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/18.jpg)
let DiffTree ( tree, tree2) =
XFoldTree(fun x, l,
r, t, t2
-> let (Node(x2,l2,r2)) = t2
Node((x2,t=t2), l l2, r r2)) (fun _ _ -> Leaf) tree tree2
Complex Simple
![Page 19: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/19.jpg)
let DiffTree( tree, tree2) = XFoldTree(fun x, l, r, t, t2 ->
let (Node(x2,l2,r2)) = t2Node((x2,t=t2), l l2, r r2)) (fun _ _ -> Leaf) tree
tree2
Complex Simple
![Page 20: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/20.jpg)
(Function) values in F#
let ten = 10
let square x = x*x
let squareList = List.map (fun n -> n*n)
let addThree = (+) 3
![Page 21: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/21.jpg)
(Function) values in F#
let outer x = let inner y = printfn "outer + inner = %d" (x + y) inner 21
let rec sum list = match list with | [] -> 0 | head::tail -> head + sum tail
![Page 22: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/22.jpg)
let rec merge l1 l2 = match l1, l2 with | [], xs | xs, [] -> xs | x::xs', (y::_ as ys) when x <= y -> x::merge xs' ys | xs, y::ys' -> y::merge xs ys'
Pattern matching
Named pattern Guarded pattern
Parallel pattern
OR pattern
![Page 23: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/23.jpg)
F# data types
+ *Algebraic Data Types
![Page 24: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/24.jpg)
F# data types - Union
type Shape = | Circle of float | Rectangle of float * float | Triangle of float * float
![Page 25: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/25.jpg)
F# data types – Recursive union
type BinTree = | Leaf of int | Node of BinTree * BinTree
![Page 26: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/26.jpg)
F# data types - Option
type Option<'a> = | Some of 'a | None
![Page 27: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/27.jpg)
F# data types - Option
public Document getDocByID(int id);
VS
val getDocByID : int -> Document option
![Page 28: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/28.jpg)
F# data types - Option
let res = match getDocByID id with | None -> …handle not found…
| Some(d) -> …do something with d…
![Page 29: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/29.jpg)
F# data types - List
type List<'a> = | Nil | Cons of 'a*List<'a>
Nil + 'a*List<'a>
![Page 30: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/30.jpg)
F# data types - List
let l1 = ["a"; "list"; "of"; "strings"]
let l2 = "a"::"consed"::"list"::[]
let l3 = [1..42]
let l4 = [for i in l3 -> i*i]
![Page 31: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/31.jpg)
Higher-order functions
let rec double l =match l with| [] -> List.empty| h::t -> h*2::double t
![Page 32: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/32.jpg)
Higher-order functions
let rec length (l:string list) =match l with
| [] -> List.empty | h::t -> h.Length::length t
![Page 33: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/33.jpg)
Higher-order functions
let rec map f l = match l with
| [] -> List.empty | h::t -> f(h)::map t
![Page 34: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/34.jpg)
Higher-order functions
map (fun x -> 2*x) list
map (fun (x:string) -> x.Length) list
![Page 35: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/35.jpg)
Pipelining
h(g(f(x)))
VS
x |> f |> g |> h
Pipeline operator
![Page 36: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/36.jpg)
Function composition
let f x = h(g(x))
VS
let f = g >> h
Composition operator
![Page 37: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/37.jpg)
Function composition
let double_then_sum = List.map ((*)2) >> List.reduce
(+)
![Page 38: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/38.jpg)
Synchronous -> Asynchronous
let getData (url:string) = let r = WebRequest.Create(url) let resp = r.GetResponse() use stream = resp.GetResponseStream() use reader = new StreamReader(stream) let data = reader.ReadToEnd() data
![Page 39: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/39.jpg)
F# 3.0
• Type Providers• Query Expressions• Triple-quoted strings
![Page 40: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/40.jpg)
Summary
Functional programming lets youFocus on the problem and less on noiseWrite consice and readable codeCreate composable programs
F# gives youAccess to the .NET frameworkCross-platformMultiparadigm for solving real-world problems
![Page 41: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/41.jpg)
So...
λ = Future?
Complex vs Simple
Lots of plumbing vs Logic
Spaghetti vs Composability
Headache vs Fun
![Page 42: Functional programming in f sharp](https://reader036.vdocuments.site/reader036/viewer/2022081422/554fb299b4c905ad218b5323/html5/thumbnails/42.jpg)
?