An Introduction to F#
description
Transcript of An Introduction to F#
![Page 2: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/2.jpg)
Why learn F#?
![Page 3: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/3.jpg)
OverviewTour of F# in Visual Studio 2011
Fundamentals of F#
Functional Programming
Examples
![Page 4: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/4.jpg)
F# in Visual Studio 2011DEMO
![Page 5: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/5.jpg)
Fundamentals of F#
![Page 6: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/6.jpg)
Signature Name Description Example
Unit Unit ()
int, float Concrete Type 10, 6.04
‘a, ‘b Generic Type
‘a -> ‘b Function Type fun x -> x + 1
‘a * ‘b Tuple Type Ordered collection of values
(1, 2), (“ten”, “soup”)
‘a list List Type List of values [1; 2; 3], [1 .. 3]
‘a option Option Type Optional value Some(3), None
Core Types
![Page 7: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/7.jpg)
Default -> anonymous module
Nest Modules
Namespace
Modules & Namespaces
![Page 8: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/8.jpg)
Pass results of first function to second
Benefit Chain functions togetherType inference
Pipe Forward
[1 .. 10] |> List.map (fun x -> x * x) |> List.iter (printfn "%i")
![Page 9: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/9.jpg)
Series of rules that will execute if a pattern matches the input.
Switch on Steroids
Pattern Matching
![Page 10: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/10.jpg)
Type
1 of a set of possible values
Used for complex data structures
Discriminated Unions
![Page 11: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/11.jpg)
Functional Programming
![Page 12: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/12.jpg)
What is Functional Programming?
A function is a rule that associates to each x from some set X of values, a unique y from another set Y of values. If f is the name of the function,
y = f ( x )
f : X → Y
![Page 13: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/13.jpg)
Functional programming is a programming paradigm that treats computations as the evaluation of mathematical functions and avoids state and mutable data.
[Wikipedia]
![Page 14: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/14.jpg)
All programs and procedures are functions and clearly distinguish incoming values from outgoing values
There are no variables or assignments – variables are replaced by parameters
There are no loops – replaced by recursive calls
The value of a function depends only on the value of its parameters and not on the order of evaluation or the execution path that led to the call
Functions are first-class values (viewed as values themselves, computed by other functions and can be parameters to functions)
[Programming Languages. Kenneth C Louden]
![Page 15: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/15.jpg)
1. First Class Functions
2. Higher Order Functions
3. Pure Functions
4. Recursion / Tail Recursion
5. Currying
![Page 16: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/16.jpg)
1. First Class Functions
a) Bind an identifier to a function definition
let sqr = fun n -> n * n
b) Store functions in data structures
let aTuple = (sqr, fun n -> n + n) let aList = [sqr, fun n -> n + n]
![Page 17: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/17.jpg)
2. Higher Order Functions
a) Pass function as an argumentlet data = List.map (fun n -> n * n) [ 1; 2; 3; 4; ]
b) Return function as value of function
let sqrList = let funSqr = fun lst -> List.map (fun a -> a * a) lst funSqr
![Page 18: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/18.jpg)
3. Pure Functions
a) No Side Effects / Referential transparency
b) Caching optimizations (memoization)
c) No Data Dependency = order of execution is independent and can be parallel (thread-safe)
![Page 19: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/19.jpg)
4. Recursion / Tail Recursion
a) Iteration through recursive invocationlet rec factorial x = if x <= 1I then 1I else let recResult = factorial (x-1I) let result = x * recResult result
b) Tail recursion – Accumulatorslet factorialTail x = let rec tailRecFact x acc = if x <= 1I then acc else tailRecFact (x-1I) (acc * x) tailRecFact x 1I
![Page 20: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/20.jpg)
5. Currying
let add x y = x + y
let addToTen = add 10
addToTen 5
![Page 21: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/21.jpg)
Demo
1. Type Provider – Netflix
2. Units of Measure
3. Async / Parallel
![Page 22: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/22.jpg)
Examples – Project Euler
![Page 23: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/23.jpg)
How to Learn F#
1. Programming F# - Chris Smith2. Beginning F# - Robert Pickering3. Expert F# - Don Syme
4. http://blogs.msdn.com/b/dsyme5. http://en.wikibooks.org/wiki/Programming:F_Sharp6. http://projecteuler.net/ 7. http://fdatamining.blogspot.com/2009/12/f-online-
videos.html8. http://www.tryfsharp.org/Tutorials.aspx9. http://fssnip.net/
![Page 24: An Introduction to F#](https://reader035.fdocuments.us/reader035/viewer/2022062813/568165eb550346895dd90dfc/html5/thumbnails/24.jpg)
Giveaway / Raffle2 e-books