ArticleslgStudy

computer science

F Sharp (programming language)

F Sharp (programming language) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand F Sharp (programming language) rather than just read about it. In short: F# (pronounced F sharp) is a general-purpose, high-level, strongly typed, functional-first 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 Sharp (programming language) — main illustration
F Sharp (programming language) — illustration

Key takeaways

  • F Sharp (programming language) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect F Sharp (programming language) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of F Sharp (programming language) from memory before moving on to harder problems.

Reference excerpt

F# (pronounced F sharp) is a general-purpose, high-level, strongly typed, functional-first 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 (.NET) and C# (albeit as an option), and remains in all later 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 F# is a 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 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:

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:

Records can be created as let r = { Name="AB"; Age=42 }. The with keyword is used to create a copy of a record, as in { r with Name="CD" }, 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,

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 head::tail notation (:: is the cons operator) or a shorthand as [item1; item2; item3]. 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 sequence expressions that define a sequence seq { ... }, list [ ... ] or array [| ... |] through code that generates values. For example,

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 computation expressions. 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:

… excerpt ends here. Continue reading the full article.

Illustrations

F Sharp (programming language) illustration

Worked examples

Example 1 — a first encounter with F Sharp (programming language)

Start with the simplest possible case. Write down what F Sharp (programming language) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to F Sharp (programming language) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about F Sharp (programming language) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of F Sharp (programming language)

In research
F Sharp (programming language) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses F Sharp (programming language) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
F Sharp (programming language) is common in secondary-school and first-year university syllabi. It links to neighbouring topics .NET programming languages, Cross-platform free software, Functional languages, so understanding it makes those chapters shorter.
In everyday life
Look for F Sharp (programming language) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “F Sharp (programming language)” →

How to study F Sharp (programming language) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what F Sharp (programming language) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain F Sharp (programming language) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is F Sharp (programming language) in simple terms?

F# (pronounced F sharp) is a general-purpose, high-level, strongly typed, functional-first 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…

Why does F Sharp (programming language) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study F Sharp (programming language)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on F Sharp (programming language).

Tags

  • .NET programming languages
  • Cross-platform free software
  • Functional languages
  • High-level programming languages
  • ML programming language family
  • Microsoft Research
  • Microsoft free software
  • Microsoft programming languages
  • OCaml programming language family
  • Pattern matching programming languages
  • Programming languages created in 2005
  • Programming languages supporting units of measure

Keep exploring