f# and functional programming

21
unctional programming and Sanko.net meeting 8.6.2011

Upload: ramikarjalainen

Post on 10-May-2015

909 views

Category:

Technology


1 download

DESCRIPTION

Presentation was given june 2011 in sanko.net meeting

TRANSCRIPT

Page 1: F# and functional programming

Functional programming and F#

Sanko.net meeting 8.6.2011

Page 2: F# and functional programming

Agenda

• Functional programming from c# perspective, Linq principles

• Functional programming in general• F# and C# interoperability• F# examples: ’basic’ stuff• F# vs. C#

Page 3: F# and functional programming

Agenda

• Functional programming from c# perspective, Linq principles

• Functional programming in general• F# and C# interoperability• F# examples: ’basic’ stuff• F# vs. C#

There’s a bug in the Agenda!!!Wrong order and names missing!

Eh?

Page 4: F# and functional programming

This happens, when you Powerpoint without test harness!

Lets kick the PowerPoint and.... Correct the agenda using....

Page 5: F# and functional programming

F#?This happens, when you Powerpoint without test harness!

Lets kick the PowerPoint and.... Correct the agenda using....

Page 6: F# and functional programming

F#?This happens, when you Powerpoint without test harness!

Lets kick the PowerPoint and.... Correct the agenda using....

Java guy? Great!

Page 7: F# and functional programming

Demo....

Source available @ github: https://github.com/ramik/Demo/blob/master/FunctionalTutorial/CSharpExamples/PresisDemo/SlideDemo.fs

And if you don’t have internet access, snapshotted source just for youmodule SlideDemo

open System;

type PresentationKeeper(name: string, employer : string) = member lollis.name = name member fuulis.employer = employer override this.ToString() = name + ", " + employer

let 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)

Page 8: F# and functional programming

Why F#?

Page 9: F# and functional programming

Why F#?

Well, simply because its fun!

Page 10: F# and functional programming

Why F#?

Well, simply because its fun!(fun is reserved keyword, for expression)!

Page 11: F# and functional programming

F# has / is:

Functional

Hybrid (oo, imperative)

OCaml

.net official language (v4 )

Immutable by default

Succint

Very expressive

Strongly typed

Interoperable

Tuples

Pattern matching

Classes

Discrimated unions

Lambda expressions (anon func)

Type inference

Closures

asynchronous workflows

Generics

REPL (interactive)

Infinite lists etc. (laziness)

Tail recursion optimization

Page 12: F# and functional programming

F# has / is:

Functional

But for now, lets focus on this!

Page 13: F# and functional programming

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

Page 14: F# and functional programming

Immutability:

Object cannot be changed after creation

In C#, mark fields as readonly

No mutable state no local variables

Page 15: F# and functional programming

Functions as first class citizens:

Functions can be passed as normal parameters

Composition Higher order functions- Takes a function as paramand/or- Returns a function as output

Page 16: F# and functional programming

Functions as first class citizens:

Functions can be passed as normal parameters

Composition Higher order functions- Takes a function as paramand/or- Returns a function as output

Tuomas will cover the deeps of the function theory later in the session

Page 17: F# and functional programming

Recursion:

In functional programming, no iteration or ”looping”

Recursion is very important concept

Page 18: F# and functional programming

Recursion:

In functional programming, no iteration or ”looping”

Recursion is very important concept

- Recursion is very important, as without it ....

Page 19: F# and functional programming

Recursion:

In functional programming, no iteration or ”looping”

Recursion is very important concept

- Recursion is very important, as without it ....

Page 20: F# and functional programming

Recursion:

In functional programming, no iteration or ”looping”

Recursion is very important concept

- Recursion is very important, as without it ....

Tail recursion or stack says bye!

Page 21: F# and functional programming

Time to switch to Visual Studio!(for good this time)