Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
dmitrinesteruk @ gmail.com
http://spbalt.net
“…a programming paradigm that
treats computation as the evaluation
of mathematical functions and
avoids state and mutable data”
—Wikipedia
Higher-order functions
i => j => f(j)
Pure functions
Immutability
No side effects
(Tail) recursion
let f(x) = …; f(x-1);
Pattern matching
Math
Probabilistic models
Symbolic processing (parsing/lexing)
Symbolic differentiation
Circuit verification
Multi-paradigm language in .NET stack
Functional
Imperative
Performance similar to C#
Interactive console
Support in Visual Studio
Debugger
Editor
Mono support
#light
printfn “Hello, F#!”
#light
Light syntax – cuts down on ceremony when
writing code
Indentation instead of begin/end
Use of in,done keywords not required
No semicolons
Currently mandatory – will be default in future
printfn “Hello, F#”
Writes a line to the console
A top-level binding (like a global function)
Part of FSharp.Core
Referenced implicitly
Appears in a generated _main()
Can “Go To Definition”
Can do it with a function
let sayHello =
printfn “Hello, F#!”
sayHello
Or pass a parameter
let sayHello s =
printfn s
sayHello "Hello, F#!"
Application operator |> (forward pipe)
let sayHello s =
s |> printfn // same as printfn s
Explicit types
let length a = a.Length; // not ok
let length (a:string) =
a.Length // ok
Recursive definition
let rec factorial n =
if n <= 1 then 1
else n * factorial(n‐1)
Mutual recursion
let rec funcA x = 1 + funcB(x)
and funcB x = 1 – funcA(x)
One statement per line, use in for more
let powerOf4 x =
let y = x * x in y * y
No implicit conversions
let GetXName xname =
XName.op_Implicit(xname)
Aggressive strong typing
let o:obj = “test”; // fails
let o = box “test”; // ok
Mutability must be explicit
mutable keyword
variable <‐ value to assign
Clever switch statement
Can match values of any type
let matchMe x =
match x with
| 1 ‐> printfn "one"
| 2 ‐> printfn "two"
| _ ‐> printfn "something else"
let sumAndProduct a b =
(a+b, a*b)
let (s, p) = sumAndProduct 2 3
printfn "%d %d" s p
match x with
| Some(name) ‐> printfn name
| None ‐> printfn “anonymous”
Your typical CLR array
let people = [|
“john”;
“jane”;
“jack”
|]
people.Length
yields 3
Enumerable values
let a = seq [1; 2; 3]
let b = seq { for i in 1 .. 10 ‐> (i*i) }
Lazy-inited
seq { 1 .. 10000000 }
Step
seq { 1 .. 2 .. 10 }
yields 1, 3, 5, 7, 9
Strings are char sequences
printfn "%d" (Seq.length "Hello")
Iterated with for .. in .. do
for i in mySeq do printfn “%d” i
Linked list of values
[1; 2; 3]
let rec nonZero myList =
match myList with
| 0 :: t ‐> 1 :: nonZero t
| h :: t ‐> h :: nonZero t
| [] ‐> []
let rec htmlDecode text =
match text with
| ‘&’ :: ‘g’ :: ‘t’ :: ‘;’ :: tail ‐>
‘>’ :: htmlDecode tail // and so on
A non-exhaustive match will throw a
MatchFailureException
shift (1, 0) (100, 100)
result is (101, 100)
let shiftRight = shift (1, 0)
shiftRight (100, 100)
result is (101, 100)
Keep a lookaside table of computed values
let rec fib n =
if n <= 2 then 1
else fib(n‐1) + fib(n‐2)
Spawn one-by-one
for url in urls do
Async.Spawn(download(url))
Expert F#
Don Syme et al.
F# for Scientists
Jon Harrop