What the math geeks don't want you to know about F#


Published on

Microsoft’s recently launched F# programming language is only for math geeks, right? If you don’t know what Turing Completeness is all about or why the Lambda Calculus is so important, that invitation you’ve anxiously been waiting on to the secret F# meeting in your town just isn’t coming. Sorry. At least that’s what it seems like. But there’s a whole different story about F# just waiting to be told: a story that includes all of us just trying to solve hard problems with the tools we have. At the end of the day, F# is just another great tool. You already have a lot of tools in the bag today: C#, VB.NET, Java, JavaScript, CSS, HTML, PL/SQL and T-SQL, maybe even some Python or Ruby. Building a modern software application, there are so many choices. A few of them are right and many of them are disasters just waiting to happen. F# can help you solve some problems in elegant ways where languages like C# and Java might make you struggle. Of course, you don’t have to write your entire application in F# any more than you would trying writing a web application without HTML, CSS and JavaScript. Come learn how to weave little bits of F# code into the fabric of your larger application to make it scalable, highly performant and expressive. Apologies to the math geeks ahead of time but we won’t be discussing monads, universal computers or anything else that might distract us from learning about this new language or how to use it during this talk.

Published in: Technology
1 Like
  • 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 not about teaching the F# language. We'll look at some code, for sure. This talk is for devs who know C# or VB.NET and looking at typical F# makes you say, "Huh?" I'll highlight some key concepts and tell you about stuff that will trip you up. In that context, we'll also talk about how you can approach the language in a much more manageable way, leveraging the skills you already have.
  • Functional languages tend to be a lot more succinct or terse than their imperative counterparts. F# is both functional and object-oriented. But the syntax is close to its functional cousins like OCaml and Haskell that are very terse. In those languages, like F#, you tend to be able to express a lot of intent with very little code. There's a tipping point, of course, where languages become so terse, so compact that the lose the ability to convey meaningful information to human readers. F# strikes a nice balance but it can be overwhelming to read if you're accustomed to languages like C#, much more so VB.NET which is praised for its "wordiness" by those who love it.The central theme to functional languages is, of course, functions. They are first class citizens. Declaring a lambda expression is easy using the fun keyword. But standalone functions can be declared in such a way that they can be connected together, or composed using a concept called currying. When I first began working with functional languages, I thought that currying had something to do with the spicy curries used in Indian and Asian foods. These are sauces made up of a lot of varying spices. I mistakenly thought that currying in functional programming got its name from mixing lots of functions together to get a result. I was naïve. We all are at some point in learning new things. Currying in functional programming gets its name from Haskell Curry, a major name in computer science whose first name is also lent to the purely functional programming language called Haskell.Currying (and composition of functions in general) are very important concepts in FP languages. Being able to pass functions to other functions and lazily evaluate results only when they are needed, immutability is possible. And when you can have truly immutable data structures, you can make the leap to expression-orientation, where you tell the compiler what you want to accomplish, not exactly how to do it. When the compiler and runtime tooling can look at an expression abstractly and farm varying parts off to different processors, you begin to take advantage of the so-called multi-core revolution that's under way right now. Writing multi-threaded code is hard. Even the best of us, who've been writing symmetric multi-processing (SMP) code for decades can make serious but subtle mistakes trying to farm out a complex operation to all of the available CPUs.Multi-processing is not the only reason to code functionally. Functional software is easier to test in many cases. If you think about it, how many of your unit tests are designed to test capabilities to mutating object state? Yeah, a lot of them.
  • A discriminated union that can contain a leaf node or another binary tree. Great for building binary trees of data. Notice how terse the syntax is. It might read, here's a binary tree of some unknown type. It can contain either a Leaf of that unknown type or another binary tree called Node. (create the type in FSI and talk about the output here) Now let's write a function to print out a tree:let recprintBinaryTreeValuest = match t with | Leaf x -> printfn "%i" x | Node (l, r) ->printBinaryTreeValueslprintBinaryTreeValuesrNotice the rec keyword? That means recursive. In F#, you must mark recursive functions as such. We use the let keyword to assign symbols to things. The printBinaryTree symbol is assigned to the code below it. F# is whitespace sensitive like other languages I love, e.g. Python. I think it makes the code so much more readable to get rid of all the curly braces and the BEGIN/END type keywords. The parameter to this method is also declared without a lot of decoration. That symbol is "t". The equals sign after that separates the symbol assignment from the code. The code starts with a pattern match. It says to match the parameter t to either a Leaf or a Node from our BinaryTree type. When it's a Leaf, just print out the value there. When it's a Node, recurse twice to visit the left and right trees there instead. There's no return type from this function which is why the output type is listed as "unit". Now let's create a binary tree and run this depth-first visitor to print it out:printBinaryTreeValues (Node ((Node (Leaf 1, Leaf 2)), (Node (Leaf 3, Leaf 4))))Cool. Here's a slightly more interesting function that will visit each node and print out the data with more detail and indentation:let printBinaryTreet = let recprintBinaryTreet indent = match t with | Leaf x -> printfn "%sLeaf %i" indent x | Node (l, r) ->printfn "%sLeft Branch" indentprintBinaryTreel (indent + " ")printfn "%sRight Branch" indentprintBinaryTreer (indent + " ")printBinaryTreet ""Notice that this function called printBinaryTree has another function defined within it. The inner function is limited in scope, of course. And using the same name for the inner function is OK because the signatures are different. See how the inner function takes 2 parameters while the outer one only takes one parameter? The inner function uses that new parameter to say how far to indent information that's printed out depending on how deep the traversal is in the tree. Run this with the same tree we used before:printBinaryTree (Node ((Node (Leaf 1, Leaf 2)), (Node (Leaf 3, Leaf 4))))Nice. Now, since the BinaryTree is generic, should we able pass tree of string nodes to it? Let's try this:printBinaryTree (Node ((Node (Leaf "Kevin", Leaf "Donna")), (Node (Leaf "Ted", Leaf "Charlotte"))))That didn't work. Why? The error message says something about expecting integers when we gave it strings. Go back and look at the response that FSI gave us when we created this new function. It says:valprintBinaryTree : BinaryTree<int> -> unitThat's odd. Nowhere in the definition of this function did I say that the type of the binary tree was integers did I? Look closely. Buried in the printfn statement for the Leaf type is "%i" which tells F# to interpret the parameter as an integer. This is an example of how deep F#'s type inference engine goes. Change that %i formatter and replace it with %O which tells F# that it's an object we’re passing and that it should box values as necessary and invoke ToString() on them. Save the new function into the REPL. What does it say is the type?valprintBinaryTree : BinaryTree<'a> -> unitAhhh! Much better. Now retry the sample data with strings in the nodes to see if it works. It does! Hooray!
  • Here are some examples:let biggestFloat = max 3.0 3.1415927let biggestInt = max 7 2let biggestString = max "Kevin" "Donna"
  • Some ancient code to find the highest common factor written in C#:public inthcf( intx, inty ){int result = 0; while (result == 0) { if (x < y) {y %= x; if (y == 0) result = x; } else {x %= y; if (x == 0) result = y; } } return result;}// porting the iterative code directly to F# - yuck!let hcf a b = let mutable x = a let mutable y = b let mutable result = 0 while result = 0 do if x < y theny <- y % x if y = 0 then result <- x elsex <- x % y if x = 0 then result <- y result// try this call – hcf should be 12hcf 29292 4524That's some ugly F# - mutation is a warning sign. If you have to mark something mutable, you are most likely writing bad F#. There are cases where you may be working with third party libraries that depend on mutation. And it's OK to do it then, of course. But for your code, always try to avoid data mutation. If you need to change data, copy it, changing what you need and pass that to a function. This is much better:let rechcf a b = if a = 0 then belifb = 0 then aelif a < b then hcf a (b % a) else hcf (a % b) bFor fun, try this:hcf 17350618 26025927Jenny's phone number is, in fact, a prime number. 
  • Read this: Successively reduce the lesser parameter to the integer remainder of division with the greater parameter until one reaches zero. So much more readable than the iterative version. And recursion is the more natural way to express this idea.
  • A workflow builder is like an AOP-hookable class. You inject methods for implementing these 12 methods, as many or as few as you need to handle flow constructs and bindings. In this simple example, we'll create a TraceBuilder that simply traces the Bind, Delay and Result events during the workflow execution.Define 3 functions for Bind, Result and Delay:let bind value1 function1 = printfn "Binding %A." value1 function1 value1let result value1 =printfn "Returning result: %A" value1 fun () -> value1let delay function1 = fun () -> function1()Now tie those implementations into the TraceBuilder class by name:type TraceBuilder() = member x.Bind(value1, function1) = bind value1 function1 member x.Return(value1) = result value1 member x.Delay(function1) = printfn "Starting traced execution." delay function1Instantiate the builder:let trace = new TraceBuilder()Bind it to the expression code:let trace1 = trace { let x = 7 let! y = 5 let sum = x + y return sum }And execute:let output = trace1()A great way to learn the usefulness of workflows is by studying one of the built in workflows called async:open System.Netopen System.Threadingopen Microsoft.FSharp.Control.WebExtensionslet fetchAsync(name, url:string) =async { try let uri = new System.Uri(url) let webClient = new WebClient() let! html = webClient.AsyncDownloadString(uri)printfn "Thread %i: Read %d characters for %s" Thread.CurrentThread.ManagedThreadIdhtml.Length name with | ex -> printfn "%s" (ex.Message); }let urlList = [ "Kevin's Blog", "http://devjourney.com" "MSDN", "http://msdn.microsoft.com" "Bing", "http://www.bing.com" ]let runAll() =urlList |> Seq.mapfetchAsync |> Async.Parallel |> Async.RunSynchronously |> ignorerunAll()
  • Open Visual Studio and select the online templates when creating a new project. Filter to F# and examine the available templates.
  • When someone gives an FP talk and they day catamorphism, please raise you hand and say, "Isn't a catamorphism just a fold operation on a sequence?" If they say, "Well… yeah." Then ask, "Why can't you just say fold? Why do you have to use mathematics terms to explain something so simple to us?" Speakers in the FP world need to learn to speak to us on our terms and in the language that we understand.Anamorphism = unfold
  • What the math geeks don't want you to know about F#

    1. 1. What the Math Geeks Don't Want You to Know about F#<br />Tips for OvercomingObstacles to Learning F#<br />August 2010<br />Kevin Hazzard, C# MVP<br />
    2. 2. What This Talk Is & Is Not<br />This talk does not intend to teach F# deeply<br />This talk is for C# / VB.NET programmers trying to answer the questions:<br />Why should I care about functional languages?<br />What makes F# worth learning?<br />What's going to slow me down on the journey?<br />
    3. 3. What makes a language functional?<br />Compositional and often expression-oriented<br />Supports functions as first-class types<br />Often promotes lazy evaluation<br />Extensive use of higher-order functions<br />Avoids memory and I/O side-effects<br />Prefers recursion over iteration<br />Excellent for concurrent programming<br />May have polymorphic data types withextensive pattern matching capabilities<br />
    4. 4. A Teaching Example<br />type BinaryTree<'a> =<br /> | Leaf of 'a<br /> | Node of BinaryTree<'a> * BinaryTree<'a><br />
    5. 5. Obstacle #1<br />Pattern matching is foreign and pervasive.<br />Pattern matching is at the core of F#<br />When learning F#, focus on every pattern matching example you can find<br />Start with the simple ones, e.g. discriminated unions, move up and out<br />Treat this learning as a block and learn it deeply, in a very focused way<br />
    6. 6. Obstacle #2<br />Type inference makes F# feel like a scripting language.<br />If you're a Pythonista, you will feel at home writing F# - the types get out of your way<br />If not…<br />Watch carefully for inferred types, especially while you are learning – use the FSI REPL a lot<br />Use automatic generalization to reduce code volume and increase simplicity<br />
    7. 7. Automatic Generalization<br />No obstacle but something to be aware of<br />What type does this operate on?<br />let max a b = if a > b then a else b<br />F# automatically generalizes complete expressions when it can<br />
    8. 8. Recursion – Port This to F#<br />public inthcf( intx, inty )<br />{<br />int result = 0;<br /> while (result == 0) {<br /> if (x < y) {<br />y %= x;<br /> if (y == 0)<br /> result = x;<br /> }<br /> else {<br />x %= y;<br /> if (x == 0)<br /> result = y;<br /> }<br /> }<br /> return result;<br />}<br />
    9. 9. Isn't This Succinct?<br />let rechcf a b =if a = 0 then belifb = 0 then aelif a < b then hcf a (b % a)else hcf (a % b) b<br />
    10. 10. Tail Recursion Optimization<br />Recursive functions that do no extra work after the recursion can often be converted to while loops<br />F# does this automatically if the code is structured to allow for it<br />The JITter will sometimes do this on compiled code<br />No guarantees though<br />
    11. 11. Obstacle #3<br />Recursion may be at odds with your belief system. <br />Many of us were taught to fear recursion<br />Stop thinking that way<br />Every time you write an iteration think,"Could this be done with recursion instead?"<br />Write your F# code so that it is tail optimizable<br />
    12. 12. Workflows (Computation Expressions)<br />Bind<br />Delay<br />Return<br />ReturnFrom<br />Combine<br />For<br />TryFinally<br />TryWith<br />Using<br />While<br />YieldFrom<br />Zero<br />
    13. 13. Obstacle #4<br />Workflows are central to F# and tough to understand.<br />Think of workflows like little Domain Specific Languages (DSL)<br />Builders are like AOP-enabled classes, allowing you to hook and define those 12 core functions<br />When the F# code in the computation expression is evaluated, your code gets to direct the work<br />Focus on understanding the built-in async workflow<br />
    14. 14. VS Gallery F# Templates<br />When you first look at the F# templates in VS 2010, you may be underwhelmed<br />The F# team and others are always adding F# templates to ease development<br />Daniel Mohl (@dmohl) has written some excellent templates that are available in the online gallery<br />
    15. 15. Obstacle #5<br />You must rewire your brain a bit to learn F#.<br />F# is part of the .NET ecosystem<br />Great tooling in Visual Studio 2010<br />Full access to all .NET types and members<br />Seamless access to & from C# and VB.NET<br />F# is multi-paradigm<br />Emphasizes data immutability and the evaluation of expressions<br />But also supports traditional OOP concepts<br />
    16. 16. Don't Get Hung Up On Terms<br />Lambda Calculus<br />Turing Completeness<br />Universal Computer<br />Endofunctors<br />Cata/Ana-morphism<br />Co-algebras<br />Category Theory<br />"How does this help me to<br />write better software?"<br />
    17. 17. Stuff That Is Important<br />Composition / Currying<br />Pattern Matching<br />Type inference<br />Immutability<br />Side-effects<br />Recursion<br />Workflows<br />
    18. 18. Obstacle #6<br />Functional programming is a new fad. It will pass.<br />Anyone want to guess how long functional programming has been around?<br />OOP dominated because it was the best way to reduce complexity and promote reuse in CPU-bound systems<br />Functional languages are leader languages<br />Data structures<br />Message and event passing<br />Dynamic typing and type inferencing<br />Generics<br />Garbage collection<br />
    19. 19. The History of F#<br />Started in 2002 in Microsoft Research<br />Based on languages like ML and OCaml<br />Influenced great stuff in .NET<br />Generics<br />LINQ<br />Visual Studio support in versions 2005 & 2008<br />Became a first-class language citizen inVisual Studio 2010<br />
    20. 20. Demo<br />Check out the F# Samplescode.msdn.microsoft.com/fsharpsamples<br />Explore Tutorial.fs with Alt + Enter<br />Samples101<br />