F# Eye for the C# guy Leon Bambrick, secretGeek.net
F#... Do not be afraid.
hello
I’m F #
 
functional
Purely functional… <ul><li>Avoid  </li></ul><ul><li>Side- </li></ul><ul><li>Effects! </li></ul>
Purely functional… <ul><li>Avoid  </li></ul><ul><li>Mutation! </li></ul>
Purely functional… <ul><li>No Variables!  </li></ul><ul><li>Only Functions! </li></ul>
Purely functional… <ul><li>Same input -> </li></ul><ul><li>Same output! </li></ul>
<ul><li>No Shared State </li></ul>Purely functional…
<ul><li>Why learn F#? </li></ul>
<ul><li>Do More With Less </li></ul><ul><ul><li>“ If I was using F# I’d be done by now” </li></ul></ul>Why learn F#?
<ul><li>Do More With Less </li></ul><ul><ul><li>“ If I was using F# I’d be done by now” </li></ul></ul><ul><li>See where C...
<ul><li>Do More With Less </li></ul><ul><ul><li>“ If I was using F# I’d be done by now” </li></ul></ul><ul><li>See where C...
<ul><li>Moore’s Law Keeps Going! </li></ul>Why learn F#?
<ul><li>But computers are not getting faster </li></ul>Why learn F#?
<ul><li>Data Grows Quickly  </li></ul><ul><li>But # of Dimensions Grows much faster! </li></ul><ul><li>And semi-structured...
<ul><li>“ Software  gets slower faster than  hardware  gets faster” </li></ul><ul><li>--Wirth’s Law </li></ul>
 
