F# Eye for the C# guy Leon Bambrick, secretGeek.net
F#... Do not be afraid.
hello
I’m F #
 
functional
Purely functional… Avoid  Side- Effects!
Purely functional… Avoid  Mutation!
Purely functional… No Variables!  Only Functions!
Purely functional… Same input -> Same output!
No Shared State Purely functional…
Why learn F#?
Do More With Less “ If I was using F# I’d be done by now” Why learn F#?
Do More With Less “ If I was using F# I’d be done by now” See where C# and VB.net are headed Why learn F#?
Do More With Less “ If I was using F# I’d be done by now” See where C# and VB.net are headed “ Learn one new language every year” (or two) Why learn F#?
Moore’s Law Keeps Going! Why learn F#?
But computers are not getting faster Why learn F#?
Data Grows Quickly  But # of Dimensions Grows much faster! And semi-structured data outgrowing structured Entropy Increasing Complexity is through the roof!
“ Software  gets slower faster than  hardware  gets faster” --Wirth’s Law
 
Some stuff is now cheap! Ram Disk  Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
Some stuff is now cheap! Ram Disk  Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
Some stuff is now cheap! Ram Disk  Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
Some stuff is now cheap! Ram Disk  Cores Some stuff remains expensive! Real Time Concurrency Locking Computer Economics is Changing
This  tips the balance  toward higher abstractions
Some Applications of F# “ Computationally intensive” work Map/Reduce over internets Financial Analysis Advertising Analysis SQL Data Mining XNA Games Development  Web tools, Compile F# to Javascript
Think About Game Programming
Game Programming Involves… 3D Animation Rendering Shading Simulation (e.g. physics) Collision Detection AI Opponents
Genealogy of F #  … Theorem proving and ISWIM
Genealogy of F #  … Theorem proving and ISWIM  begat: ML “Meta Language”
Genealogy of F #  … Theorem proving and ISWIM begat: ML “Meta Language”,  which begat: CAML
Genealogy of F #  … Theorem proving and ISWIM begat: ML “Meta Language”, which begat: CAML,  which in turn begat OCaml Oh!
Genealogy of F #  … Theorem proving and ISWIM begat: ML “Meta Language”, which begat: CAML, which in turn begat OCaml,  which in turn begat F# ... a sort of OCaml.net (plus more) Oh!
WTF # ? First official functional language on .net
WTF # ? First official functional language on .net Deep support thanks to Generics
WTF # ? First official functional language on .net Deep support thanks to Generics Assimilated by dev-div
WTF # ? First official functional language on .net Deep support thanks to Generics Assimilated by dev-div Will ship in VS.next
Code! Finally!
Code! //F# let a = 2
Code! //F# let a = 2 The ‘let’ keyword represents a binding between a value and an expression tree.
Code! //F# let a = 2 The ‘let’ blah blah blahblah blah blahblah blahblah blah blah blah blah blah blah.
Code! //F# let a = 2 //C# int a = 2 ≠
Code! //F# let a = 2 //C# //a function! static int a() { return 2; } More like
More Code! //F# #light open  System let  a = 2 Console.WriteLine a //C# using  System; namespace  ConsoleApplication1 { class   Program { static   int  a() { return  2; } static void  Main( string [] args) { Console .WriteLine(a);  } } }
More Code! //F# #light open  System let  a = 2 Console.WriteLine a More Noise  Than Signal!
More Code! //F# #light open  System let  a = 2 Console.WriteLine a Looks Weakly typed? Maybe Dynamic?
F#?
F#
F# Yet Expressive
F# Yet Versatile
More Code! //F# #light open  System let  a = 2 Console.WriteLine a Type Inference
let  a = 2 let  a = 3 error: FS0037: Duplicate definition of value 'a' Immutable by default
let  square x = x * x > val square : int -> int square 5 > val it : int = 25 simple function…
let  square x = x * x > val square : int -> int square 5 > val it : int = 25 simple function… Parameter
let  square x = x * x > val square : int -> int square 5 > val it : int = 25 simple function… “ Signature”
let  square x = x * x > val square : int -> int square 5 > val it : int = 25 simple function… Types are “inferred”
Discriminated union types type NullableInt = | Value of int | Nothing of unit
Discriminated union types type NullableInt = | Value of int | Nothing of unit Wish C#/“O-O” had this.
Discriminated union types type NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail
Discriminated union types type NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail e.g. “Inheritance” ends up being used in places where it’s not a perfect fit…
Discriminated union types type NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail e.g. “Inheritance” ends up being used in places where it’s not a perfect fit… ‘ Discriminated union types’ -- a better fit for some problems?
Another great tool is  “pattern matching”
Another great tool is   “pattern matching” “ Switch/Case” statements on steroids
Another great tool is   “pattern matching” “ Switch/Case” statements on steroids “ method overloads” on crack
Discriminated unions   example type Weapon = | Knife  | Gun  | Bomb
Pattern Matching type Weapon = | Knife  | Gun  | Bomb  //block any weapon! let block w = match w with | Knife  | Gun  -> disarm w | _ -> difuse w block Gun block Knife block Bomb
Pattern Matching type Weapon = | Knife  | Gun  | Bomb  //block any weapon let block w = match w with | Knife  | Gun  -> disarm w | _ -> difuse w block Gun block Knife block Bomb
Lazy is a virtue let  lazy_square x =  lazy  ( print_endline “thinking...“ x * x ) let  lazy_square_ten = lazy_square 10 //first time: “thinking…” Lazy.force  (lazy_square_ten) //second time: no thinking, just result Lazy.force  (lazy_square_ten)
Lazy is a virtue let  lazy_square x =  lazy  ( print_endline “thinking...“ x * x ) let  lazy_square_ten = lazy_square 10 //first time: “thinking…” Lazy.force  (lazy_square_ten) //second time: no thinking, just result Lazy.force  (lazy_square_ten)
Useful libraries Neat manual Awesome Samples
 
 
“ Empty” source file… 5 pages of help!
Make sure F# Interactive is running!
F# Interactive: It’s the bomb!
F# Interactive:
 
