Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
F Sharp (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language overview== ===Functional programming=== F# is a [[Strong and weak typing|strongly typed]] functional-first language with a large number of capabilities that are normally found only in [[functional programming]] languages, while supporting object-oriented features available in C#. 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 system|Hindley–Milner type inference]]) * [[First-class function|Functions as first-class citizens]] * [[Anonymous function]]s with capturing semantics (i.e., [[Closure (computer science)|closures]]) * Immutable variables and objects * [[Lazy evaluation]] support * [[Higher-order function]]s * Nested functions * [[Currying]] * [[Pattern matching]] * [[Algebraic data types]] * [[Tuple (computer science)|Tuples]] * [[List comprehension]] * [[Monad (functional programming)|Monad]] pattern support (called ''computation expressions''<ref>{{Cite web|title=F Sharp Programming/Computation Expressions - Wikibooks, open books for an open world|url=https://en.wikibooks.org/wiki/F_Sharp_Programming/Computation_Expressions|access-date=2022-01-21|website=en.wikibooks.org|language=en}}</ref>) * [[Tail call|Tail call optimisation]]<ref>{{Cite web |last=kexugit |title=Tail calls in F# |url=https://docs.microsoft.com/en-us/archive/blogs/fsharpteam/tail-calls-in-f |access-date=2022-04-22 |website=docs.microsoft.com |date=8 July 2011 |language=en-us}}</ref> F# is an expression-based language using [[eager evaluation]] and also in some instances [[lazy evaluation]]. Every statement in F#, including <code>if</code> expressions, <code>try</code> expressions and loops, is a composable expression with a static type.<ref name="overview"/> Functions and expressions that do not return any value have a return type of <code>unit</code>. F# uses the <code>let</code> keyword for binding values to a name.<ref name="overview">{{cite web |url=http://tomasp.net/articles/fsharp-i-introduction/article.pdf |title=F# Language Overview |access-date=2007-12-14}}</ref> For example: <syntaxhighlight lang="fsharp"> let x = 3 + 4 </syntaxhighlight> binds the value <code>7</code> to the name <code>x</code>. New types are defined using the <code>type</code> keyword. For functional programming, F# provides ''tuple'', ''record'', ''discriminated union'', ''list'', ''option'', and ''result'' types.<ref name="overview"/> A ''[[n-tuple|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 <code>(A, B, C)</code>, 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: <syntaxhighlight lang="fsharp"> type R = { Name : string Age : int } </syntaxhighlight> Records can be created as {{code|lang=fsharp|code=let r = { Name="AB"; Age=42 } }}. The <code>with</code> keyword is used to create a copy of a record, as in {{code|lang=fsharp|code={ r with Name="CD" } }}, which creates a new record by copying <code>r</code> and changing the value of the <code>Name</code> field (assuming the record created in the last example was named <code>r</code>). A [[tagged union|discriminated union]] type is a [[type-safe]] version of [[union (computer science)|C unions]]. For example, <syntaxhighlight lang="fsharp"> type A = | UnionCaseX of string | UnionCaseY of int </syntaxhighlight> 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 {{code|lang=fsharp|code=head::tail}} notation (<code>::</code> is the [[cons]] operator) or a shorthand as {{code|lang=fsharp|code=[item1; item2; item3]}}. An empty list is written <code>[]</code>. The ''option'' type is a discriminated union type with choices <code>Some(x)</code> or <code>None</code>. F# types may be [[generic programming|generic]], implemented as generic .NET types. F# supports [[lambda calculus|lambda functions]] and [[closure (computing)|closure]]s.<ref name="overview"/> All functions in F# are first class values and are immutable.<ref name="overview"/> Functions can be [[curry function|curried]]. Being first-class values, functions can be passed as arguments to other functions. Like other functional programming languages, F# allows [[function composition (computer science)|function composition]] using the <code>>></code> and <code><<</code> operators. F# provides ''{{visible anchor|sequence expressions}}''<ref name="seq"/> that define a sequence <code>seq { ... }</code>, list <code>[ ... ]</code> or array <code>[| ... |]</code> through code that generates values. For example, <syntaxhighlight lang="fsharp"> seq { for b in 0 .. 25 do if b < 15 then yield b*b } </syntaxhighlight> 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 [[Generator (computer programming)|generators]] – values are generated on-demand (i.e., are [[lazy evaluation|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.<ref name="activePatterns"/> It is used, for example, when multiple ways of matching on a type exist.<ref name="overview"/> F# supports a general syntax for defining compositional computations called ''{{visible anchor|computation expressions}}''. Sequence expressions, asynchronous computations and queries are particular kinds of computation expressions. Computation expressions are an implementation of the [[monad (functional programming)|monad]] pattern.<ref name="seq">{{cite web |url=http://blogs.msdn.com/dsyme/archive/2007/09/22/some-details-on-f-computation-expressions-aka-monadic-or-workflow-syntax.aspx |title=Some Details on F# Computation Expressions |access-date=2007-12-14}}</ref> ===Imperative programming=== F# support for imperative programming includes * <code>for</code> [[Control flow#Loops|loops]] * <code>while</code> [[Control flow#Loops|loops]] * [[array data structure|arrays]], created with the <code>[| ... |]</code> syntax * [[Associative array|hash table]], created with the <code>dict [ ... ]</code> syntax or <code>System.Collections.Generic.Dictionary<_,_></code> type. Values and record fields can also be labelled as <code>mutable</code>. For example: <syntaxhighlight lang="fsharp"> // Define 'x' with initial value '1' let mutable x = 1 // Change the value of 'x' to '3' x <- 3 </syntaxhighlight> Also, F# supports access to all CLI types and objects such as those defined in the <code>System.Collections.Generic</code> namespace defining imperative data structures. ===Object-oriented programming=== Like other [[Common Language Infrastructure]] (CLI) languages, F# can use CLI types through object-oriented programming.<ref name="overview"/> F# support for object-oriented programming in expressions includes: * Dot-notation, e.g., {{code|lang=fsharp|code=x.Name}} * Object expressions, e.g., {{ code|lang=fsharp| code={ new obj() with member x.ToString() = "hello" } }} * Object construction, e.g., {{code|lang=fsharp|code=new Form()}} * Type tests, e.g., {{code|lang=fsharp|code=x :? string}} * Type coercions, e.g., {{code|lang=fsharp|code=x :?> string}} * Named arguments, e.g., {{code|lang=fsharp|code=x.Method(someArgument=1)}} * Named setters, e.g., {{code|lang=fsharp|code=new Form(Text="Hello")}} * Optional arguments, e.g., {{code|lang=fsharp|code=x.Method(OptionalArgument=1)}} Support for object-oriented programming in patterns includes * Type tests, e.g., {{code|lang=fsharp|code=:? string as s}} * Active patterns, which can be defined over object types<ref name="activePatterns">{{cite web |url=http://www.developerfusion.com/article/133772/pattern-matching-in-f-part-2-active-patterns/ |title=Pattern Matching in F# Part 2 : Active Patterns |access-date=2012-11-24 |archive-date=2012-12-09 |archive-url=https://web.archive.org/web/20121209125518/http://www.developerfusion.com/article/133772/pattern-matching-in-f-part-2-active-patterns |url-status=dead}}</ref> F# object type definitions can be class, struct, interface, enum, or delegate type definitions, corresponding to the definition forms found in [[C Sharp (programming language)|C#]]. For example, here is a class with a constructor taking a name and age, and declaring two properties. <syntaxhighlight lang="fsharp"> /// A simple object type definition type Person(name : string, age : int) = member x.Name = name member x.Age = age </syntaxhighlight> ===Asynchronous programming=== F# supports [[asynchronous programming]] through ''asynchronous workflows''.<ref name="aw"/> An asynchronous workflow is defined as a sequence of commands inside an <code>async{ ... }</code>, as in <syntaxhighlight lang="fsharp"> 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() } </syntaxhighlight> The <code>let!</code> 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 until the result needed for this one becomes available. The async block may be invoked using the <code>Async.RunSynchronously</code> function. Multiple async blocks can be executed in parallel using the <code>Async.Parallel</code> function that takes a list of <code>async</code> objects (in the example, <code>asynctask</code> is an async object) and creates another async object to run the tasks in the lists in parallel. The resultant object is invoked using <code>Async.RunSynchronously</code>.<ref name="aw">{{cite web |url=http://blogs.msdn.com/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx |title=Introducing F# Asynchronous Workflows |access-date=2007-12-14}}</ref> [[Inversion of control]] in F# follows this pattern.<ref name="aw"/> Since version 6.0, F# supports creating, consuming and returning .NET tasks directly. <ref>{{cite web |url=https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/task-expressions | title=Task Expressions| date=19 April 2022|access-date=2023-01-15}}</ref> <syntaxhighlight lang="fsharp"> open System.Net.Http let fetchUrlAsync (url:string) = // string -> Task<string> task { use client = new HttpClient() let! response = client.GetAsync(url) let! content = response.Content.ReadAsStringAsync() do! Task.Delay 500 return content } // Usage let fetchPrint() = let task = task { let! data = fetchUrlAsync "https://example.com" printfn $"{data}" } task.Wait() </syntaxhighlight> ===Parallel programming=== Parallel programming is supported partly through the <code>Async.Parallel</code>, <code>Async.Start</code> and other operations that run asynchronous blocks in parallel. Parallel programming is also supported through the <code>Array.Parallel</code> functional programming operators in the F# standard library, direct use of the <code>System.Threading.Tasks</code> 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 [[Graphics processing unit|GPU]]<ref name="gpgpu"/> code. ===Units of measure=== The F# type system supports [[Units of measurement|units of measure]] checking for numbers.<ref name="units-msdn">{{cite web |url=http://msdn.microsoft.com/en-us/library/dd233243.aspx |title=Units of Measure (F#) |access-date=2012-11-24}}</ref> In F#, you can assign units of measure, such as meters or kilograms, to floating point, unsigned integer<ref name="units extended">{{cite web|url=https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1091-Extend-Units-of-Measure.md| title=Extend Units of Measure to Include More Numeric Types | website=[[GitHub]] }}</ref> and signed integer values. This allows the compiler to check that arithmetic involving these values is dimensionally consistent, helping to prevent common programming mistakes by ensuring that, for instance, lengths aren't mistakenly added to times. The units of measure feature integrates with F# type inference to require minimal type annotations in user code.<ref name="units">{{cite web |url=http://blogs.msdn.com/b/andrewkennedy/archive/2008/08/29/units-of-measure-in-f-part-one-introducing-units.aspx |title=Units of Measure in F#: Part One, Introducing Units |access-date=2012-11-24}}</ref> <syntaxhighlight lang="fsharp"> [<Measure>] type m // meter [<Measure>] type s // second let distance = 100.0<m> // float<m> let time = 5.0<s> // float<s> let speed = distance/time // float<m/s> [<Measure>] type kg // kilogram [<Measure>] type N = (kg * m)/(s^2) // Newtons [<Measure>] type Pa = N/(m^2) // Pascals [<Measure>] type days let better_age = 3u<days> // uint<days> </syntaxhighlight> The F# static type checker provides this functionality at compile time, but units are erased from the compiled code. Consequently, it is not possible to determine a value's unit at runtime. ===Metaprogramming=== F# allows some forms of syntax customizing via [[metaprogramming]] to support embedding custom [[domain-specific language]]s within the F# language, particularly through computation expressions.<ref name="overview"/> F# includes a feature for run-time meta-programming called quotations.<ref name="quotations">{{cite web |url=http://msdn.microsoft.com/en-us/library/dd233212.aspx |title=Code Quotations (F#) |access-date=2012-11-24}}</ref> A quotation expression evaluates to an abstract syntax tree representation of the F# expressions. Similarly, definitions labelled with the <code>[<ReflectedDefinition>]</code> attribute can also be accessed in their quotation form. F# quotations are used for various purposes including to compile F# code into [[JavaScript]]<ref name="js"/> and [[Graphics processing unit|GPU]]<ref name="gpgpu"/> 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.<ref name="typeproviders">{{cite web |url=http://msdn.microsoft.com/en-us/library/hh156509.aspx |title=Type Providers |access-date=2012-11-24}}</ref> 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 (database)|Freebase]] knowledge graph.<ref>{{cite web |url=http://blogs.msdn.com/b/dsyme/archive/2012/09/21/new-tech-report-from-microsoft-research-strongly-typed-language-support-for-internet-scale-information-sources.aspx |title=New Tech Report from Microsoft Research: Strongly-Typed Language Support for Internet-Scale Information Sources |access-date=2012-11-24}}</ref> In F# 3.0 the F# quotation and computation expression features are combined to implement [[LINQ]] queries.<ref name="queries">{{cite web |url=http://msdn.microsoft.com/en-us/library/vstudio/hh225374.aspx |title=Query Expressions (F#) |access-date=2012-11-24}}</ref> For example: <syntaxhighlight lang="fsharp"> // 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 } </syntaxhighlight> The combination of type providers, queries and strongly typed functional programming is known as ''information rich programming''.<ref name="irp">{{cite web |url=http://www.infoq.com/news/2011/09/Fsharp-3.0 |title=F# 3.0 – LINQ + Type Providers= Information Rich Programming |access-date=2012-11-24}}</ref> ===Agent programming=== F# supports a variation of the [[Actor model|actor]] programming model through the in-memory implementation of lightweight asynchronous agents. For example, the following code defines an agent and posts 2 messages: <syntaxhighlight lang="fsharp"> type Message = | Enqueue of string | Dequeue of AsyncReplyChannel<Option<string>> // Provides concurrent access to a list of strings let listManager = MailboxProcessor.Start(fun inbox -> let rec messageLoop list = async { let! msg = inbox.Receive() match msg with | Enqueue item -> return! messageLoop (item :: list) | Dequeue replyChannel -> match list with | [] -> replyChannel.Reply None return! messageLoop list | head :: tail -> replyChannel.Reply (Some head) return! messageLoop tail } // Start the loop with an empty list messageLoop [] ) // Usage async { // Enqueue some strings listManager.Post(Enqueue "Hello") listManager.Post(Enqueue "World") // Dequeue and process the strings let! str = listManager.PostAndAsyncReply(Dequeue) str |> Option.iter (printfn "Dequeued: %s") } |> Async.Start </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)