2. ïĄ F# is starting from a research project of
Microsoft, created by Don Syme
ïĄ F# is a functional programming language
that runs on top of .NET
ïĄ F# is now part of VS 2010 built in
programming language
ïĄ Also part of OCAML programming language
family
3. ïĄ There are many definitions about this, and
thereâs no simple standard definition
ïĄ According to Erik Meijer and Brian Beckman
of Microsoft, Functional Programming is
programming with mathematical function,
where function is a first class citizen of a
program
4. ïĄ A simple operation that returns a value, that
can have a parameter or more than one
parameters
7. F# OOP (C#, VB, C++âŠ)
ïĄ Immutable by default ïĄ Mutable by default
ïĄ Function is the same as ïĄ Function is treated as a
data in a program, and can method that must reside in
be written as a standalone a body of a class
function ïĄ âNoisyâ syntax
ïĄ Succinct syntax ïĄ Type inference is only
ïĄ Type inference is available available since C# 3.0 and
from the start VB 9.0, not in C++ and
others
9. ïĄ Immutable by default ïĄ Mutable by default
let y = x + 1 x = x +1
ïĄ Mutable is explicit ïĄ Immutable is explicit.
For example, in C#:
let mutable x = x + 1 readonly x = 0
ïĄ In VB:
ReadOnly x As Integer
10. ïĄ In F#, it can be ïĄ In C# and VB, it must
standalone and simple be enclosed in a
class/module
let f x = x +2 class SimpleFunc
{
public static Int32
f(Int32 x)
ïĄ This is why it is called {
return x + 2;
âsuccinctâ }
}
ïĄ This is ânoisyâ
11. ïĄ Already have at the first release
ïĄ It is also a strong type language
ïĄ Including built in support of Generic
ïĄ The type inference is not just local type
inference, but it then can be inferred based
on the use of the parameters!
12. Int32
let avalue =10 String
let aName = âHelloâ
let savingInterest = 0.2 Double
13. ïĄ Type inference on functions when itâs used
let f x = sqr x + 1
Infers integer
.. return type from a whole
becomes int numberâŠ
let sqr x = x * x
Infers double
from a double
literal..
let f x = sqr x + 1.0
20. ïĄ Multiple lines of code is using indentation:
let rec fib x =
if x < 2 then 1
else fib (xâ1) + fib (x-2)
21. ïĄ Comment is the same with VB and C#
// some code
let x = x + 2
// XML doc comment:
/// <summary>A square function<summary>
/// <param name=âxâ>the value</param>
let f x = x * x
24. ïĄ Mutable is easy, by adding keyword
âmutableâ
ïĄ Next operation of assignment must use â<-â
to differentiate mutability.
let mutable y = 10
y <- y + 1
25. ïĄ Creating enum is also easy:
type Suit =
| Heart
| Diamond
| Spade
| Club
26. type Vector2D(dx:float, dy:float) =
// The pre-computed length of the vector
let length = sqrt(dx*dx + dy*dy)
/// The displacement along the X-axis
member v.DX = dx
/// The displacement along the Y-axis
member v.DY = dy
/// The length of the vector
member v.Length = length
// Re-scale the vector by a constant
member v.Scale(k) = Vector2D(k*dx, k*dy)
27. ïĄ Itâs easy! Just create type but with abstract
keyword as modifier for all functions and
other members:
type IPeekPoke =
abstract Peek: unit -> int
abstract Poke: int -> unit