F # is a new object-oriented functional programming language for the Microsoft. NET Framework, which has been integrated into this year's release of Microsoft Visual Studio 2010. F # Sets simple, concise syntax and high static typing. This language is capable of tasks ranging from lightweight exploratory programming in F # Interactive to large-scale component development using Visual Studio based on the. NET Framework.
F # is designed to run entirely on the CLR. As a language based on the. NET Framework, F # makes full use of the rich library resources on the. NET Framework platform to build. NET libraries or implement. NET interfaces. F # also leverages most of the CLR core features, including generics, garbage collection, tail-call directives, and basic common Language Infrastructure (CLI) type systems.
This article describes some of the core concepts of the F # language and its implementation on the CLR.
A brief overview of F #
First, let's take a quick look at some of the core features in the F # language. For more information about these features in the F # language and other concepts that you are interested in, refer to the documentation on the F # Developer Center for Fsharp.net.
The most basic function of F # is the Let keyword, which binds the value to the name. Let can be used to bind data and function values to achieve top-level binding and local binding:
let data = 12
let f x =
let sum = x + 1
let g y = sum + y*y
g x
F # provides several core data types, as well as a syntax for using structured data, including lists, typed optional values, and tuples:
let list1 = ["Bob"; "Jom"]
let option1 = Some("Bob")
let option2 = None
let tuple1 = (1, "one", '1')
You can match these structured data types with other types by using an F # pattern matching expression. Pattern matching is similar to using switch statements in programming languages such as C, but provides more ways to match and extract parts from matched expressions, somewhat similar to how regular expressions are used for pattern-matching strings:
let person = Some ("Bob", 32)
match person with
| Some(name,age) -> printfn "We got %s, age %d" name age
| None -> printfn "Nope, got nobody"
F # fully leverages the. NET Framework libraries to perform many tasks, such as accessing data from a wide variety of data sources.. NET libraries are used in F # in the same way that they are used in other. NET languages:
let http url =
let req = WebRequest.Create(new Uri(url))
let resp = req.GetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
reader.ReadToEnd()
Like C # or Visual Basic, F # is also an object-oriented language that can define any. NET class or structure:
type Point2D(x,y) =
member this.X = x
member this.Y = y
member this.Magnitude =
x*x + y*y
member this.Translate(dx, dy) =
new Point2D(x + dx, y + dy)
In addition, F # supports two special types: records and recognizable unions. Records provide a simple representation of data values with named fields, and an identifiable union is a type expression that contains a number of different values, and the associated data in each value varies:
type Person =
{ Name : string;
HomeTown : string;
BirthDate : System.DateTime }
type Tree =
| Branch of Tree * Tree
| Leaf of int