What The F#?A beginners guide to F#ing around with 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
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
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
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
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
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
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
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!
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!!!
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!
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!
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
Anonymous F#ing• let sumResult x y operation = (operation x) + (operation y)• let sumOfDoubles = sumResult 1 2 (fun x -> x + x) – sumOfDoubles = 6!