Functional programming in f sharp


Published on

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

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • This talk is about functional programming in general and F# in particular.
  • This man, Alonzo Church, invented Lambda Calculus which is a formal theory for functionality.Many of the languages depicted here represents family of languages so for example LISP has a number of derivatives such as CLISP, Scheme, Clojure and ML has Standard ML, CAML, OCAML and F#LISP (Conditional expr, Cons cells, Garbage collection, S-expressions)APL doesn’t rely on lambda expressionISWIM (if you see what I mean). It’s interesting to see that it could’ve been a description of F# (infix, let where, Indentation). Invented by Peter Landin (also the inventor of the first VM and the expression ’syntactic sugar’)ML uses Hindley-Milner type inference and parametric polymorphism, abstract data typesFP created by John Backus (creator of Fortran) who gave the ”Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs”Miranda has list comprehensions and possibility to convert infix operators to functionsOcaml is multi-paradigm in that it combines OO and FuncScalaF# is a Ocaml dialectClojure is a LISP dialect running on JVM and CLR with built in support for concurrency
  • In declarative programming style focus is on the logic (the what) and control is handed to the language.In imperative programming style both control and logic is the responsibility of the programmer.Side effects = state changes may occurReferential transparency = an expression may be substituted by its value and it won't affect the end result.Ref.
  • The definition of a functional programming langugage is a bit vague but some traits that most fp languages support are...A lot of langugages support a functional programming style like Javascript, C#, C++ etc. so you could say there’s a purity scaleDeclarative style means that you describe the solution to the problem instead of what steps needs to be performed to solve the problem. Expressions are used for building the program instead of statements.Referential transparency means that what you declare is written in stone. You can refer to the stones in order to create new data structures but you can’t change it. Values are thus immutable.Functions are first class citizens meaning that they can be treated as any other value, i.e. they can passed to other functions as arguments and be returned from functions.Since we want to use decalrative style we can’t really use while and for loops as control mechanisms for iterating over data structures. Instead, recursion is utilized.
  • So what’s wrong with imperative programming?Due to mutability, it takes an inhumanly amount of DISCIPLINE to make STATE changes EXPLICIT.
  • Some arguments raised against fp, however:The leap from Java/C# to Scala/F# isn’t that big; there’s a different way of thinking but devs like to think.Interop/few libs is not a valid argument for Scala/F#Customer do care about getting new functionality fast and which is robust.
  • There are many good reasons: More concise and readable code Fewer bugs due to immutability Easier to reason about code due to referential transparency and therefore easier to prove correctness which leads to better optimizations by compiler Programmer effeciency Easier to parallelize etcBut apart from these reasons: It's fun! It bends your mind into thinking differently (if you come from the oo/imperative world)One of the strongest benefits is that it lends it self to modular programs through composability. Here are some examples of how we can compose functions using just a few simple primitives.
  • F# is a pragmatic language - "write simple code to solve complex problems“Interop with .NET’s CLI makes it very powerfulThe fact that a large company supports it makes it a good choice
  • Strong, static and parametric polymorphic type systemMulti-paradigmCurrent version 3.0
  • C#-programmers sometime find F#’s syntax hard to decipher...This is a rather complex function in C# with pretty low readability, somewhat due to strange formatting, but mainly due to ”noise” from type annotations. It calculates the difference between two tree structures
  • Remove keywords...
  • Remove type annotations...
  • Currying
  • Pattern matching and tuples
  • Remove brackets, semi-colons and curly braces
  • Add F# keywords and create expression
  • And format
  • let denotes binding value to variable, i.e. not assignment (remember immutability)Function definition using letfun is the keyword for lambda function
  • Nested function definitionsRecursive function
  • In functional languages algebraic data types are often used as data carriers (all CLI types are of course available as well). They are types composed by other types.The two most common classes are the sum type and the product type.
  • A union is a sum type that takes the shape of one of its composed types.It can be compared to a object hierarchy in the OO paradigm and in fact, the compiler creates a base class and subclasses for each construct.The correspondance to method dispatching is done by using pattern matching in F#
  • Union types can be recursively definded.
  • In C# for example a returned reference object might be null but it’s easy to forget to check this case. When option type is used in a match expression then the compiler issues a warning if the None-case isn’t covered.What is returned in the above case when id not found?
  • One of the most central types in FPSemantically eqvivalent to the sum of union and a product type. In F# Nil is [] and Cons (hd*tl) is hd::tl
  • Lists can be constructed in several different ways: by semi-colon separated listed values, using the cons operator, through sequence expression
  • Functions as values (first-class functions) makes it possible for a function to take other functions as an arguments and/or return a function. Such functions are called higher-order functions.Higher-order functions allows us to build abstractions and makes it possible to write declarative code since the control structure is hidden in these functions. Examples are map, fold, reduce, filter etc.
  • map is an reusable abstracted hof that hides the control structure and lets us instead focus on the logic of our program
  • Declarative way of using the hof map, i.e. ”map this function to the list l”
  • Pipelining lets you pipe data in the ”x-direction” instead of in the ”z-direction” which makes it more readable.It also benefits type inference
  • Is functional programming the future? I hope I’ve managed to show that functional programming meansSimpler codeMore solid programsFUN
  • Functional programming in f sharp

    1. 1. Functional programming in F# The Why, What and How
    2. 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. 3. Some definitionsProgram Data - Logic - ControlDeclarative Logic - Referential transparencyImperative Logic - Control - Side effects
    4. 4. Functional programming language DeclarativeFirst class λHigher-Order ImmutabilityFunctions
    5. 5. The PAIN of imperativenessScrewupability Shared mutable states
    6. 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. 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. 8. FP demand UK ClojureScalaF# Erlang
    9. 9. Real-world usesF# Svea Ekonomi, Microsoft (Halo), Credit Suisse, ProverHaskell AT & T, ScriveErlang Ericsson, Klarna, Basho, FacebookLisp Yahoo StoreScala Twitter, LinkedIn
    10. 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. 11. F#“F# is a practical, functional-first languagethat lets you write simple codeto solve complex problems"
    12. 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. 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. 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. 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. 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. 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. 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. 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. 20. (Function) values in F#let ten = 10let square x = x*xlet squareList = (fun n -> n*n)let addThree = (+) 3
    21. 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. 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. 23. F# data typesAlgebraic Data Types+ *
    24. 24. F# data types - Uniontype Shape = | Circle of float | Rectangle of float * float | Triangle of float * float
    25. 25. F# data types – Recursive union type BinTree = | Leaf of int | Node of BinTree * BinTree
    26. 26. F# data types - Option type Option<a> = | Some of a | None
    27. 27. F# data types - Option public Document getDocByID(int id); VSval getDocByID : int -> Document option
    28. 28. F# data types - Optionlet res = match getDocByID id with | None -> …handle not found… | Some(d) -> …do something with d…
    29. 29. F# data types - Listtype List<a> = | Nil | Cons of a*List<a> Nil + a*List<a>
    30. 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. 31. Higher-order functionslet rec double l = match l with | [] -> List.empty | h::t -> h*2::double t
    32. 32. Higher-order functionslet rec length (l:string list) = match l with | [] -> List.empty | h::t -> h.Length::length t
    33. 33. Higher-order functionslet rec map f l = match l with | [] -> List.empty | h::t -> f(h)::map t
    34. 34. Higher-order functionsmap (fun x -> 2*x) listmap (fun (x:string) -> x.Length) list
    35. 35. Pipelining h(g(f(x))) VS x |> f |> g |> hPipeline operator
    36. 36. Function composition let f x = h(g(x)) VS let f = g >> h Composition operator
    37. 37. Function compositionlet double_then_sum = ((*)2) >> List.reduce (+)
    38. 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. 39. F# 3.0• Type Providers• Query Expressions• Triple-quoted strings
    40. 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. 41. So...Complex vs Simple Spaghetti vs Composability λ = Future? Headache vs Fun Lots of plumbing vs Logic
    42. 42. Programming Sthlm User GroupJayway seminarsØredev