Practical F#Ryan RileyCatapult Systems, Inc.Houston TechFest 2009
AgendaCore ConceptsBasic SyntaxWhy Consider F#When to use F#When not to use F#Summary
Core Concepts
F# ParadigmsFunctionalObject-orientedLanguage-oriented
Functional Programming ConceptsFirst-Class Functions (and Events)Strong Type InferenceImmutabilityMake side-effects explicit (the M-word)Functional Reactive ProgrammingEasy ConcurrencyActor-model Messaging (similar to Erlang)Lazy Evaluation
Basic Syntax
letSimilar to var in C#.
TuplesType-safe collection of valuesC# out parameters are returned as tuples
Class Declaration
Abstract Classes
Interfaces
Inheritance
RecordsBut then, why bother?
Discriminated UnionsFunctional inheritance
Pattern Matching
Extension Methods
Object Expressions
Control Flow with Pipes
Function Composition
Computation Workflows
QuotationsThis is supposed to be a practical discussion!Quotations are similar to LINQ Expressions.
REPL
Visual Studio Integration
Why Consider F#?You can do everything you did in C# within F#Some things are simpler, some harderSimpler: many OO design patternsSimpler: Asynchronous tasksHarder: GUI programmingTerrific for specialized tasks (polyglot)Many great F# librariesTerse language = fewer lines of code = $ saved
When to use F#Asynchronous operationsConcurrent operationsMessaging (MailboxProcessor)Transformation operations (like SSIS)Computation-intensive operations (like SSAS)Creating Domain Specific Languages (DSLs)Aspect-Oriented Programming (AOP)Testing
FP vs. OOP
#1 Reason to use F#	Your entire application is already just a series of LINQ statements!
TestingFsTestFsUnit
Generate Test Data
Mathematical CalculationsProject Euler Problem 4:Project Euler Problem 3:Project Euler Problem 6
Excel Financial Functions
ParsersFSharp.PowerPack has a lexer and parserFparsecCashel
Automated Builds with Fake
Async Workflows
Async Workflows from C#
Functional AOP?
When not to use F# Just want to use the newest thingGUI programmingYou can do itCan’t use the designersCan still be a great toolWhen you are already proficient in C# and are focused almost entirely on OOP.Stick to what you know. You’ll be faster.
Summary“Write tomorrow’s legacy code today with C#”Chris Smith’s F# FactsTry F#Fall in love with F#Use F#The End
ResourcesF# is still CTPTry it in VS2010 Beta 1Or use the VS2008 CTP installerRead booksFoundations of F#(Pickering)Expert F# (Syme)Functional Programming for the Real World (Petricek and Skeet)F# for Scientists (Harrop)
Resourceshttp://channel9.msdn.com/pdc2008/TL11/http://tomasp.net/blog/fsharp-webcast-functional.aspxhttp://lorgonblog.spaces.live.com/blog/http://codebetter.com/blogs/matthew.podwysocki/default.aspx
The end …
MonadsMonads are about function composition.Take a monoid:f(x) -> xg(x) -> xf(g(x)) -> xf.g x -> xAdd a transformation:f(x) -> Mxg(x) -> Mxf(g(x)) -> Mxf.g x -> Mx
Creating Monadic BuildersThe parts of a monad:(in F#, this would be a workflow builder)x.Let (value, transform): sets a value, like LINQ’s Select().x.Bind (value, transform): splits the value from the transform and passes it into the next function, like LINQ’s SelectMany().x.Return (value): returns the result.x.Delay (transform): kicks off the workflow.
Computation workflow structureTypical structure of a workflow builder:type WorkflowBuilder () =  member x.Bind (value, transform) = transform value  member x.Return (value) = fun () -> value  member x.Delay (transform) = fun () -> transform ()let workflow = new WorkflowBuilder()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

Practical F#