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!
===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>
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)