Download presentation
Presentation is loading. Please wait.
1
Phillip Trelford Twitter: @ptrelford Blog: http://www.trelford.com/blog 1
2
Goals Introduce F# Non-goals Provide in-depth understanding Mass conversion to functional programming cult Sell books 2
3
1. Background 2. Language Syntax 3. Code Samples 3
4
F# is multi-paradigm language, that is: Functional Declarative Object Orientated Imperative.Net language with VS integration (A bit like Python) 4
5
© Trayport 2007 - PROPRIETARY & CONFIDENTIAL Key Features Functions are idempotent Functions are first class High order functions Languages: Haskell Excel JavaScript 5
6
Communities Scientific (Microsoft Research, AdCenter) Academic (French Universities use OCaml) Financial (Investment Banks, Hedge Funds, etc) Problem domains Maths DSLs Scripting 6
7
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
8
© Trayport 2007 - PROPRIETARY & CONFIDENTIAL 8
9
RSS: Microsft F# Developer Center Forums: HubFs London F# User Group 9
10
© Trayport 2007 - PROPRIETARY & CONFIDENTIAL 10
11
© Trayport 2007 - 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
12
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
13
© Trayport 2007 - 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
14
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”, 244.75, -40.25) double price = get (ticker); 14
15
let mul x y = x*y let tentimes = mul 10.0 15
16
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 Directory.GetFile(@“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))
17
if total > 21 then print "Bust" if total > 21: print "Bust” 17
18
© Trayport 2007 - PROPRIETARY & CONFIDENTIAL type Sex = | Male | Female type BinaryTree = | Branch of BinaryTree * BinaryTree | Leaf of ‘a 18
19
Order of declaration of functions and modules Values may be redefined Obscure syntax, e.g. what is :?> 19
20
Fewer lines ≈ Fewer errors Const correctness ≈ More correctness Less state ≈ Less errors DSLs Asynchronous workflows Units of measure 20
21
21
22
22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.