80
F# Eye for the C# guy F# Eye for the C# guy Leon Bambrick, secretGeek.net Leon Bambrick, secretGeek.net

F# Eye for the C# Guy

Embed Size (px)

DESCRIPTION

F# Eye for the C# Guy by Leon Bambrick

Citation preview

Page 1: F# Eye for the C# Guy

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

Leon Bambrick, secretGeek.netLeon Bambrick, secretGeek.net

Page 2: F# Eye for the C# Guy

F#...F#...

Do not be afraid.Do not be afraid.

Page 3: F# Eye for the C# Guy

hello

Page 4: F# Eye for the C# Guy

I’m F#

Page 5: F# Eye for the C# Guy
Page 6: F# Eye for the C# Guy

functional

Page 7: F# Eye for the C# Guy

Purely functional…

Avoid Side-

Effects!

Page 8: F# Eye for the C# Guy

Purely functional…

Avoid Mutation!

Page 9: F# Eye for the C# Guy

Purely functional…

No Variables!

Only Functions!

Page 10: F# Eye for the C# Guy

Purely functional…

Same input ->Same output!

Page 11: F# Eye for the C# Guy

No Shared StateNo Shared State

Purely functional…Purely functional…

Page 12: F# Eye for the C# Guy

Why learn F#?

Page 13: F# Eye for the C# Guy

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

Why learn F#?

Page 14: F# Eye for the C# Guy

• 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#?

Page 15: F# Eye for the C# Guy

• 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#?

Page 16: F# Eye for the C# Guy

Moore’s Law Keeps Going!

Why learn F#?

Page 17: F# Eye for the C# Guy

But computers are not getting faster

Why learn F#?

Page 18: F# Eye for the C# Guy

• Data Grows Quickly

• But # of Dimensions Grows much faster!

• And semi-structured data outgrowing structured

• Entropy Increasing

• Complexity is through the roof!

Page 19: F# Eye for the C# Guy

“Software gets slower faster than hardware gets faster”

--Wirth’s Law

Page 20: F# Eye for the C# Guy
Page 21: F# Eye for the C# Guy

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

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Page 22: F# Eye for the C# Guy

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

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Page 23: F# Eye for the C# Guy

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

Some stuff remains expensive!– Time–Concurrency– Locking

Computer Economics is Changing

Page 24: F# Eye for the C# Guy

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

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

Computer Economics is Changing

Page 25: F# Eye for the C# Guy

This tips the balance toward higher abstractions

Page 26: F# Eye for the C# Guy

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

Page 27: F# Eye for the C# Guy

Think About Game Programming

Page 28: F# Eye for the C# Guy

Game Programming Involves…

• 3D Animation

• Rendering

• Shading

• Simulation (e.g. physics)

• Collision Detection

• AI Opponents

Page 29: F# Eye for the C# Guy

Genealogy of F# …

• Theorem proving and ISWIM

Page 30: F# Eye for the C# Guy

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”

Page 31: F# Eye for the C# Guy

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”, which begat:

• CAML

Page 32: F# Eye for the C# Guy

Genealogy of F# …

• Theorem proving and ISWIM begat:

–ML “Meta Language”, which begat:

• CAML, which in turn begat

–OCaml

Oh!

Page 33: F# Eye for the C# Guy

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!

Page 34: F# Eye for the C# Guy

WTF#?

• First official functional language on .net

Page 35: F# Eye for the C# Guy

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

Page 36: F# Eye for the C# Guy

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

• Assimilated by dev-div

Page 37: F# Eye for the C# Guy

WTF#?

• First official functional language on .net

• Deep support thanks to Generics

• Assimilated by dev-div

• Will ship in VS.next

Page 38: F# Eye for the C# Guy

Code!

Finally!

Page 39: F# Eye for the C# Guy

Code!

//F#let a = 2

Page 40: F# Eye for the C# Guy

Code!

//F#let a = 2

The ‘let’ keyword represents a binding

between a value and an expression tree.

Page 41: F# Eye for the C# Guy

Code!

//F#let a = 2

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

blah blah blah.

Page 42: F# Eye for the C# Guy

Code!

//F#let a = 2

//C#int a = 2≠

Page 43: F# Eye for the C# Guy

Code!

//F#let a = 2

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

More like

Page 44: F# Eye for the C# Guy

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); } }}

Page 45: F# Eye for the C# Guy

More Code!

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

More Noise Than Signal!

Page 46: F# Eye for the C# Guy

More Code!

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

Looks Weakly typed?Maybe Dynamic?

Page 47: F# Eye for the C# Guy

F#?

Page 48: F# Eye for the C# Guy

F#

Page 49: F# Eye for the C# Guy

F# Yet Expressive

Page 50: F# Eye for the C# Guy

F#

Yet Versatile

Page 51: F# Eye for the C# Guy

More Code!

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

Type Inference

Page 52: F# Eye for the C# Guy

let a = 2let a = 3

error: FS0037: Duplicate definition of value 'a'

Immutable by default

Page 53: F# Eye for the C# Guy

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

Page 54: F# Eye for the C# Guy

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

Parameter

Page 55: F# Eye for the C# Guy

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

“Signature”

Page 56: F# Eye for the C# Guy

let square x = x * x

> val square : int -> int

square 5

> val it : int = 25

simple function…

Types are “inferred”

Page 57: F# Eye for the C# Guy

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

Page 58: F# Eye for the C# Guy

Discriminated union typestype NullableInt =

| Value of int

| Nothing of unit

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

Page 59: F# Eye for the C# Guy

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

Page 60: F# Eye for the C# Guy

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…

Page 61: F# Eye for the C# Guy

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

Page 62: F# Eye for the C# Guy

Another great tool is “pattern matching”

Page 63: F# Eye for the C# Guy

Another great tool is “pattern matching”

“Switch/Case” statements on

steroids

Page 64: F# Eye for the C# Guy

Another great tool is “pattern matching”

“Switch/Case” statements on

steroids

“method overloads” on

crack

Page 65: F# Eye for the C# Guy

Discriminated unions example

type Weapon =

| Knife

| Gun

| Bomb

Page 66: F# Eye for the C# Guy

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

Page 67: F# Eye for the C# Guy

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

Page 68: F# Eye for the C# Guy

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)

Page 69: F# Eye for the C# Guy

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)

Page 70: F# Eye for the C# Guy

Useful libraries

Neat manual

AwesomeSamples

Page 71: F# Eye for the C# Guy
Page 72: F# Eye for the C# Guy
Page 73: F# Eye for the C# Guy

“Empty” source file…

5 pages of help!

Page 74: F# Eye for the C# Guy

Make sure F# Interactive is running!

Page 75: F# Eye for the C# Guy

F# Interactive:

It’s the bomb!

Page 76: F# Eye for the C# Guy

F# Interactive:

Page 77: F# Eye for the C# Guy
Page 78: F# Eye for the C# Guy

msdn.microsoft.com/fsharp/

Page 79: F# Eye for the C# Guy

8 Ways to Learn

• http://cs.hubfs.net

• Codeplex Fsharp Samples

• Books

• ML

Page 80: F# Eye for the C# Guy

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