Phillip Trelford Blog: 1
Goals Introduce F# Non-goals Provide in-depth understanding Mass conversion to functional programming cult Sell books 2
1. Background 2. Language Syntax 3. Code Samples 3
F# is multi-paradigm language, that is: Functional Declarative Object Orientated Imperative.Net language with VS integration (A bit like Python) 4
© Trayport PROPRIETARY & CONFIDENTIAL Key Features Functions are idempotent Functions are first class High order functions Languages: Haskell Excel JavaScript 5
Communities Scientific (Microsoft Research, AdCenter) Academic (French Universities use OCaml) Financial (Investment Banks, Hedge Funds, etc) Problem domains Maths DSLs Scripting 6
Created in 2002 by Don Syme (Microsoft Research Cambridge) (Creator of.Net Generics & Cup holder) Influenced by OCaml (1996) ML (1973) Mission: First class functional programming for.Net 7
© Trayport PROPRIETARY & CONFIDENTIAL 8
RSS: Microsft F# Developer Center Forums: HubFs London F# User Group 9
© Trayport PROPRIETARY & CONFIDENTIAL 10
© Trayport PROPRIETARY & CONFIDENTIAL C#: int x; If(result) x = 1; else x = 0; F#: let x = if result then 1 else 0 C#: var x = result ? 1 : 0; 11
Programming in F# can be a bit like programming in C++ with const permanently turned on: 12 C++F# const int x = 10;let x = 10 mutable int x; void foo() const { x = 20; } let mutable x = 10; x <- 20
© Trayport PROPRIETARY & CONFIDENTIAL NameExample Single(1) Pair(1,2) Triple(1,2,null) Quadruple(1,2,3.0, null) Quintuple Sextuple Septuble Octuple Nonuple Decuple 13
F#: let me = (“Phil”, Male, 1.92, 1971) let name, sex, height, year = me C# (.Net 4.0): var me = new Tuple (“Phil”,1971) C++ (Boost ): Ticker ticker = make_tuple(“Barclays”, , ) double price = get (ticker); 14
let mul x y = x*y let tentimes = mul
let (|>) x f = f x Reduces scope of temporary variables 16 F# Code Sample // Shows name and first line of all cpp files modified today on drive C “*.cpp”, SearchOption.AllDirectories) |> Array.filter (fun name -> File.GetModifiedTime(name) > DateTime.Now.Date) |> Array.map (fun name -> name, using (File.OpenText (name)) (fun reader -> reader.ReadLine()) ) |> Array.iter (fun name, firstLine) -> Console.WriteLine (name) Console.WriteLine (firstLine))
if total > 21 then print "Bust" if total > 21: print "Bust” 17
© Trayport PROPRIETARY & CONFIDENTIAL type Sex = | Male | Female type BinaryTree = | Branch of BinaryTree * BinaryTree | Leaf of ‘a 18
Order of declaration of functions and modules Values may be redefined Obscure syntax, e.g. what is :?> 19
Fewer lines ≈ Fewer errors Const correctness ≈ More correctness Less state ≈ Less errors DSLs Asynchronous workflows Units of measure 20
21
22