Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional programming in F#

Avatar for chribben chribben
September 14, 2012

Functional programming in F#

About functional programming in general and F# in particular

Avatar for chribben

chribben

September 14, 2012
Tweet

Other Decks in Programming

Transcript

  1. Back in the days... λ LISP ISWIM APL FP ML

    1958 1966 1962 1977 1973 Haskell 1988 Scala 2003 Ocaml 1996 F# 2005 Clojure 2007 1930-ish Erlang 1986
  2. Some definitions Program Data - Logic - Control Declarative Logic

    - Referential transparency Imperative Logic - Control - Side effects
  3. 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
  4. Why Functional Programming? let sum lst = foldr (+) lst

    0 let product lst = foldr (*) lst 1 let append l1 l2 = foldr cons l1 l2 let length lst = foldr (+) lst 0 let map f lst = foldr (cons << f) lst []
  5. Real-world uses F# Svea Ekonomi, Microsoft (Halo), Credit Suisse, Prover

    Haskell AT & T, Scrive Erlang Ericsson, Klarna, Basho, Facebook Lisp Yahoo Store Scala Twitter, LinkedIn
  6. Why F#? Functional + OO + .NET + Open Source

    => The Most Powerful Language In The WORLD! Scala-crowd goes: O rly? Lisp-crowd goes: F-what?
  7. F# “F# is a practical, functional-first language that lets you

    write simple code to solve complex problems"
  8. Complex Simple static 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); }
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. (Function) values in F# let ten = 10 let square

    x = x*x let squareList = List.map (fun n -> n*n) let addThree = (+) 3
  17. (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
  18. 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
  19. F# data types - Union type Shape = | Circle

    of float | Rectangle of float * float | Triangle of float * float
  20. F# data types – Recursive union type BinTree = |

    Leaf of int | Node of BinTree * BinTree
  21. F# data types - Option public Document getDocByID(int id); VS

    val getDocByID : int -> Document option
  22. F# data types - Option let res = match getDocByID

    id with | None -> …handle not found… | Some(d) -> …do something with d…
  23. F# data types - List type List<'a> = | Nil

    | Cons of 'a*List<'a> Nil + 'a*List<'a>
  24. 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]
  25. Higher-order functions let rec double l = match l with

    | [] -> List.empty | h::t -> h*2::double t
  26. Higher-order functions let rec length (l:string list) = match l

    with | [] -> List.empty | h::t -> h.Length::length t
  27. Higher-order functions let rec map f l = match l

    with | [] -> List.empty | h::t -> f(h)::map t
  28. Function composition let f x = h(g(x)) VS let f

    = g >> h Composition operator
  29. 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
  30. Summary Functional programming lets you Focus on the problem and

    less on noise Write consice and readable code Create composable programs F# gives you Access to the .NET framework Cross-platform Multiparadigm for solving real-world problems
  31. So... λ = Future? Complex vs Simple Lots of plumbing

    vs Logic Spaghetti vs Composability Headache vs Fun