1 / 30

F# on VS 2010 Beta 2

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

penney
Download Presentation

F# on VS 2010 Beta 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EriawanKusumawardhono F# on VS 2010 Beta 2

  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

  3. 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

  4. What is a function? • A simple operation that returns a value, that can have a parameter or more than one parameters

  5. A simple function f(x) = x+1 y=x+1

  6. Not a function! x=x+1 x++

  7. 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

  8. Detailed comparisons Comparison of F# to others such as C#, VB, C++

  9. 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

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

  11. 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!

  12. From a simple type inference.. Int32 String let avalue =10 let aName = ‘Hello’ let savingInterest = 0.2 Double

  13. … 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

  14. Succinct syntax Less noise syntax but it’s still strongly typed

  15. Variable declaration (1) • Always begin with keyword “let” letavalue = 10 letaName = ‘Hello’ letsavingInterest = 0.2

  16. 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’

  17. Function declaration (1) • Always begin with let keyword let f x = x + 1 let sqr y = y * y let force x = x * gravity

  18. 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

  19. The code The code in F#

  20. 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)

  21. 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

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

  23. A function (revisited) let f x = x *x let g(x) = x* x fun x -> x * x

  24. 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

  25. Creating enum • Creating enum is also easy: type Suit = | Heart | Diamond | Spade | Club

  26. 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)

  27. 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

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

  29. 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)

  30. Created by EriawanKusumawardhono, courtesy of RX Communica End

More Related