FUNCTIONAL
PROGRAMMING IN F#

      An introduction




  Functional Programming Day
          Jonas Follesø
           17/12/2012
ABOUT ME




           Jonas Follesø
           Scientist & Manager BEKK
           Trondheim
           github.com/follesoe
           twitter.com/follesoe
           jonas.folesoe.no
AGENDA


         Part 1 – F# & it’s history
         Part 2 – F# Fundamentals
         Part 3 – Units of Measure
         Part 4 – Type Providers
         Part 5 – Async Workflows
         Part 6 – Summary & Resources
2006:
   IT2105 -
 Functional
Programming
Scheme
Func<int, bool> isEven = n => n%2 == 0;
Action<int> print = n => Console.WriteLine("The number is {0}", n);

var numbers = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

numbers.Where(isEven)
       .ToList()
       .ForEach(print);


                                                               C#
Disclaimer:
F# beginner
Part 1
What is F#?
WHAT IS F#?




  … a statically typed, functional first, hybrid, open source,
              programming language for the CLR…
IN MICROSOFT OWN WORDS…




    … a productive, supported, interoperable, functional
   language that allows you to write simple code to solve
                    complex problems.
QUICK SHOW OF HANDS…




               JVM?    CLR?
IT HAS BEEN SAID…




          …F# is to C# what Scala is to Java...
HISTORY

                   • Designed and implemented by Don Syme at
                     Microsoft Research



                   • Wanted a functional programming language
                     on the CLR for research and scientific
                     computing


Photo: John Lamb
F# LANGUAGE INFLUENCES




        OCaml
                         F#     C#/.NET


 Similar core language        Similar object model
HISTORY




 Pre 2005      2005      2010                      2012
   F# 0.9       F# 1.0   F# 2.0                   F# 3.0
  Microsoft    VS2005    VS2010                   VS2012
  Research    Download   Built-in                 Built-in


                                 04. November 2010
                               F# compiler and libraries
                          released under Apache 2.0 license
Demo: F#
Fundamentals
Part 2:
Units of
Measure
MARS CLIMATE ORBITER
MARS CLIMATE ORBITER




The 'root cause' of the loss of the spacecraft was the failed
translation of English units into metric units in a segment of
ground-based, navigation-related mission software


http://mars.jpl.nasa.gov/msp98/news/mco991110.html
Demo:
Units of
Measure
Part 3
  Type
Providers
THE CHALLENGE




1. The world is information rich

2. Our programming languages are information sparse
IMPEDANCE MISS MATCH

• Rich information spaces with structured data



                                                       Open APIs
           Web
                            OData          Databases    and data
          Services
                                                        markeds




          XML             Semantic
                                           CRM data    Others…
        Documents           Web
IMPEDANCE MISS MATCH

• Need bridging mechanisms to access structured data in our programs



  • Hand-written static libraries

  • Generated static libraries

  • Dynamically-typed information representation
F# 3.0 TYPE PROVIDERS

A Type Provider is…
                                                  Statement
                                               completion for data
A design-time component that provides a
computed space of types and methods
                                                 Extensible and
                                                     open

A compiler/IDE extension
                                                 Breaking down
                                                     walls!
The static counterpart to dynamic languages:
Compile-time meta programming
Demo:
  Type
Providers
Part 5
 Async
Workflows
Part 6
Summary &
 Resources
Why?
POSSIBLE REASONS – MY OWN SPECULATIONS




             C# is too                    Microsoft
              good?                      positioning?
IS C# TOO GOOD?



• Implicitly Typed Local Variables

• Extension Methods

• Lambda Expressions and Higher Order Functions

• LINQ

• Task Parallel Library

• Extension Methods

• New asynchronous language constructs (async & await)
MICROSOFT POSITIONING


                                                C#, VB,
        F# 3.0                 F# 2.0          HTML5, JS



                        Transformation,
     Access
                           Analysis,      Presentation,
      Data,
                          Algorithms,     Publication,
  Information,
                             Code,         Experience
    Services
                            Parallel
Learning
Resources
HTTP://WWW.TRYFSHARP.ORG/
HTTP://FSHARPFORFUNANDPROFIT.COM/
FILE – NEW – F# TUTORIAL
BOOKS




Programming F# 3.0   Expert F# 3.0
by Chris Smith       by Don Syme, Adam Granicz
                     and Antonio Cisternino
THANK YOU!

     Jonas Follesø
http://jonas.follesoe.no
       @follesoe

