300 likes | 420 Views
Eriawan Kusumawardhono. F# on VS 2010 Beta 2. 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
E N D
EriawanKusumawardhono F# on VS 2010 Beta 2
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+1 y=x+1
Not a function! x=x+1 x++
Then, compared to other(s).. F# 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 • 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
Detailed comparisons Comparison 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.. Int32 String let avalue =10 let aName = ‘Hello’ let savingInterest = 0.2 Double
… to a complex inference! • Type inference on functions when it’s used let f x = sqr x + 1 Infers integer from a whole number… .. return type becomes int let sqr x = x * x Infers double from a double literal.. let f x = sqr x + 1.0
Succinct syntax Less noise syntax but it’s still strongly typed
Variable declaration (1) • Always begin with keyword “let” letavalue = 10 letaName = ‘Hello’ letsavingInterest = 0.2
Variable declaration (2) • When there’s a need for explicit type system: let x:int = 0 let piConstant:float = 3.141 let Name:String = ‘Eriawan’
Function declaration (1) • Always begin with let keyword let f x = x + 1 let sqr y = y * y let 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 code The code in F#
Multiple lines? Use indentation • Multiple lines of code is using indentation: letrec 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 code let x = x + 2 // XML doc comment: /// <summary>A square function<summary> /// <paramname=“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 *x let g(x) = x* x fun x -> x * x
Mutable values • Mutable is easy, by adding keyword “mutable” • Next operation of assignment must use “<-” to differentiate mutability. let mutable y = 10 y <- 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 memberv.DX = dx /// The displacement along the Y-axis memberv.DY = dy /// The length of the vector memberv.Length = length // Re-scale the vector by a constant memberv.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 letgravityOnEarth = 9.81<m/s^2> letheightOfMyOfficeWindow = 3.5<m> letspeedOfImpact = sqrt (2.0 * gravityOnEarth + heightOfMyOfficeWindow)
Created by EriawanKusumawardhono, courtesy of RX Communica End