F# Eye for the C# Guy

Post on 02-Jul-2015

10658 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

F# Eye for the C# Guy by Leon Bambrick

Transcript

F# Eye for the C# guyF# Eye for the C# guy

Leon Bambrick, secretGeek.netLeon Bambrick, secretGeek.net

F#...F#...

Do not be afraid.Do not be afraid.

hello

I’m F#

functional

Purely functional…

Avoid Side-

Effects!

Purely functional…

Avoid Mutation!

Purely functional…

No Variables!

Only Functions!

Purely functional…

Same input ->Same output!

No Shared StateNo Shared State

Purely functional…Purely functional…

Why learn F#?

• Do More With Less– “If I was using F# I’d be done by now”

Why learn F#?

• Do More With Less– “If I was using F# I’d be done by now”

• See where C# and VB.net are headed

Why learn F#?

• Do More With Less– “If I was using F# I’d be done by now”

• See where C# and VB.net are headed

• “Learn one new language every year” (or two)

Why learn F#?

Moore’s Law Keeps Going!

Why learn F#?

But computers are not getting faster

Why learn F#?

• Data Grows Quickly

• But # of Dimensions Grows much faster!

• And semi-structured data outgrowing structured

• Entropy Increasing

• Complexity is through the roof!

“Software gets slower faster than hardware gets faster”

--Wirth’s Law

Some stuff is now cheap!–Ram–Disk –Cores

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Some stuff is now cheap!–Ram–Disk –Cores

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Some stuff is now cheap!–Ram–Disk –Cores

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Some stuff is now cheap!–Ram–Disk –Cores

Some stuff remains expensive!–Real Time–Concurrency– Locking

Computer Economics is Changing

This tips the balance toward higher abstractions

Some Applications of F#

• “Computationally intensive” work

• Map/Reduce over internets

• Financial Analysis

• Advertising Analysis

• SQL Data Mining

• XNA Games Development

• Web tools, Compile F# to Javascript

Think About Game Programming

Game Programming Involves…

• 3D Animation

• Rendering

• Shading

• Simulation (e.g. physics)

• Collision Detection

• AI Opponents

Genealogy of F# …

• Theorem proving and ISWIM

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”, which begat:

• CAML

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”, which begat:

• CAML, which in turn begat

–OCaml

Oh!

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”, which begat:

• CAML, which in turn begat

–OCaml, which in turn begat

»F#... a sort of OCaml.net

(plus more)Oh!

WTF#?

• First official functional language on .net

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

• Assimilated by dev-div

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

• Assimilated by dev-div

• Will ship in VS.next

Code!

Finally!

Code!

//F#let a = 2

Code!

//F#let a = 2

The ‘let’ keyword represents a binding

between a value and an expression tree.

Code!

//F#let a = 2

The ‘let’ blah blah blahblah blah blahblah blahblah blah blah blah

blah blah blah.

Code!

//F#let a = 2

//C#int a = 2≠

Code!

//F#let a = 2

//C#//a function!static int a(){ return 2;}

More like

More Code!

//F##lightopen Systemlet a = 2Console.WriteLine a

//C#using System;

namespace ConsoleApplication1{ class Program { static int a() { return 2; }

static void Main(string[] args) { Console.WriteLine(a); } }}

More Code!

//F##lightopen Systemlet a = 2Console.WriteLine a

More Noise Than Signal!

More Code!

//F##lightopen Systemlet a = 2Console.WriteLine a

Looks Weakly typed?Maybe Dynamic?

F#?

F#

F# Yet Expressive

F#

Yet Versatile

More Code!

//F##lightopen Systemlet a = 2Console.WriteLine a

Type Inference

let a = 2let a = 3

error: FS0037: Duplicate definition of value 'a'

Immutable by default

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

Parameter

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

“Signature”

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

Types are “inferred”

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

Wish C#/“O-O” had this.

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

“O-O” has discriminated

union envy

When the only tool you have is a

hammerEverything looks

like a nail

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

“O-O” has discriminated

union envy

When the only tool you have is a

hammerEverything looks

like a nail

e.g. “Inheritance” ends up being used in places

where it’s not a perfect fit…

“O-O” has discriminated

union envy

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit When the only tool you have is a

hammerEverything 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?

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

type Weapon =

| Knife

| Gun

| Bomb

Pattern Matching

type Weapon =

| Knife

| Gun

| Bomb

//block any weapon!let block w = match w with | Knife | Gun -> disarm w | _ -> difuse w

block Gunblock Knifeblock Bomb

Pattern Matching

type Weapon =

| Knife

| Gun

| Bomb

//block any weaponlet block w = match w with | Knife | Gun -> disarm w | _ -> difuse w

block Gunblock Knifeblock Bomb

Lazy is a virtuelet lazy_square x = lazy ( print_endline “thinking...“ x * x )

let lazy_square_ten = lazy_square 10

//first time: “thinking…”Lazy.force (lazy_square_ten)

//second time: no thinking, just resultLazy.force (lazy_square_ten)

Lazy is a virtuelet lazy_square x = lazy ( print_endline “thinking...“ x * x )

let lazy_square_ten = lazy_square 10

//first time: “thinking…”Lazy.force (lazy_square_ten)

//second time: no thinking, just resultLazy.force (lazy_square_ten)

Useful libraries

Neat manual

AwesomeSamples

“Empty” source file…

5 pages of help!

Make sure F# Interactive is running!

F# Interactive:

It’s the bomb!

F# Interactive:

msdn.microsoft.com/fsharp/

8 Ways to Learn

• http://cs.hubfs.net

• Codeplex Fsharp Samples

• Books

• ML

Acknowledgements• Cartman

• Einstein

• Dilbert

• Alan Turing

• Alonzo Church

• Godzilla

• Gears of war

• John Hughes, Why Functional Programming Matters, http://www.math.chalmers.se/~rjmh/Papers/whyfp.html

• Robert Pickering, Foundations of F#, http://www.apress.com/book/view/1590597575

• Slava Akhmechet, Functional Programming For The Rest of Us, http://www.defmacro.org/ramblings/fp.html

• Steve Yegge, Execution In the Kingdom of Nouns, http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

• P. J. Landin, The Next 700 Programming Languages http://www.cs.utah.edu/~wilson/compilers/old/papers/p157-landin.pdf

• Tim Sweeney, The Next Mainstream Programming Language, http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf

• Tomas Petricek, F# Web Tools, Ajax Made Simple, http://www.codeplex.com/fswebtools

• Don Syme, http://blogs.msdn.com/dsyme

top related