User:Soumyasch/Incubator/Fsharp

F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and will be a productized as fully supported language in the .NET Framework and Visual Studio ecosystem. .

Overview
F# is a strongly-typed language that uses type inference. As a result, data types need not be explicitly declared by the programmer, rather will be deduced by the compiler during compilation. However, F# also allows explicit data type declaration. Being a .NET language, F# supports .NET types and objects. But it extends the type system and categorizes types as immutable types or mutable types. .NET objects classify as mutable types (which can be edited in-place), and are used to provide an object-oriented programming model. Immutable types (editing which creates a new instance without overwriting the older one) are primarily used for functional programming.

Like ML, F# includes a functional programming component supporting eager evaluation. For functional programming, it provides several constructs and a set of immutable types: tuples, records, discriminated unions and lists. A tuple represents a collection of more than one values. It is represented as, where A and B can be of any type. A tuple can be used only to store values when the number of values is known at design-time and stays constant throughout execution. A record is a specialization of tuple where the data members are named, as in. Records can be created as  The   keyword is used to create a copy of a record, as in   creates a new record that by changing the value of the   field in record   (assuming the record created in the last example was named  ). The list type is a regular linked list represented either using a  notation (composition using the   -   - operator) or a shorthand as. An empty list is defined as. A discriminated union (which is functionally like a type-safe version of C union) can be defined to hold a value of any of a pre-defined type. For example, can hold values as instantiated by either type constructor. The type of the values the type constructors will act on can be defined as well. Type constructors are used to create a view of the data type different from the actual implementation, as required for supporting the Active Patterns concept. Data types are created with the  keyword. F# uses the  keyword for binding type values to a name (variable).

F# uses pattern matching to resolve names into values. It is also used when accessing discriminated unions. Functions using a discriminated union provide different expressions to be invoked, associated with the data type in the union. The union is matched against these data types, and the expression associated with the match is invoked. F# also supports the Active Patterns pattern. It is used, for example, when a type needs to provide multiple views. For example, an exponential number will provide both the final value, as well as the base and exponents.

All functions in F# are instances of the  type, and are immutable as well. Functions can either be curried or in uncurried form. Being an instance of a type, functions can be passed as arguments to other functions, resulting in higher order functions. F# supports lambda functions and closures as well. Like other functional programming languages, F# allows function composition using the  operator. Every statement and F#, including if-statements and loops, are composable expressions with a definite return type as well. Functions and expressions that do not return any value have a return type of.

The F# extended type system is implemented as generic .NET types. The Record type creates a .NET class with the field names as properties. Tuples are generic classes of type Tuple<_,_>. the number of type parameters define the number and types of the elements in the typle. Discriminated unions are implemented as tagged unions. Functions are of type  with type parameters specifying the parameter and return types.

F#, like other .NET languages, can use .NET types and objects, using an imperative object-oriented style of programming. For imperative programming, F# supports  and   loops, arrays (created with the   syntax, and number sequences written in shorthand as in  ) and support for creating   (equivalent to .NET classes). F# also allows extending the syntax to support embedding custom domain specific languages within the F# language itself.

F# provides sequence expressions that allows for a defining a sequence block ( or   or  ) encapsulating constructs (either functions, conditional expressions or loops) that act on a collection such that the results and another function (or lambda), such that the function is invoked on the results yielded from the collection collection-processing expressions. For example,  is a sequence expression that forms a list of squares of numbers from 0 to 14 by filtering out numbers from an array of 0 to 25. The sequence is lazily evaluated, i.e., the collection is processed and results yielded on-demand. It can be used for filtering and is the basis of support for LINQ queries. Sequence expressions are generalized as Computation Expressions which are equivalent to monads.

Sequence expressions and computation expressions are also used for creating asynchronous workflows. An asynchronous workflow is defined as a sequence of commands inside a, as in The   allows the rest of the async block to be defined as the delegate and passed as the callback function of an asynchronous operation. This helps deal with inversion of control issues. The async block is invoked using the  function. Multiple async blocks are executed parallelly using the  function that takes a list of   objects (in the example,   is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using.

Examples
A few small samples follow:

(* This is a comment *) (* Sample hello world program *) printf "Hello World!"

open Microsoft.FSharp.Collection.List (* print a list of numbers recursively *) let rec printlist lst = (* When using "#light", you must indent with 4 spaces *) if lst = [] then else printf "%d\n" (nth lst 0) printlist (tl lst)
 * 1) light

(* Same thing, using matching against list elements *) let rec printlist l = match l with | []    ->     | h :: t -> printfn "%A" h                printlist t

(* Fibonacci Number formula *) let rec fib n = match n with | 0    -> 0    | 1 | 2 -> 1    | n     -> (fib (n - 1)) + (fib (n - 2))
 * 1) light

(* Print even fibs *) [1 .. 10]
 * > List.map    (fun n -> fib n)
 * > List.filter (fun n -> (n mod 2) = 0)
 * > printlist

(* Same thing, using Comprehension syntax *) [ for i in 1..10 do   let r = fib i    if r % 2 = 0 then yield r ]
 * > printlist

(* Sample Windows Forms Program *)
 * 1) light

(* We need to open the Windows Forms library *) open System.Windows.Forms

(* Create a window and set a few properties *) let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")

(* Create a label to show some text in the form *) let label = let temp = new Label let x = 3 + (4 * 5) (* Set the value of the Text*) temp.Text <- sprintf "x = %d" x   (* Remember to return a value! *) temp

(* Add the label to the form *) do form.Controls.Add(label)

(* Finally, run the form *) do Application.Run(form)

F# is compatible with the XNA Game Studio Express product, which permits construction of games for Windows and for the Xbox 360.