F #

F# is a functional programming language for the .NET Framework. It combines the succinct, expressive, and compositional style of functional programming with the runtime, libraries, interoperability, and object model of .NET. F# stems from the ML family of languages and has a core language compatible with that of OCaml, though also draws from C# and Haskell. It runs on Microsoft's Common Language Runtime and the .NET Framework. This means that F# has access to .NET Framework APIs and conversely, that other .NET languages can use F# libraries. For example, C# and F# can call each other directly. Similarly, libraries developed in F# may be used from other .NET languages.

F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and will be distributed as a fully supported language in the .NET Framework and Visual Studio ecosystem.

Unlike other scripting languages it executes at or near the speed of C# and C++, making use of the performance that comes through strong typing. Unlike many type-inferred, statically-typed languages it also supports many dynamic language techniques, such as property discovery and reflection where needed. F# includes extensions for working across languages and for object-oriented programming, and it works seamlessly with other .NET programming languages and tools.

F# is a strongly-typed language that uses type inference. As a result, data types need not be explicitly declared by the programmer; they 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 value. It is represented as (A, B), 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 { Name:string; Age:int }. Records can be created as { Name="AB"; Age=32 } The with keyword is used to create a copy of a record, as in { r with Name="CD" } creates a new record that by changing the value of the Name field in record r (assuming the record created in the last example was named r). The list type is a regular linked list represented either using a head::tail notation (composition using the ::-cons operator) or a shorthand as [item1; item2; item3]. An empty list is defined as [].

Since F# and OCaml share a similar core language, some OCaml libraries and applications can cross-compile either directly or with minor conditionally-compiled changes. This provides a path to cross-compile and/or port existing OCaml code to .NET and also allows programmers to transfer skills between these languages. A major focus of the project has been to extend the reach of OCaml-like languages into arenas where they have not traditionally been used. Throughout the project the designers of F# are grateful for the support and encouragement of Xavier Leroy and others in the OCaml community.


F# as a Language:

  • F# includes support for the foundational features of functional programming including tuples, lists, options, function values, local function definitions, pattern matching and sequence expressions.

  • The powerful type inference mechanisms of F# allow code to be both succinct and yet fully type-checked.

  • F# also includes support for advanced functional programming constructs such as active patterns and computation expressions. Computation expressions can be used to express data queries and client/server modalities in AJAX-style web programming. They enable programmers to write succinct and robust reactive agents through the use of asynchronous workflows. Computation expressions are related to "monads" in Haskell.

  • F# embraces object-oriented programming and includes support for type-inferred, succinct descriptions of object types.

  • F# allows types and values in an F# program to be accessed from other .NET languages in a predictable and friendly way.

  • F# includes support for a form of meta-programming, inspired by LINQ. This allows data queries to be expressed and type-checked in F# code and then dynamically compiled and translated to target languages such as SQL using the LinqToSql framework.

  • F# fully supports .NET generics and the language was designed partly with this in mind.

  • Through .NET, F# supports advanced language and runtime features such as Unicode strings, dynamic linking, preemptive multithreading, and SMP support.

F# for Developers:

  • The F# Interactive environment fsi.exe supports top-level development and exploration of the dynamics of your code and environment.

  • The command line compiler fsc.exe supports separate compilation, debug information and optimization.

  • F# comes with F# for Visual Studio, an extension to Visual Studio 2005 that supports features such as an integrated build/debug environment, graphical debugging, interactive syntax highlighting, parsing and typechecking, IntelliSense, CodeSense, MethodTips and a project system.

  • F# can be used with tools from the .NET Framework, Microsoft's Visual Studio and many other .NET development tools.

  • F# comes with an ML compatibility library that approximates the OCaml 3.06 libraries. This means you don't have to use .NET libraries if it is not appropriate. It is possible to write large and sophisticated applications that can be cross-compiled as OCaml code or F# code, and we take this mode of use seriously.