f sharp _vs2010_beta2
DESCRIPTION
Overview of F# on Visual Studio 2010 Beta 2TRANSCRIPT
F# on VS 2010 Beta 2Eriawan Kusumawardhono
What is F#?
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
What is functional programming?
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
What is a function?
A simple operation that returns a value, that can have a parameter or more than one parameters
A simple function
f(x) = x+1y=x+1
Not a function!
x=x+1x++
Then, compared to other(s)..
F#
Immutable by default Function is the same
as data in a program, and can be written as a standalone function
Succinct syntax Type inference is
available from the start
OOP (C#, VB, C++…)
Mutable by default Function is treated as
a method that must reside in a body of a class
‘Noisy’ syntax Type inference is only
available since C# 3.0 and VB 9.0, not in C++ and others
Detailed comparisonsComparison of F# to others such as C#, VB, C++
Immutability: F# vs others Immutable by
default
Mutable is explicit
Mutable by default
Immutable is explicit. For example, in C#:
In VB:
let y = x + 1 x = x +1
let mutable x = x + 1
readonly x = 0
ReadOnly x As Integer
Function (or method) syntaxes In F#, it can be
standalone and simple
This is why it is called “succinct”
In C# and VB, it must be enclosed in a class/module
This is “noisy”
let f x = x +2 class SimpleFunc{ public static Int32 f(Int32 x) { return x + 2; }}
Type inference flows nicely
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!
From a simple type inference..
let avalue =10 let aName = ‘Hello’let savingInterest = 0.2
Int32
String
Double
… to a complex inference!
Type inference on functions when it’s used
let sqr x = x * x
let f x = sqr x + 1
let f x = sqr x + 1.0
Infers integer from a whole number…
Infers double from
a double literal..
.. return type
becomes int
Succinct syntaxLess noise syntax but it’s still strongly typed
Variable declaration (1)
Always begin with keyword “let”
let avalue = 10 let aName = ‘Hello’let savingInterest = 0.2
Variable declaration (2)
When there’s a need for explicit type system:
let x:int = 0let piConstant:float = 3.141let Name:String = ‘Eriawan’
Function declaration (1)
Always begin with let keyword
let f x = x + 1let sqr y = y * ylet force x = x * gravity
Function declaration (2)
Parameters are written in a nice separation “juxtaposition” syntax: a space
let sqr x = x * x
let add a b = a + b
Function parameter
The codeThe code in F#
Multiple lines? Use indentation
Multiple lines of code is using indentation:
let rec fib x = if x < 2 then 1 else fib (x–1) + fib (x-2)
Commenting code
Comment is the same with VB and C#
// some codelet x = x + 2
// XML doc comment:
/// <summary>A square function<summary>/// <param name=“x”>the value</param>let f x = x * x
OOP and imperative in F#The object oriented and imperative are here in F#
A function (revisited)
let f x = x *xlet g(x) = x* xfun x -> x * x
Mutable values
Mutable is easy, by adding keyword “mutable”
Next operation of assignment must use “<-” to differentiate mutability.
let mutable y = 10y <- y + 1
Creating enum
Creating enum is also easy:
type Suit = | Heart | Diamond | Spade | Club
Creating types (..or class in OOP)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)
How about interface?
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
A unique feature: units of measure!Unit of measure in F# only!
Samples of unit of measure
[<Measure>] type kg
[<Measure>] type m
[<Measure>] type s
let gravityOnEarth = 9.81<m/s^2>let heightOfMyOfficeWindow = 3.5<m>let speedOfImpact = sqrt (2.0 * gravityOnEarth + heightOfMyOfficeWindow)
End
Created by Eriawan Kusumawardhono, courtesy of RX Communica