Practical F#

3,820 views

Published on

Presented at Houston Tech Fest 2009.

F# is the new kid on the block, and many are wondering why they should care. F# offers a lot of really elegant solutions to some tasks many avoid simply because of the pain involved. What many don't realize is that F# was the basis for a lot of time-saving tools now available in C#, such as LINQ. That's not to say everyone should switch all their projects from VB.NET and C# to F#. Find out how you can start leveraging F# in your current projects by using the right language for the right job.

Published in: Technology
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total views
3,820
On SlideShare
0
From Embeds
0
Number of Embeds
275
Actions
Shares
0
Downloads
81
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide
  • Practical F#

    1. 1. Practical F#<br />Ryan Riley<br />Catapult Systems, Inc.<br />Houston TechFest 2009<br />
    2. 2. Agenda<br />Core Concepts<br />Basic Syntax<br />Why Consider F#<br />When to use F#<br />When not to use F#<br />Summary<br />
    3. 3. Core Concepts<br />
    4. 4. F# Paradigms<br />Functional<br />Object-oriented<br />Language-oriented<br />
    5. 5. Functional Programming Concepts<br />First-Class Functions (and Events)<br />Strong Type Inference<br />Immutability<br />Make side-effects explicit (the M-word)<br />Functional Reactive Programming<br />Easy Concurrency<br />Actor-model Messaging (similar to Erlang)<br />Lazy Evaluation<br />
    6. 6. Basic Syntax<br />
    7. 7. let<br />Similar to var in C#.<br />
    8. 8. Tuples<br />Type-safe collection of values<br />C# out parameters are returned as tuples<br />
    9. 9. Class Declaration<br />
    10. 10. Abstract Classes<br />
    11. 11. Interfaces<br />
    12. 12. Inheritance<br />
    13. 13. Records<br />But then, why bother?<br />
    14. 14. Discriminated Unions<br />Functional inheritance<br />
    15. 15. Pattern Matching<br />
    16. 16. Extension Methods<br />
    17. 17. Object Expressions<br />
    18. 18. Control Flow with Pipes<br />
    19. 19. Function Composition<br />
    20. 20. Computation Workflows<br />
    21. 21. Quotations<br />This is supposed to be a practical discussion!<br />Quotations are similar to LINQ Expressions.<br />
    22. 22. REPL<br />
    23. 23. Visual Studio Integration<br />
    24. 24. Why Consider F#?<br />You can do everything you did in C# within F#<br />Some things are simpler, some harder<br />Simpler: many OO design patterns<br />Simpler: Asynchronous tasks<br />Harder: GUI programming<br />Terrific for specialized tasks (polyglot)<br />Many great F# libraries<br />Terse language = fewer lines of code = $ saved<br />
    25. 25. When to use F#<br />Asynchronous operations<br />Concurrent operations<br />Messaging (MailboxProcessor)<br />Transformation operations (like SSIS)<br />Computation-intensive operations (like SSAS)<br />Creating Domain Specific Languages (DSLs)<br />Aspect-Oriented Programming (AOP)<br />Testing<br />
    26. 26. FP vs. OOP<br />
    27. 27. #1 Reason to use F#<br /> Your entire application is already just a series of LINQ statements!<br />
    28. 28. Testing<br />FsTest<br />FsUnit<br />
    29. 29. Generate Test Data<br />
    30. 30. Mathematical Calculations<br />Project Euler Problem 4:<br />Project Euler Problem 3:<br />Project Euler Problem 6<br />
    31. 31. Excel Financial Functions<br />
    32. 32. Parsers<br />FSharp.PowerPack has a lexer and parser<br />Fparsec<br />Cashel<br />
    33. 33. Automated Builds with Fake<br />
    34. 34. Async Workflows<br />
    35. 35. Async Workflows from C#<br />
    36. 36. Functional AOP?<br />
    37. 37. When not to use F# <br />Just want to use the newest thing<br />GUI programming<br />You can do it<br />Can’t use the designers<br />Can still be a great tool<br />When you are already proficient in C# and are focused almost entirely on OOP.<br />Stick to what you know. You’ll be faster.<br />
    38. 38. Summary<br />“Write tomorrow’s legacy code today with C#”<br />Chris Smith’s F# Facts<br />Try F#<br />Fall in love with F#<br />Use F#<br />The End<br />
    39. 39. Resources<br />F# is still CTP<br />Try it in VS2010 Beta 1<br />Or use the VS2008 CTP installer<br />Read books<br />Foundations of F#(Pickering)<br />Expert F# (Syme)<br />Functional Programming for the Real World (Petricek and Skeet)<br />F# for Scientists (Harrop)<br />
    40. 40. Resources<br />http://channel9.msdn.com/pdc2008/TL11/<br />http://tomasp.net/blog/fsharp-webcast-functional.aspx<br />http://lorgonblog.spaces.live.com/blog/<br />http://codebetter.com/blogs/matthew.podwysocki/default.aspx<br />
    41. 41. The end …<br />
    42. 42. Monads<br />Monads are about function composition.<br />Take a monoid:f(x) -&gt; xg(x) -&gt; xf(g(x)) -&gt; x<br />f.g x -&gt; x<br />Add a transformation:f(x) -&gt; Mxg(x) -&gt; Mxf(g(x)) -&gt; Mx<br />f.g x -&gt; Mx<br />
    43. 43. Creating Monadic Builders<br />The parts of a monad:(in F#, this would be a workflow builder)<br />x.Let (value, transform): sets a value, like LINQ’s Select().<br />x.Bind (value, transform): splits the value from the transform and passes it into the next function, like LINQ’s SelectMany().<br />x.Return (value): returns the result.<br />x.Delay (transform): kicks off the workflow.<br />
    44. 44. Computation workflow structure<br />Typical structure of a workflow builder:type WorkflowBuilder () = member x.Bind (value, transform) = transform value member x.Return (value) = fun () -&gt; value member x.Delay (transform) = fun () -&gt; transform ()let workflow = new WorkflowBuilder()<br />Execute like so:let myWorkflow = workflow { let x = 1 // x.Let let! y = 2 // x.Bind return x + y // x.Return }let result = myWorkflow () // x.Delay<br />

    ×