Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

1,368 views

Published on

A presentation about functional programming in general and F# in particular.

No Downloads

Total views

1,368

On SlideShare

0

From Embeds

0

Number of Embeds

10

Shares

0

Downloads

34

Comments

0

Likes

4

No embeds

No notes for slide

- 1. Functional programming in F# The Why, What and How
- 2. Back in the days... ISWIM λ 1958 LISP 1966 1930-ish 1962 APL 1973 ML 2007 1977 FP Clojure2005 F# 1986 Erlang 2003 Scala 1988 Haskell Ocaml 1996
- 3. Some definitionsProgram Data - Logic - ControlDeclarative Logic - Referential transparencyImperative Logic - Control - Side effects
- 4. Functional programming language DeclarativeFirst class λHigher-Order ImmutabilityFunctions
- 5. The PAIN of imperativenessScrewupability Shared mutable states
- 6. 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
- 7. 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 []
- 8. FP demand UK ClojureScalaF# Erlang
- 9. Real-world usesF# Svea Ekonomi, Microsoft (Halo), Credit Suisse, ProverHaskell AT & T, ScriveErlang Ericsson, Klarna, Basho, FacebookLisp Yahoo StoreScala Twitter, LinkedIn
- 10. Why F#?Functional + OO + .NET + Open Source => The Most Powerful Language In The WORLD! Scala-crowd goes: O rly? Lisp-crowd goes: F-what?
- 11. F#“F# is a practical, functional-first languagethat lets you write simple codeto solve complex problems"
- 12. 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);}
- 13. Complex Simple 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);}
- 14. Complex Simple 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);}
- 15. Complex Simple 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);}
- 16. Complex Simple 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;}
- 17. Complex Simple DiffTree ( tree, tree2) XFoldTree( x, l, r, t, t2 =>let (Node(x2,l2,r2)) = t2Node((x2,t=t2), l l2, r r2)) (fun _ _ -> Leaf) tree tree2
- 18. Complex Simple 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
- 19. Complex Simplelet 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
- 20. (Function) values in F#let ten = 10let square x = x*xlet squareList = List.map (fun n -> n*n)let addThree = (+) 3
- 21. (Function) values in F#let outer x = let inner y = printfn "outer + inner = %d" (x + y) inner 21let rec sum list = match list with | [] -> 0 | head::tail -> head + sum tail
- 22. Pattern matchinglet rec merge l1 l2 = Parallel pattern match l1, l2 with OR pattern | [], xs | xs, [] -> xs | x::xs, (y::_ as ys) when x <= y -> x::merge xs ys | xs, y::ys -> y::merge xs ys Named pattern Guarded pattern
- 23. F# data typesAlgebraic Data Types+ *
- 24. F# data types - Uniontype Shape = | Circle of float | Rectangle of float * float | Triangle of float * float
- 25. F# data types – Recursive union type BinTree = | Leaf of int | Node of BinTree * BinTree
- 26. F# data types - Option type Option<a> = | Some of a | None
- 27. F# data types - Option public Document getDocByID(int id); VSval getDocByID : int -> Document option
- 28. F# data types - Optionlet res = match getDocByID id with | None -> …handle not found… | Some(d) -> …do something with d…
- 29. F# data types - Listtype List<a> = | Nil | Cons of a*List<a> Nil + a*List<a>
- 30. F# data types - Listlet l1 = ["a"; "list"; "of"; "strings"]let l2 = "a"::"consed"::"list"::[]let l3 = [1..42]let l4 = [for i in l3 -> i*i]
- 31. Higher-order functionslet rec double l = match l with | [] -> List.empty | h::t -> h*2::double t
- 32. Higher-order functionslet rec length (l:string list) = match l with | [] -> List.empty | h::t -> h.Length::length t
- 33. Higher-order functionslet rec map f l = match l with | [] -> List.empty | h::t -> f(h)::map t
- 34. Higher-order functionsmap (fun x -> 2*x) listmap (fun (x:string) -> x.Length) list
- 35. Pipelining h(g(f(x))) VS x |> f |> g |> hPipeline operator
- 36. Function composition let f x = h(g(x)) VS let f = g >> h Composition operator
- 37. Function compositionlet double_then_sum = List.map ((*)2) >> List.reduce (+)
- 38. Synchronous -> Asynchronouslet 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
- 39. F# 3.0• Type Providers• Query Expressions• Triple-quoted strings
- 40. SummaryFunctional programming lets you Focus on the problem and less on noise Write consice and readable code Create composable programsF# gives you Access to the .NET framework Cross-platform Multiparadigm for solving real-world problems
- 41. So...Complex vs Simple Spaghetti vs Composability λ = Future? Headache vs Fun Lots of plumbing vs Logic
- 42. Jaywaychristian.jacobsen@jayway.com@chribbenFunctional Programming Sthlm User GroupJayway seminarsØredev

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment