What The F#

866 views
829 views

Published on

A quick overview of functional programming with F#

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
866
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

What The F#

  1. 1. What The F#?A beginners guide to F#ing around with F#
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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!
  10. 10. 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!!!
  11. 11. 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!
  12. 12. 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!
  13. 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
  14. 14. Anonymous F#ing• let sumResult x y operation = (operation x) + (operation y)• let sumOfDoubles = sumResult 1 2 (fun x -> x + x) – sumOfDoubles = 6!

×