<ul><li>Some stuff is now cheap! </li></ul><ul><ul><li>Ram </li></ul></ul><ul><ul><li>Disk  </li></ul></ul><ul><ul><li>Cor...
<ul><li>Some stuff is now cheap! </li></ul><ul><ul><li>Ram </li></ul></ul><ul><ul><li>Disk  </li></ul></ul><ul><ul><li>Cor...
<ul><li>Some stuff is now cheap! </li></ul><ul><ul><li>Ram </li></ul></ul><ul><ul><li>Disk  </li></ul></ul><ul><ul><li>Cor...
<ul><li>Some stuff is now cheap! </li></ul><ul><ul><li>Ram </li></ul></ul><ul><ul><li>Disk  </li></ul></ul><ul><ul><li>Cor...
This  tips the balance  toward higher abstractions
Some Applications of F# <ul><li>“ Computationally intensive” work </li></ul><ul><li>Map/Reduce over internets </li></ul><u...
Think About Game Programming
Game Programming Involves… <ul><li>3D Animation </li></ul><ul><li>Rendering </li></ul><ul><li>Shading </li></ul><ul><li>Si...
Genealogy of F #  … <ul><li>Theorem proving and ISWIM </li></ul>
Genealogy of F #  … <ul><li>Theorem proving and ISWIM  begat: </li></ul><ul><ul><li>ML “Meta Language” </li></ul></ul>
Genealogy of F #  … <ul><li>Theorem proving and ISWIM begat: </li></ul><ul><ul><li>ML “Meta Language”,  which begat: </li>...
Genealogy of F #  … <ul><li>Theorem proving and ISWIM begat: </li></ul><ul><ul><li>ML “Meta Language”, which begat: </li><...
Genealogy of F #  … <ul><li>Theorem proving and ISWIM begat: </li></ul><ul><ul><li>ML “Meta Language”, which begat: </li><...
WTF # ? <ul><li>First official functional language on .net </li></ul>
WTF # ? <ul><li>First official functional language on .net </li></ul><ul><li>Deep support thanks to Generics </li></ul>
WTF # ? <ul><li>First official functional language on .net </li></ul><ul><li>Deep support thanks to Generics </li></ul><ul...
WTF # ? <ul><li>First official functional language on .net </li></ul><ul><li>Deep support thanks to Generics </li></ul><ul...
Code! <ul><li>Finally! </li></ul>
Code! <ul><li>//F# </li></ul><ul><li>let a = 2 </li></ul>
Code! <ul><li>//F# </li></ul><ul><li>let a = 2 </li></ul>The ‘let’ keyword represents a binding between a value and an exp...
Code! <ul><li>//F# </li></ul><ul><li>let a = 2 </li></ul>The ‘let’ blah blah blahblah blah blahblah blahblah blah blah bla...
Code! <ul><li>//F# </li></ul><ul><li>let a = 2 </li></ul>//C# int a = 2 ≠
Code! <ul><li>//F# </li></ul><ul><li>let a = 2 </li></ul>//C# //a function! static int a() { return 2; } More like
More Code! <ul><li>//F# </li></ul><ul><li>#light </li></ul><ul><li>open  System </li></ul><ul><li>let  a = 2 </li></ul><ul...
More Code! <ul><li>//F# </li></ul><ul><li>#light </li></ul><ul><li>open  System </li></ul><ul><li>let  a = 2 </li></ul><ul...
More Code! <ul><li>//F# </li></ul><ul><li>#light </li></ul><ul><li>open  System </li></ul><ul><li>let  a = 2 </li></ul><ul...
F#?
F#
F# Yet Expressive
F# Yet Versatile
More Code! <ul><li>//F# </li></ul><ul><li>#light </li></ul><ul><li>open  System </li></ul><ul><li>let  a = 2 </li></ul><ul...
<ul><li>let  a = 2 </li></ul><ul><li>let  a = 3 </li></ul><ul><li>error: FS0037: Duplicate definition of value 'a' </li></...
<ul><li>let  square x = x * x </li></ul><ul><li>> val square : int -> int </li></ul><ul><li>square 5 </li></ul><ul><li>> v...
<ul><li>let  square x = x * x </li></ul><ul><li>> val square : int -> int </li></ul><ul><li>square 5 </li></ul><ul><li>> v...
<ul><li>let  square x = x * x </li></ul><ul><li>> val square : int -> int </li></ul><ul><li>square 5 </li></ul><ul><li>> v...
<ul><li>let  square x = x * x </li></ul><ul><li>> val square : int -> int </li></ul><ul><li>square 5 </li></ul><ul><li>> v...
Discriminated union types <ul><li>type NullableInt = </li></ul><ul><li>| Value of int </li></ul><ul><li>| Nothing of unit ...
Discriminated union types <ul><li>type NullableInt = </li></ul><ul><li>| Value of int </li></ul><ul><li>| Nothing of unit ...
Discriminated union types <ul><li>type NullableInt = </li></ul><ul><li>| Value of int </li></ul><ul><li>| Nothing of unit ...
Discriminated union types <ul><li>type NullableInt = </li></ul><ul><li>| Value of int </li></ul><ul><li>| Nothing of unit ...
Discriminated union types <ul><li>type NullableInt = </li></ul><ul><li>| Value of int </li></ul><ul><li>| Nothing of unit ...
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 <ul><li>type Weapon = </li></ul><ul><li>| Knife  </li></ul><ul><li>| Gun  </li></ul><ul><li...
Pattern Matching <ul><li>type Weapon = </li></ul><ul><li>| Knife  </li></ul><ul><li>| Gun  </li></ul><ul><li>| Bomb  </li>...
Pattern Matching <ul><li>type Weapon = </li></ul><ul><li>| Knife  </li></ul><ul><li>| Gun  </li></ul><ul><li>| Bomb  </li>...
Lazy is a virtue <ul><li>let  lazy_square x =  </li></ul><ul><li>lazy  ( print_endline “thinking...“ </li></ul><ul><li>x *...
Lazy is a virtue <ul><li>let  lazy_square x =  </li></ul><ul><li>lazy  ( print_endline “thinking...“ </li></ul><ul><li>x *...
Useful libraries Neat manual Awesome Samples
 
 
“ Empty” source file… 5 pages of help!
Make sure F# Interactive is running!
F# Interactive: <ul><li>It’s the bomb! </li></ul>
F# Interactive:
 
<ul><li>msdn.microsoft.com/fsharp/ </li></ul>
8 Ways to Learn <ul><li>http://cs.hubfs.net </li></ul><ul><li>Codeplex Fsharp Samples </li></ul><ul><li>Books </li></ul><u...
Acknowledgements <ul><li>Cartman </li></ul><ul><li>Einstein </li></ul><ul><li>Dilbert </li></ul><ul><li>Alan Turing </li><...
Upcoming SlideShare
Loading in …5
×

F# Eye for the C# Guy

16,386 views

Published on

F# Eye for the C# Guy by Leon Bambrick

Published in: Technology, Health & Medicine

F# Eye for the C# Guy

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

×