KEMBAR78
F# and functional programming | PPTX
Functional programming and F#Sanko.net meeting 8.6.2011
AgendaFunctional programming from c# perspective, Linq principlesFunctional programming in generalF# and C# interoperabilityF# examples: ’basic’ stuffF# vs. C#
AgendaFunctional programming from c# perspective, Linq principlesFunctional programming in generalF# and C# interoperabilityF# examples: ’basic’ stuffF# vs. C# Eh?There’s a bug in the Agenda!!!Wrong order and names missing!
This happens, when you Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....
F#?This happens, when you Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....
F#?This happens, when you Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....Java guy? Great!
Demo.... Source available @ github: https://github.com/ramik/Demo/blob/master/FunctionalTutorial/CSharpExamples/PresisDemo/SlideDemo.fsAnd if you don’t have internet access, snapshotted source just for youmodule SlideDemoopen System;type PresentationKeeper(name: string, employer : string) =         member lollis.name = name        member fuulis.employer = employer        override this.ToString() = name + ", " + employerlet Rami = PresentationKeeper("Rami Karjalainen", "Reaktor")let Tuomas = PresentationKeeper("Tuomas Hietanen", "Basware")let topics = [ (2, "Functional programming from c# perspective, Linq principles", Tuomas);               (1, "Functional programming in general", Rami);               (3, "F# examples: ’basic’ stuff", Rami);               (4, "F# and C# interoperability", Rami);               (5, "F# vs. C#", Tuomas) ]let formatTopic topic keeper = String.Format ("{0} ({1})", topic, keeper.ToString())let pimpTheList = topics |> Seq.sortBy (fun (prio,_,_) -> prio)                          |> Seq.map (fun (_, a, b) -> a,b)                         |> Seq.map (fun x -> (formatTopic (fst x) (snd x)))let printPimped x = x |> Seq.iter (fun c -> printfn "%s" c)
Why F#?
Why F#?Well, simply because its fun!
Why F#?Well, simply because its fun!(fun is reserved keyword, for      expression)!
F# has / is:SuccintClosuresFunctionalGenericsDiscrimated unions.net official language (v4  )Pattern matchingTail recursion optimization  TuplesImmutable by defaultasynchronous workflowsVery expressiveInteroperableType inferenceClassesInfinite lists etc. (laziness) Strongly typedHybrid (oo, imperative)Lambda expressions (anon func)REPL (interactive)OCaml
F# has / is:But for now, lets focus on this!Functional
Well, what is functional programming?Immutable constructs
Functions as first class ”citizens”
Recursion
No mutable state Many other concepts apply, but these are ones to focus in this presentation
Immutability:Object cannot be changed after creationIn C#, mark fields as readonlyNo mutable state  no local variables
Functions as first class citizens:Functions can be passed as normal parameters CompositionHigher order functionsTakes a function as paramand/orReturns a function as outputFunctions as first class citizens:Functions can be passed as normal parameters CompositionHigher order functionsTakes a function as paramand/orReturns a function as outputTuomas will cover the deeps of the function theory later in the session
Recursion:In functional programming, no iteration or ”looping”Recursion is very important concept
Recursion:In functional programming, no iteration or ”looping”Recursion is very important conceptRecursion is very important, as without it ....
Recursion:In functional programming, no iteration or ”looping”Recursion is very important conceptRecursion is very important, as without it ....

F# and functional programming

  • 1.
    Functional programming andF#Sanko.net meeting 8.6.2011
  • 2.
    AgendaFunctional programming fromc# perspective, Linq principlesFunctional programming in generalF# and C# interoperabilityF# examples: ’basic’ stuffF# vs. C#
  • 3.
    AgendaFunctional programming fromc# perspective, Linq principlesFunctional programming in generalF# and C# interoperabilityF# examples: ’basic’ stuffF# vs. C# Eh?There’s a bug in the Agenda!!!Wrong order and names missing!
  • 4.
    This happens, whenyou Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....
  • 5.
    F#?This happens, whenyou Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....
  • 6.
    F#?This happens, whenyou Powerpoint without test harness! Lets kick the PowerPoint and.... Correct the agenda using....Java guy? Great!
  • 7.
    Demo.... Source available@ github: https://github.com/ramik/Demo/blob/master/FunctionalTutorial/CSharpExamples/PresisDemo/SlideDemo.fsAnd if you don’t have internet access, snapshotted source just for youmodule SlideDemoopen System;type PresentationKeeper(name: string, employer : string) = member lollis.name = name member fuulis.employer = employer override this.ToString() = name + ", " + employerlet Rami = PresentationKeeper("Rami Karjalainen", "Reaktor")let Tuomas = PresentationKeeper("Tuomas Hietanen", "Basware")let topics = [ (2, "Functional programming from c# perspective, Linq principles", Tuomas); (1, "Functional programming in general", Rami); (3, "F# examples: ’basic’ stuff", Rami); (4, "F# and C# interoperability", Rami); (5, "F# vs. C#", Tuomas) ]let formatTopic topic keeper = String.Format ("{0} ({1})", topic, keeper.ToString())let pimpTheList = topics |> Seq.sortBy (fun (prio,_,_) -> prio) |> Seq.map (fun (_, a, b) -> a,b) |> Seq.map (fun x -> (formatTopic (fst x) (snd x)))let printPimped x = x |> Seq.iter (fun c -> printfn "%s" c)
  • 8.
  • 9.
    Why F#?Well, simplybecause its fun!
  • 10.
    Why F#?Well, simplybecause its fun!(fun is reserved keyword, for expression)!
  • 11.
    F# has /is:SuccintClosuresFunctionalGenericsDiscrimated unions.net official language (v4  )Pattern matchingTail recursion optimization TuplesImmutable by defaultasynchronous workflowsVery expressiveInteroperableType inferenceClassesInfinite lists etc. (laziness) Strongly typedHybrid (oo, imperative)Lambda expressions (anon func)REPL (interactive)OCaml
  • 12.
    F# has /is:But for now, lets focus on this!Functional
  • 13.
    Well, what isfunctional programming?Immutable constructs
  • 14.
    Functions as firstclass ”citizens”
  • 15.
  • 16.
    No mutable stateMany other concepts apply, but these are ones to focus in this presentation
  • 17.
    Immutability:Object cannot bechanged after creationIn C#, mark fields as readonlyNo mutable state  no local variables
  • 18.
    Functions as firstclass citizens:Functions can be passed as normal parameters CompositionHigher order functionsTakes a function as paramand/orReturns a function as outputFunctions as first class citizens:Functions can be passed as normal parameters CompositionHigher order functionsTakes a function as paramand/orReturns a function as outputTuomas will cover the deeps of the function theory later in the session
  • 19.
    Recursion:In functional programming,no iteration or ”looping”Recursion is very important concept
  • 20.
    Recursion:In functional programming,no iteration or ”looping”Recursion is very important conceptRecursion is very important, as without it ....
  • 21.
    Recursion:In functional programming,no iteration or ”looping”Recursion is very important conceptRecursion is very important, as without it ....
  • 22.
    Recursion:In functional programming,no iteration or ”looping”Recursion is very important conceptRecursion is very important, as without it ....Tail recursion or stack says bye!
  • 23.
    Time to switchto Visual Studio!(for good this time)