Introduction to F#

  • 1.
    FUNCTIONAL PROGRAMMING IN F# An introduction Functional Programming Day Jonas Follesø 17/12/2012
  • 2.
    ABOUT ME Jonas Follesø Scientist & Manager BEKK Trondheim github.com/follesoe twitter.com/follesoe jonas.folesoe.no
  • 3.
    AGENDA Part 1 – F# & it’s history Part 2 – F# Fundamentals Part 3 – Units of Measure Part 4 – Type Providers Part 5 – Async Workflows Part 6 – Summary & Resources
  • 4.
    2006: IT2105 - Functional Programming
  • 5.
  • 7.
    Func<int, bool> isEven= n => n%2 == 0; Action<int> print = n => Console.WriteLine("The number is {0}", n); var numbers = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; numbers.Where(isEven) .ToList() .ForEach(print); C#
  • 8.
  • 9.
  • 10.
    WHAT IS F#? … a statically typed, functional first, hybrid, open source, programming language for the CLR…
  • 11.
    IN MICROSOFT OWNWORDS… … a productive, supported, interoperable, functional language that allows you to write simple code to solve complex problems.
  • 12.
    QUICK SHOW OFHANDS… JVM? CLR?
  • 13.
    IT HAS BEENSAID… …F# is to C# what Scala is to Java...
  • 14.
    HISTORY • Designed and implemented by Don Syme at Microsoft Research • Wanted a functional programming language on the CLR for research and scientific computing Photo: John Lamb
  • 15.
    F# LANGUAGE INFLUENCES OCaml F# C#/.NET Similar core language Similar object model
  • 16.
    HISTORY Pre 2005 2005 2010 2012 F# 0.9 F# 1.0 F# 2.0 F# 3.0 Microsoft VS2005 VS2010 VS2012 Research Download Built-in Built-in 04. November 2010 F# compiler and libraries released under Apache 2.0 license
  • 20.
  • 21.
  • 22.
  • 23.
    MARS CLIMATE ORBITER The'root cause' of the loss of the spacecraft was the failed translation of English units into metric units in a segment of ground-based, navigation-related mission software http://mars.jpl.nasa.gov/msp98/news/mco991110.html
  • 24.
  • 25.
    Part 3 Type Providers
  • 26.
    THE CHALLENGE 1. Theworld is information rich 2. Our programming languages are information sparse
  • 27.
    IMPEDANCE MISS MATCH •Rich information spaces with structured data Open APIs Web OData Databases and data Services markeds XML Semantic CRM data Others… Documents Web
  • 28.
    IMPEDANCE MISS MATCH •Need bridging mechanisms to access structured data in our programs • Hand-written static libraries • Generated static libraries • Dynamically-typed information representation
  • 29.
    F# 3.0 TYPEPROVIDERS A Type Provider is… Statement completion for data A design-time component that provides a computed space of types and methods Extensible and open A compiler/IDE extension Breaking down walls! The static counterpart to dynamic languages: Compile-time meta programming
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
    POSSIBLE REASONS –MY OWN SPECULATIONS C# is too Microsoft good? positioning?
  • 35.
    IS C# TOOGOOD? • Implicitly Typed Local Variables • Extension Methods • Lambda Expressions and Higher Order Functions • LINQ • Task Parallel Library • Extension Methods • New asynchronous language constructs (async & await)
  • 36.
    MICROSOFT POSITIONING C#, VB, F# 3.0 F# 2.0 HTML5, JS Transformation, Access Analysis, Presentation, Data, Algorithms, Publication, Information, Code, Experience Services Parallel
  • 37.
  • 38.
  • 39.
  • 40.
    FILE – NEW– F# TUTORIAL
  • 41.
    BOOKS Programming F# 3.0 Expert F# 3.0 by Chris Smith by Don Syme, Adam Granicz and Antonio Cisternino
  • 42.
    THANK YOU! Jonas Follesø http://jonas.follesoe.no @follesoe

Editor's Notes

  • #10 1973, Robin Milner at the University of Edinburgh developed the ML programming language to develop proof tactics for the LCF Theorem prover. Lisp continued to be used for years as the favored language of AI researchers.ML stands out among other functional programming languages; its polymorphic functions made it a very expressive language, while its strong typing and immutable data structures made it possible to compile ML into very efficient machine code.L&apos;s relative success spawned an entire family of ML-derived languages, including Standard ML, Caml, and its most famous dialect called OCaml which unifies functional programming with object-oriented and imperative styles.F# is essentially a .Net implementation of OCaml, combining the power and expressive syntax of functional programming with the tens of thousands of classes which make up the .NET class library.
  • #14 Mars Climate Orbiter ble skutt opp 11. desember 1998. Oppdraget var å studere været, klimaet, og vann- og karbondioksidinnhold på planeten Mars. Den 23. september 1999 skulle farkosten gå inn i bane rundt Mars, men en beregningsfeil resulterte i at Mars Climate Orbiter gikk inn 90 km for lavt. Dette førte til for store påkjenninger og friksjon fra Mars atmosfære, og fartøyet gikk i oppløsning.
  • #15 NASAs undersøkelser etter ulykken avdekket at feilen skyltes en software-feil, hvor to delsystemer brukte forskjellig sett med måleenheter. Det ene systemet brukte imperiske enheter (pound-seconds, lbf*s) istedenfor metriske enheter (newton-seconds, N·s) som spesifisert av NASA.
  • #21 Mars 2012: Microsoft&apos;s F# continues to evolve, with the recent release of F# 3.0 beta. F# is excellent at concisely expressing business and domain logic. Developers trying to achieve explicit business logic within an application may opt to express their domain in F# with the majority of plumbing code in C#.
  • #23 Takk for oss.Ha en heltfantastiskfagdag!