what the f#

14
What The F#? A beginners guide to F#ing around with F#

Upload: rtigger

Post on 04-Jul-2015

781 views

Category:

Technology


0 download

DESCRIPTION

A quick overview of functional programming with F#

TRANSCRIPT

Page 1: What The F#

What The F#?

A beginners guide to F#ing around with F#

Page 2: What The F#

Who started F#ing?

• Functional programming has been around since 1956, starting with Information Processing Language, and popularized with Lisp

• F# started by Microsoft Research in 2005, as an implementation of OCaml

Page 3: What The F#

What the F# would I use it for?

• Used for scientific modelling, math, AI research, financial institutions, graphic design, CPU design, compiler programming, telecom engineers, etc.

• Targeted for heavy number crunching, abstract symbolic processing, or theorem proving

• That being said, could totally be used for CRUD, web dev, GUI, games, and general-purpose programming

Page 4: What The F#

What F#ing good is it?

• All those cool things in imperative programming, like generics, type inference, list comprehension, and anonymous types?

ALL CAME FROM FUNCTIONAL PROGRAMMING

Page 5: What The F#

F#ing Functions, how do they work?

• syntax:

– let function-name parameter parameter = function-body

• let sum x y = x + y

• “sum” is a symbol that points to the function body

• Execution:

– let result = sum 1 2 //result 1 + 2

Page 6: What The F#

What the F# is an Immutable Value?

• Every value in F# is immutable – it will not change after being declared

• This allows for values to be passed around without worrying if a function is going to change it– Data can be processed multi-threaded without having

to worry about locks or race conditions

• Functions that return objects actually return entirely new objects cloned from what was passed in

Page 7: What The F#

How the F# does it know what type to use?

• Type Inference!

• let increment x = x + 1

– you’re adding ints and returning an int

• let add x y = x + y

– you’re adding two values of the same type and returning that type

Page 8: What The F#

F#ing Function Signatures

• let increment x = x + 1

• increment’s type is “int -> int”

– We take an int, and return an int

• let sum x y = x + y

• sum‘s type is “int -> int -> int”

– We take two ints, and return an int

• Functions always return one value

Page 9: What The F#

I’m about to blow your F#ing mind

• let sum x y = x + y

• int -> int -> int

• … is actually …

• int -> (int -> int) :-o

• EVERY FUNCTION ALWAYS TAKES ONE PARAMTER AND RETURNS ONE VALUE!

Page 10: What The F#

Hold on to your F#ing seat

• let sum x y = x + y

– int -> (int -> int)

• Calling “sum 5” RETURNS A FUNCTION

– of type “int -> int”

• let half-function = sum 5

• let result = half-function 10

• RESULT EQUALS 15!!!

Page 11: What The F#

F# beef stew, this is Currying

• The ability to take larger functions and use specific pieces of them

• let sum x y = x + y

• let increment = sum 1

• Now we can increment whatever the F# we want!

Page 12: What The F#

How to F# up an argument

• Because functions are values, they can be passed around like arguments

• let sumResult x y operation =

(operation x) + (operation y)

• let square x = x * x

• let sumOfSquares = sumResult 2 3 square

– sumOfSquares == 13!

Page 13: What The F#

F#ing complicated types

• let sumResult x y op = (op x) + (op y)

• Type: int -> int -> (int -> int) -> int

• But really…

– int -> (int -> ((int -> int) -> int))

• let alwaysWorkWithOneAndTwo =

sumResult 1 2

• Type: (int -> int) -> int

Page 14: What The F#

Anonymous F#ing

• let sumResult x y operation =

(operation x) + (operation y)

• let sumOfDoubles =

sumResult 1 2 (fun x -> x + x)

– sumOfDoubles = 6!