f# and functional programming

Post on 10-May-2015

909 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation was given june 2011 in sanko.net meeting

TRANSCRIPT

Functional programming and F#

Sanko.net meeting 8.6.2011

Agenda

• Functional programming from c# perspective, Linq principles

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

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?

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

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:

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

F# has / is:

Functional

But for now, lets focus on this!

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 creation

In C#, mark fields as readonly

No mutable state no local variables

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

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

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 concept

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

Recursion:

In functional programming, no iteration or ”looping”

Recursion is very important concept

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

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!

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

top related