F sharp _vs2010_beta2
-
Upload
eriawan-kusumawardhono -
Category
Technology
-
view
102 -
download
2
description
Transcript of F sharp _vs2010_beta2
![Page 1: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/1.jpg)
F# on VS 2010 Beta 2Eriawan Kusumawardhono
![Page 2: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/5.jpg)
A simple function
f(x) = x+1y=x+1
![Page 6: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/6.jpg)
Not a function!
x=x+1x++
![Page 7: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/8.jpg)
Detailed comparisonsComparison of F# to others such as C#, VB, C++
![Page 9: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/12.jpg)
From a simple type inference..
let avalue =10 let aName = ‘Hello’let savingInterest = 0.2
Int32
String
Double
![Page 13: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/13.jpg)
… 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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/14.jpg)
Succinct syntaxLess noise syntax but it’s still strongly typed
![Page 15: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/15.jpg)
Variable declaration (1)
Always begin with keyword “let”
let avalue = 10 let aName = ‘Hello’let savingInterest = 0.2
![Page 16: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/19.jpg)
The codeThe code in F#
![Page 20: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/22.jpg)
OOP and imperative in F#The object oriented and imperative are here in F#
![Page 23: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/23.jpg)
A function (revisited)
let f x = x *xlet g(x) = x* xfun x -> x * x
![Page 24: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/25.jpg)
Creating enum
Creating enum is also easy:
type Suit = | Heart | Diamond | Spade | Club
![Page 26: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/28.jpg)
A unique feature: units of measure!Unit of measure in F# only!
![Page 29: F sharp _vs2010_beta2](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061212/5495c06dac795925288b50e3/html5/thumbnails/30.jpg)
End
Created by Eriawan Kusumawardhono, courtesy of RX Communica