What is F#
F# is an open-source, cross-platform, interoperable programming language for writing succinct, robust and performant code. Your focus remains on your problem domain, rather than the details of programming.
F# programming is data-oriented, where code involves transforming data with functions.
F#
open System
// Gets access to functionality in System namespace.
// Defines a list of names
let names = [
"Peter";
"Julia";
"Xi" ]
// Defines a function that takes a name and produces a
greeting.
let getGreeting name = $"Hello, {name}"
// Prints a greeting for each name!
names
|> List.map getGreeting
|> List.iter (fun greeting ->
printfn $"{greeting}! Enjoy your F#")
F# has numerous features, including:
- Lightweight syntax
- Immutable by default
- Type inference and automatic generalization
- First-class functions
- Powerful data types
- Pattern matching
- Async programming
Get Started
You can get started with F# on your machine or online.
Get started on your machine
There are multiple guides on how to install and use F# for the first time on your machine. You can use the following table to help in making a decision:
Get started online
If you'd rather not install F# and .NET on your machine, you can also get started with F# in the browser:
- Introduction to F# on Binder is a Jupyter notebook on hosted via the free Binder service. No sign-up needed!
- The Fable REPL is an interactive, in-browser REPL that uses Fable to translate F# code into JavaScript. Check out the numerous samples that range from F# basics to a fully fledged video game all executing in your browser!
Introduction to Functional Programming in F#
Functional programming is a style of programming that emphasizes the use of functions and immutable data. Typed functional programming is when functional programming is combined with static types, such as with F#. In general, the following concepts are emphasized in functional programming:
- Functions as the primary constructs you use
- Expressions instead of statements
- Immutable values over variables
- Declarative programming over imperative programming
Terminology
Functional programming, like other programming paradigms, comes with a vocabulary that you will eventually need to learn. Here are some common terms you'll see all of the time:
- Function - A function is a construct that will produce an output when given an input. More formally, it maps an item from one set to another set. This formalism is lifted into the concrete in many ways, especially when using functions that operate on collections of data. It is the most basic (and important) concept in functional programming.
- Expression - An expression is a construct in code that produces a value. In F#, this value must be bound or explicitly ignored. An expression can be trivially replaced by a function call.
- Purity - Purity is a property of a function such that its return value is always the same for the same arguments, and that its evaluation has no side effects. A pure function depends entirely on its arguments.
- Referential Transparency - Referential Transparency is a property of expressions such that they can be replaced with their output without affecting a program's behavior.
- Immutability - Immutability means that a value cannot be changed in-place. This is in contrast with variables, which can change in place.
Async programming in F#
Asynchronous programming is a mechanism that is essential to modern applications for diverse reasons. There are two primary use cases that most developers will encounter:
- Presenting a server process that can service a significant number of concurrent incoming requests, while minimizing the system resources occupied while request processing awaits inputs from systems or services external to that process
- Maintaining a responsive UI or main thread while concurrently progressing background work
Although background work often does involve the utilization of multiple threads, it's important to consider the concepts of asynchrony and multi-threading separately. In fact, they are separate concerns, and one does not imply the other. This article describes the separate concepts in more detail.
Asynchrony defined
The previous point - that asynchrony is independent of the utilization of multiple threads - is worth explaining a bit further. There are three concepts that are sometimes related, but strictly independent of one another:
- Concurrency; when multiple computations execute in overlapping time periods.
- Parallelism; when multiple computations or several parts of a single computation run at exactly the same time.
- Asynchrony; when one or more computations can execute separately from the main program flow.
All three are orthogonal concepts, but can be easily conflated, especially when they are used together. For example, you may need to execute multiple asynchronous computations in parallel. This relationship does not mean that parallelism or asynchrony imply one another.
If you consider the etymology of the word "asynchronous", there are two pieces involved:
- "a", meaning "not".
- "synchronous", meaning "at the same time".
When you put these two terms together, you'll see that "asynchronous" means "not at the same time". That's it! There is no implication of concurrency or parallelism in this definition. This is also true in practice.
In practical terms, asynchronous computations in F# are scheduled to execute independently of the main program flow. This independent execution doesn't imply concurrency or parallelism, nor does it imply that a computation always happens in the background. In fact, asynchronous computations can even execute synchronously, depending on the nature of the computation and the environment the computation is executing in.
The main takeaway you should have is that asynchronous computations are independent of the main program flow. Although there are few guarantees about when or how an asynchronous computation executes, there are some approaches to orchestrating and scheduling them. The rest of this article explores core concepts for F# asynchrony and how to use the types, functions, and expressions built into F#.
Credit
All the text in this page is from Microsoft Documentation.