f sharp _vs2010_beta2

30
F# on VS 2010 Beta 2 Eriawan Kusumawardhono

Upload: eriawan-kusumawardhono

Post on 17-Dec-2014

102 views

Category:

Technology


2 download

DESCRIPTION

Overview of F# on Visual Studio 2010 Beta 2

TRANSCRIPT

Page 1: F sharp _vs2010_beta2

F# on VS 2010 Beta 2Eriawan Kusumawardhono

Page 2: F sharp _vs2010_beta2

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

Page 3: F sharp _vs2010_beta2

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

Page 4: F sharp _vs2010_beta2

What is a function?

A simple operation that returns a value, that can have a parameter or more than one parameters

Page 5: F sharp _vs2010_beta2

A simple function

f(x) = x+1y=x+1

Page 6: F sharp _vs2010_beta2

Not a function!

x=x+1x++

Page 7: F sharp _vs2010_beta2

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

Page 8: F sharp _vs2010_beta2

Detailed comparisonsComparison of F# to others such as C#, VB, C++

Page 9: F sharp _vs2010_beta2

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

Page 10: F sharp _vs2010_beta2

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; }}

Page 11: F sharp _vs2010_beta2

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!

Page 12: F sharp _vs2010_beta2

From a simple type inference..

let avalue =10 let aName = ‘Hello’let savingInterest = 0.2

Int32

String

Double

Page 13: F sharp _vs2010_beta2

… 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

Page 14: F sharp _vs2010_beta2

Succinct syntaxLess noise syntax but it’s still strongly typed

Page 15: F sharp _vs2010_beta2

Variable declaration (1)

Always begin with keyword “let”

let avalue = 10 let aName = ‘Hello’let savingInterest = 0.2

Page 16: F sharp _vs2010_beta2

Variable declaration (2)

When there’s a need for explicit type system:

let x:int = 0let piConstant:float = 3.141let Name:String = ‘Eriawan’

Page 17: F sharp _vs2010_beta2

Function declaration (1)

Always begin with let keyword

let f x = x + 1let sqr y = y * ylet force x = x * gravity

Page 18: F sharp _vs2010_beta2

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

Page 19: F sharp _vs2010_beta2

The codeThe code in F#

Page 20: F sharp _vs2010_beta2

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)

Page 21: F sharp _vs2010_beta2

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

Page 22: F sharp _vs2010_beta2

OOP and imperative in F#The object oriented and imperative are here in F#

Page 23: F sharp _vs2010_beta2

A function (revisited)

let f x = x *xlet g(x) = x* xfun x -> x * x

Page 24: F sharp _vs2010_beta2

Mutable values

Mutable is easy, by adding keyword “mutable”

Next operation of assignment must use “<-” to differentiate mutability.

let mutable y = 10y <- y + 1

Page 25: F sharp _vs2010_beta2

Creating enum

Creating enum is also easy:

type Suit = | Heart | Diamond | Spade | Club

Page 26: F sharp _vs2010_beta2

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)

Page 27: F sharp _vs2010_beta2

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

Page 28: F sharp _vs2010_beta2

A unique feature: units of measure!Unit of measure in F# only!

Page 29: F sharp _vs2010_beta2

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)

Page 30: F sharp _vs2010_beta2

End

Created by Eriawan Kusumawardhono, courtesy of RX Communica