Download An Introduction to F# – Sushant Bhatia

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm characterizations wikipedia , lookup

C Sharp syntax wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Subroutine wikipedia , lookup

Structured programming wikipedia , lookup

APL syntax and symbols wikipedia , lookup

Monad (functional programming) wikipedia , lookup

Reactive programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Go (programming language) wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Functional programming wikipedia , lookup

C++ wikipedia , lookup

Standard ML wikipedia , lookup

Corecursion wikipedia , lookup

Transcript
An Introduction to F#
Sushant Bhatia
@aboutdev
aboutdev.com
[email protected]
Why learn F#?
Overview
Tour of F# in Visual Studio 2011
Fundamentals of F#
Functional Programming
Examples
DEMO
F# in Visual Studio 2011
Fundamentals of F#
Core Types
Signature
Name
Description
Example
Unit
Unit
()
int, float
Concrete Type
10, 6.04
‘a, ‘b
Generic Type
‘a -> ‘b
Function Type
‘a * ‘b
Tuple Type
Ordered collection (1, 2), (“ten”,
of values
“soup”)
‘a list
List Type
List of values
[1; 2; 3], [1 .. 3]
‘a option
Option Type
Optional value
Some(3), None
fun x -> x + 1
Modules & Namespaces
Default -> anonymous
module
Nest Modules
Namespace
Pipe Forward
Pass results of first function to second
Benefit
Chain functions together
Type inference
[1 .. 10] |> List.map (fun x -> x * x)
|> List.iter (printfn "%i")
Pattern Matching
Series of rules that will execute if a pattern
matches the input.
Switch on Steroids
Discriminated Unions
Type
1 of a set of possible values
Used for complex data structures
Functional Programming
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
Functional programming is a
programming paradigm that
treats computations as the
evaluation of mathematical
functions and avoids state and
mutable data.
[Wikipedia]
 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]
1. First Class Functions
2. Higher Order Functions
3. Pure Functions
4. Recursion / Tail Recursion
5. Currying
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]
2. Higher Order Functions
a) Pass function as an argument
let 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
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)
4. Recursion / Tail Recursion
a) Iteration through recursive invocation
let rec factorial x =
if x <= 1I then 1I
else let recResult = factorial (x-1I)
let result = x * recResult
result
b) Tail recursion – Accumulators
let factorialTail x =
let rec tailRecFact x acc =
if x <= 1I then acc
else tailRecFact (x-1I) (acc * x)
tailRecFact x 1I
5. Currying
let add x y = x + y
let addToTen = add 10
addToTen 5
1. Type Provider – Netflix
2. Units of Measure
3. Async / Parallel
Demo
Examples – Project Euler
1. Programming F# - Chris Smith
2. Beginning F# - Robert Pickering
3. Expert F# - Don Syme
4.
5.
6.
7.
http://blogs.msdn.com/b/dsyme
http://en.wikibooks.org/wiki/Programming:F_Sharp
http://projecteuler.net/
http://fdatamining.blogspot.com/2009/12/f-onlinevideos.html
8. http://www.tryfsharp.org/Tutorials.aspx
9. http://fssnip.net/
How to Learn F#
2 e-books
Giveaway / Raffle