msdn.microsoft.com/fsharp/
8 Ways to Learn http://cs.hubfs.net Codeplex Fsharp Samples Books ML
Acknowledgements Cartman Einstein Dilbert Alan Turing Alonzo Church Godzilla Gears of war John Hughes,  Why Functional Programming Matters,   http://www.math.chalmers.se/~rjmh/Papers/whyfp.html Robert Pickering,  Foundations of F#,  http://www.apress.com/book/view/1590597575 Slava Akhmechet,  Functional Programming For The Rest of Us,  http://www.defmacro.org/ramblings/fp.html Steve Yegge,  Execution In the Kingdom of Nouns,  http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html P. J. Landin,  The Next 700 Programming Languages   http://www.cs.utah.edu/~wilson/compilers/old/papers/p157-landin.pdf Tim Sweeney,  The Next Mainstream Programming Language ,  http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf Tomas Petricek,  F# Web Tools, Ajax Made Simple ,  http://www.codeplex.com/fswebtools Don Syme,  http://blogs.msdn.com/dsyme

F# Eye for the C# Guy

  • 1.
    F# Eye forthe C# guy Leon Bambrick, secretGeek.net
  • 2.
    F#... Do notbe afraid.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
    Purely functional… NoVariables! Only Functions!
  • 10.
    Purely functional… Sameinput -> Same output!
  • 11.
    No Shared StatePurely functional…
  • 12.
  • 13.
    Do More WithLess “ If I was using F# I’d be done by now” Why learn F#?
  • 14.
    Do More WithLess “ If I was using F# I’d be done by now” See where C# and VB.net are headed Why learn F#?
  • 15.
    Do More WithLess “ If I was using F# I’d be done by now” See where C# and VB.net are headed “ Learn one new language every year” (or two) Why learn F#?
  • 16.
    Moore’s Law KeepsGoing! Why learn F#?
  • 17.
    But computers arenot getting faster Why learn F#?
  • 18.
    Data Grows Quickly But # of Dimensions Grows much faster! And semi-structured data outgrowing structured Entropy Increasing Complexity is through the roof!
  • 19.
    “ Software gets slower faster than hardware gets faster” --Wirth’s Law
  • 20.
  • 21.
    Some stuff isnow cheap! Ram Disk Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
  • 22.
    Some stuff isnow cheap! Ram Disk Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
  • 23.
    Some stuff isnow cheap! Ram Disk Cores Some stuff remains expensive! Time Concurrency Locking Computer Economics is Changing
  • 24.
    Some stuff isnow cheap! Ram Disk Cores Some stuff remains expensive! Real Time Concurrency Locking Computer Economics is Changing
  • 25.
    This tipsthe balance toward higher abstractions
  • 26.
    Some Applications ofF# “ Computationally intensive” work Map/Reduce over internets Financial Analysis Advertising Analysis SQL Data Mining XNA Games Development Web tools, Compile F# to Javascript
  • 27.
    Think About GameProgramming
  • 28.
    Game Programming Involves…3D Animation Rendering Shading Simulation (e.g. physics) Collision Detection AI Opponents
  • 29.
    Genealogy of F# … Theorem proving and ISWIM
  • 30.
    Genealogy of F# … Theorem proving and ISWIM begat: ML “Meta Language”
  • 31.
    Genealogy of F# … Theorem proving and ISWIM begat: ML “Meta Language”, which begat: CAML
  • 32.
    Genealogy of F# … Theorem proving and ISWIM begat: ML “Meta Language”, which begat: CAML, which in turn begat OCaml Oh!
  • 33.
    Genealogy of F# … Theorem proving and ISWIM begat: ML “Meta Language”, which begat: CAML, which in turn begat OCaml, which in turn begat F# ... a sort of OCaml.net (plus more) Oh!
  • 34.
    WTF # ?First official functional language on .net
  • 35.
    WTF # ?First official functional language on .net Deep support thanks to Generics
  • 36.
    WTF # ?First official functional language on .net Deep support thanks to Generics Assimilated by dev-div
  • 37.
    WTF # ?First official functional language on .net Deep support thanks to Generics Assimilated by dev-div Will ship in VS.next
  • 38.
  • 39.
  • 40.
    Code! //F# leta = 2 The ‘let’ keyword represents a binding between a value and an expression tree.
  • 41.
    Code! //F# leta = 2 The ‘let’ blah blah blahblah blah blahblah blahblah blah blah blah blah blah blah.
  • 42.
    Code! //F# leta = 2 //C# int a = 2 ≠
  • 43.
    Code! //F# leta = 2 //C# //a function! static int a() { return 2; } More like
  • 44.
    More Code! //F##light open System let a = 2 Console.WriteLine a //C# using System; namespace ConsoleApplication1 { class Program { static int a() { return 2; } static void Main( string [] args) { Console .WriteLine(a); } } }
  • 45.
    More Code! //F##light open System let a = 2 Console.WriteLine a More Noise Than Signal!
  • 46.
    More Code! //F##light open System let a = 2 Console.WriteLine a Looks Weakly typed? Maybe Dynamic?
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
    More Code! //F##light open System let a = 2 Console.WriteLine a Type Inference
  • 52.
    let a= 2 let a = 3 error: FS0037: Duplicate definition of value 'a' Immutable by default
  • 53.
    let squarex = x * x > val square : int -> int square 5 > val it : int = 25 simple function…
  • 54.
    let squarex = x * x > val square : int -> int square 5 > val it : int = 25 simple function… Parameter
  • 55.
    let squarex = x * x > val square : int -> int square 5 > val it : int = 25 simple function… “ Signature”
  • 56.
    let squarex = x * x > val square : int -> int square 5 > val it : int = 25 simple function… Types are “inferred”
  • 57.
    Discriminated union typestype NullableInt = | Value of int | Nothing of unit
  • 58.
    Discriminated union typestype NullableInt = | Value of int | Nothing of unit Wish C#/“O-O” had this.
  • 59.
    Discriminated union typestype NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail
  • 60.
    Discriminated union typestype NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail e.g. “Inheritance” ends up being used in places where it’s not a perfect fit…
  • 61.
    Discriminated union typestype NullableInt = | Value of int | Nothing of unit “ O-O” has discriminated union envy When the only tool you have is a hammer Everything looks like a nail e.g. “Inheritance” ends up being used in places where it’s not a perfect fit… ‘ Discriminated union types’ -- a better fit for some problems?
  • 62.
    Another great toolis “pattern matching”
  • 63.
    Another great toolis “pattern matching” “ Switch/Case” statements on steroids
  • 64.
    Another great toolis “pattern matching” “ Switch/Case” statements on steroids “ method overloads” on crack
  • 65.
    Discriminated unions example type Weapon = | Knife | Gun | Bomb
  • 66.
    Pattern Matching typeWeapon = | Knife | Gun | Bomb //block any weapon! let block w = match w with | Knife | Gun -> disarm w | _ -> difuse w block Gun block Knife block Bomb
  • 67.
    Pattern Matching typeWeapon = | Knife | Gun | Bomb //block any weapon let block w = match w with | Knife | Gun -> disarm w | _ -> difuse w block Gun block Knife block Bomb
  • 68.
    Lazy is avirtue let lazy_square x = lazy ( print_endline “thinking...“ x * x ) let lazy_square_ten = lazy_square 10 //first time: “thinking…” Lazy.force (lazy_square_ten) //second time: no thinking, just result Lazy.force (lazy_square_ten)
  • 69.
    Lazy is avirtue let lazy_square x = lazy ( print_endline “thinking...“ x * x ) let lazy_square_ten = lazy_square 10 //first time: “thinking…” Lazy.force (lazy_square_ten) //second time: no thinking, just result Lazy.force (lazy_square_ten)
  • 70.
    Useful libraries Neatmanual Awesome Samples
  • 71.
  • 72.
  • 73.
    “ Empty” sourcefile… 5 pages of help!
  • 74.
    Make sure F#Interactive is running!
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
    8 Ways toLearn http://cs.hubfs.net Codeplex Fsharp Samples Books ML
  • 80.
    Acknowledgements Cartman EinsteinDilbert Alan Turing Alonzo Church Godzilla Gears of war John Hughes, Why Functional Programming Matters, http://www.math.chalmers.se/~rjmh/Papers/whyfp.html Robert Pickering, Foundations of F#, http://www.apress.com/book/view/1590597575 Slava Akhmechet, Functional Programming For The Rest of Us, http://www.defmacro.org/ramblings/fp.html Steve Yegge, Execution In the Kingdom of Nouns, http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html P. J. Landin, The Next 700 Programming Languages http://www.cs.utah.edu/~wilson/compilers/old/papers/p157-landin.pdf Tim Sweeney, The Next Mainstream Programming Language , http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf Tomas Petricek, F# Web Tools, Ajax Made Simple , http://www.codeplex.com/fswebtools Don Syme, http://blogs.msdn.com/dsyme

Editor's Notes

  • #2 What’s this F# thing anyway?