F Sharp (programming language)

F Sharp (programming language)
F# (pronounced F sharp) is a functional-first, general-purpose, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods. It is most often used as a cross-platform Common Language Infrastructure (CLI) language on .NET, but can also generate JavaScript and graphics processing unit (GPU) code.
F# is developed by the F# Software Foundation, Microsoft and open contributors. An open source, cross-platform compiler for F# is available from the F# Software Foundation. F# is a fully supported language in Visual Studio and JetBrains Rider. Plug-ins supporting F# exist for many widely used editors including Visual Studio Code, Vim, and Emacs.
F# is a member of the ML language family and originated as a .NET Framework implementation of a core of the programming language OCaml. It has also been influenced by C#,
Python, Haskell, Scala and Erlang.
History
Versions
Language evolution
F# uses an open development and engineering process.
The language evolution process is managed by Don Syme from Microsoft Research as the benevolent dictator for life (BDFL) for the language design, together with the F# Software Foundation.
Earlier versions of the F# language were designed by Microsoft and Microsoft Research using a closed development process.
F# was first included in Visual Studio in the 2010 edition, at the same level as Visual Basic and C# (albeit as an option), and has remained in subsequent editions, thus making the language widely available and well-supported.
F# originates from Microsoft Research, Cambridge, UK. The language was originally designed and implemented by Don Syme, according to whom in the fsharp team, they say the F is for "Fun".
Andrew Kennedy contributed to the design of units of measure. The Visual F# Tools for Visual Studio are developed by Microsoft. The F# Software Foundation developed the F# open-source compiler and tools, incorporating the open-source compiler implementation provided by the Microsoft Visual F# Tools team.
Language overview
Functional programming
While supporting object-oriented features available in C#, F# is a strongly typed functional-first language with a large number of capabilities that are normally found only in functional programming languages. Together, these features allow F# programs to be written in a completely functional style and also allow functional and object-oriented styles to be mixed.
Examples of functional features are:
Everything is an expression
Type inference (using Hindley–Milner type inference)
Functions as first-class citizens
Anonymous functions with capturing semantics (i.e., closures)
Immutable variables and objects
Lazy evaluation support
Higher-order functions
Nested functions
Currying
Pattern matching
Algebraic data types
Tuples
List comprehension
Monad pattern support (called computation expressions)
Tail Call Optimisation
F# is an expression-based language using eager evaluation and also in some instances lazy evaluation. Every statement in F#,
including if expressions, try expressions and loops, is a composable expression with a static type. Functions and expressions that do not return any value have a return type of unit. F# uses the let keyword for binding values to a name. For example:
let x = 3 + 4
binds the value 7 to the name x.
New types are defined using the type keyword. For functional programming, F# provides tuple, record, discriminated union, list, option, and result types. A tuple represents a set of n values, where n ≥ 0. The value n is called the arity of the tuple. A 3-tuple would be represented as (A, B, C), where A, B, and C are values of possibly different types. A tuple can be used to store values only when the number of values is known at design-time and stays constant during execution.
A record is a type where the data members are named. Here is an example of record definition:
type R =
{ Name : string
Age : int }
Records can be created as }. The with keyword is used to create a copy of a record, as in }, which creates a new record by copying r and changing the value of the Name field (assuming the record created in the last example was named r).
A discriminated union type is a type-safe version of C unions. For example,
type A =
| UnionCaseX of string
| UnionCaseY of int
Values of the union type can correspond to either union case. The types of the values carried by each union case is included in the definition of each case.
The list type is an immutable linked list represented either using a notation (:: is the cons operator) or a shorthand as . An empty list is written []. The option type is a discriminated union type with choices Some(x) or None. F# types may be generic, implemented as generic .NET types.
F# supports lambda functions and closures. All functions in F# are first class values and are immutable. Functions can be curried. Being first-class values, functions can be passed as arguments to other functions. Like other functional programming languages, F# allows function composition using the >> and << operators.
F# provides that define a sequence seq { ... }, list [ ... ] or array [| ... |] through code that generates values. For example,
seq { for b in 0 .. 25 do
if b < 15 then
yield b*b }
forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25. Sequences are generators – values are generated on-demand (i.e., are lazily evaluated) – while lists and arrays are evaluated eagerly.
F# uses pattern matching to bind values to names. Pattern matching is also used when accessing discriminated unions – the union is value matched against pattern rules and a rule is selected when a match succeeds. F# also supports Active Patterns as a form of extensible pattern matching. It is used, for example, when multiple ways of matching on a type exist.
F# supports a general syntax for defining compositional computations called . Sequence expressions, asynchronous computations and queries are particular kinds of computation expressions. Computation expressions are an implementation of the monad pattern.
Imperative programming
F# support for imperative programming includes
for loops
while loops
arrays, created with the [| ... |] syntax
hash table, created with the dict [ ... ] syntax or System.Collections.Generic.Dictionary<_,_> type.
Values and record fields can also be labelled as mutable. For example:
// Define 'x' with initial value '1'
let mutable x = 1
// Change the value of 'x' to '3'
x <- 3
Also, F# supports access to all CLI types and objects such as those defined in the System.Collections.Generic namespace defining imperative data structures.
Object-oriented programming
Like other Common Language Infrastructure (CLI) languages, F# can use CLI types through object-oriented programming. F# support for object-oriented programming in expressions includes:
Dot-notation, e.g.,
Object expressions, e.g., }
Object construction, e.g.,
Type tests, e.g.,
Type coercions, e.g.,
Named arguments, e.g.,
Named setters, e.g.,
Optional arguments, e.g.,
Support for object-oriented programming in patterns includes
Type tests, e.g.,
Active patterns, which can be defined over object types
F# object type definitions can be class, struct, interface, enum, or delegate type definitions, corresponding to the definition forms found in C#. For example, here is a class with a constructor taking a name and age, and declaring two properties.
/// A simple object type definition
type Person(name : string, age : int) =
member x.Name = name
member x.Age = age
Asynchronous programming
F# supports asynchronous programming through asynchronous workflows. An asynchronous workflow is defined as a sequence of commands inside an async{ ... }, as in
let asynctask =
async { let req = WebRequest.Create(url)
let! response = req.GetResponseAsync()
use stream = response.GetResponseStream()
use streamreader = new System.IO.StreamReader(stream)
return streamreader.ReadToEnd() }
The let! indicates that the expression on the right (getting the response) should be done asynchronously but the flow should only continue when the result is available. In other words, from the point of view of the code block, it's as if getting the response is a blocking call, whereas from the point of view of the system, the thread won't be blocked and may be used to process other flows while the result needed for this one doesn't become available.
The async block may be invoked using the Async.RunSynchronously function. Multiple async blocks can be executed in parallel using the Async.Parallel function that takes a list of async objects (in the example, asynctask is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using Async.RunSynchronously.
Inversion of control in F# follows this pattern.
Parallel programming
Parallel programming is supported partly through the Async.Parallel, Async.Start and other operations that run asynchronous blocks in parallel.
Parallel programming is also supported through the Array.Parallel functional programming operators in the F# standard library, direct use of the System.Threading.Tasks task programming model, the direct use of .NET thread pool and .NET threads and through dynamic translation of F# code to alternative parallel execution engines such as GPU code.
Units of measure
The F# type system supports units of measure checking for numbers. The units of measure feature integrates with F# type inference to require minimal type annotations in user code.
Metaprogramming
F# allows some forms of syntax customizing via metaprogramming to support embedding custom domain-specific languages within the F# language, particularly through computation expressions.
F# includes a feature for run-time meta-programming called quotations. A quotation expression evaluates to an abstract syntax tree representation of the F# expressions. Similarly, definitions labelled with the [<ReflectedDefinition>] attribute can also be accessed in their quotation form. F# quotations are used for various purposes including to compile F# code into JavaScript and GPU code. (Quotations represent their F# code expressions as data for use by other parts of the program while requiring it to be syntactically correct F# code).
Information-rich programming
F# 3.0 introduced a form of compile-time meta-programming through statically extensible type generation called F# type providers. F# type providers allow the F# compiler and tools to be extended with components that provide type information to the compiler on-demand at compile time. F# type providers have been used to give strongly typed access to connected information sources in a scalable way, including to the Freebase knowledge graph.
In F# 3.0 the F# quotation and computation expression features are combined to implement LINQ queries. For example:
// Use the OData type provider to create types that can be used to access the Northwind database.
open Microsoft.FSharp.Data.TypeProviders
type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc">
let db = Northwind.GetDataContext()
// A query expression.
let query1 = query { for customer in db.Customers do
select customer }
The combination of type providers, queries and strongly typed functional programming is known as information rich programming.
Agent programming
F# supports a variation of the Actor programming model through the in-memory implementation of lightweight asynchronous agents. For example, the following code defines an agent and posts 2 messages:
let counter =
MailboxProcessor.Start(fun inbox ->
let rec loop n =
async { do printfn "n = %d, waiting..." n
let! msg = inbox.Receive()
return! loop(n+msg) }
loop 0)
Development tools
Visual Studio, with the Visual F# tools from Microsoft installed, can be used to create, run and debug F# projects. The Visual F# tools include a Visual Studio-hosted read–eval–print loop (REPL) interactive console that can execute F# code as it is written. Visual Studio for Mac also fully supports F# projects.
Visual Studio Code contains full support for F# via the Ionide extension.
F# can be developed with any text editor. Specific support exists in editors such as Emacs.
JetBrains Rider is optimized for the development of F# Code starting with release 2019.1.
LINQPad has supported F# since version 2.x.
Comparison of integrated development environments
Application areas
F# is a general-purpose programming language.
Web programming
The SAFE Stack is an end-to-end F# stack to develop web applications. It uses ASP.NET Core on the server side and Fable on the client side.
An alternative end-to-end F# option is the WebSharper framework.
Cross-platform app development
F# can be used together with the Visual Studio Tools for Xamarin to develop apps for iOS and Android. The Fabulous library provides a more comfortable functional interface.
Analytical programming
Among others, F# is used for quantitative finance programming, energy trading and portfolio optimization, machine learning, business intelligence and social gaming on Facebook.
In the 2010s, F# has been positioned as an optimized alternative to C#. F#'s scripting ability and inter-language compatibility with all Microsoft products have made it popular among developers.
Scripting
F# can be used as a scripting language, mainly for desktop read–eval–print loop (REPL) scripting.
Open-source community
The F# open-source community includes the F# Software Foundation and the F# Open Source Group at GitHub. Popular open-source F# projects include:
Fable, an F# to Javascript transpiler based on Babel.
Paket, an alternative package manager for .NET that can still use NuGet repositories, but has centralised version-management.
FAKE, an F# friendly build-system.
Giraffe, a functionally oriented middleware for ASP.NET Core.
Suave, a lightweight web-server and web-development library.
Compatibility
F# features a legacy "ML compatibility mode" that can directly compile programs written in a large subset of OCaml roughly, with no functors, objects, polymorphic variants, or other additions.
Examples
A few small samples follow:
// This is a comment for a sample hello world program.
printfn "Hello World!"
A Person class with a constructor taking a name and age and two immutable properties.
/// This is a documentation comment for a type definition.
type Person(name : string, age : int) =
member x.Name = name
member x.Age = age
/// class instantiation
let mrSmith = Person("Smith", 42)
A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative 32-bit integers, here shown in F#:
/// Using pattern matching expression
let rec factorial n =
match n with
| 0 -> 1
| _ -> n * factorial (n - 1)
/// For a single-argument functions there is syntactic sugar (pattern matching function):
let rec factorial = function
| 0 -> 1
| n -> n * factorial (n - 1)
/// Using fold and range operator
let factorial n = [1..n] |> Seq.fold (*) 1
Iteration examples:
/// Iteration using a 'for' loop
let printList lst =
for x in lst do
printfn "%d" x
/// Iteration using a higher-order function
let printList2 lst =
List.iter (printfn "%d") lst
/// Iteration using a recursive function and pattern matching
let rec printList3 lst =
match lst with
| [] ->
| h :: t ->
printfn "%d" h
printList3 t
Fibonacci examples:
/// Fibonacci Number formula
let fib n =
let rec g n f0 f1 =
match n with
| 0 -> f0
| 1 -> f1
| _ -> g (n - 1) f1 (f0 + f1)
g n 0 1
/// Another approach - a lazy infinite sequence of Fibonacci numbers
let fibSeq = Seq.unfold (fun (a,b) -> Some(a+b, (b, a+b))) (0,1)
// Print even fibs
[1 .. 10]
|> List.map fib
|> List.filter (fun n -> (n % 2) = 0)
|> printList
// Same thing, using a list expression
[ for i in 1..10 do
let r = fib i
if r % 2 = 0 then yield r ]
|> printList
A sample Windows Forms program:
// 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 x = 3 + (4 * 5)
new Label(Text = $"{x}")
// Add the label to the form
form.Controls.Add(label)
// Finally, run the form
[<System.STAThread>]
Application.Run(form)
Asynchronous parallel programming sample (parallel CPU and I/O tasks):
/// A simple prime number detector
let isPrime (n:int) =
let bound = int (sqrt (float n))
seq {2 .. bound} |> Seq.forall (fun x -> n % x <> 0)
// We are using async workflows
let primeAsync n =
async { return (n, isPrime n) }
/// Return primes between m and n using multiple threads
let primes m n =
seq {m .. n}
|> Seq.map primeAsync
|> Async.Parallel
|> Async.RunSynchronously
|> Array.filter snd
|> Array.map fst
// Run a test
primes 1000000 1002000
|> Array.iter (printfn "%d")
See also
OCaml
C#
.NET Framework
Notes
References
External links
The F# Software Foundation
The F# Open Source Group at GitHub
The Visual F# Developer Center
Tsunami, an embeddable desktop F# IDE
Cloud Tsunami, an embeddable Silverlight F# IDE
Try F#, for learning F# in a web browser
F# Snippets Site
The Visual F# team blog
The original Microsoft Research website for F#
Planet F#
The F# Survival Guide, Dec 2009 (Web-based book)
The F# Language Specification
An introduction to F# programming
A tutorial showing the process of reaching a functional design; includes test and parallel coding
.NET programming languages
Cross-platform free software
Functional languages
Microsoft free software
Microsoft programming languages
Microsoft Research
ML programming language family
OCaml programming language family
Pattern matching programming languages
Programming languages created in 2005
Programming languages supporting units of measure
Software using the Apache license
Software using the MIT license
Statically typed programming languages