F#
- Pronunciation
- EF-SHARP
In short
F# is a functional-first, statically typed language for .NET that pairs concise, type-inferred code with full access to the .NET ecosystem and C# libraries.
What is F#?
F# is a general-purpose programming language designed by Don Syme at Microsoft Research and first released in 2005. It belongs to the ML family of languages and was strongly influenced by OCaml. F# runs on .NET, is open source and cross-platform, and is developed by Microsoft together with the F# Software Foundation community.
F# is functional-first, meaning values are immutable and functions are the main building blocks by default, but it also supports classes, interfaces and mutable state when needed. Type inference is strong enough that most code has no type annotations at all, yet everything is checked at compile time. Indentation defines blocks, the pipe operator |> chains transformations, and discriminated unions with pattern matching make it easy to model data that can take one of several shapes, such as a payment that is either a card, a bank transfer or cash.
F# is used for financial modeling, data analysis, scientific computing, domain-heavy backend services and scripting with .fsx files. Because it compiles to the same intermediate language as C#, an F# project can use any .NET library and be called from C# code. Features such as units of measure, which let the compiler catch mistakes like adding meters to seconds, show its focus on correctness.
F# is most often compared with C#, its sibling on .NET. C# is object-oriented first, with functional features added over time, and has a much larger community, while F# is functional first, more concise and makes immutability and exhaustive pattern matching the default. F# is also compared with Haskell: both are statically typed functional languages, but F# evaluates eagerly and allows side effects anywhere, which makes it more pragmatic and easier to mix with existing .NET code.
Key takeaways
- F# is a functional-first, statically typed language that runs on .NET.
- Strong type inference means most code needs no type annotations.
- Discriminated unions and pattern matching model data clearly and safely.
- It interoperates fully with C# and the rest of the .NET ecosystem.
- Units of measure let the compiler catch unit mix-ups in calculations.
Example
// A discriminated union: a payment is exactly one of these cases
type Payment =
| Card of number: string
| BankTransfer of iban: string
| Cash
let describe payment =
match payment with
| Card n -> sprintf "Card ending in %s" (n.Substring(n.Length - 4))
| BankTransfer iban -> sprintf "Transfer from %s" iban
| Cash -> "Paid in cash"
[ Card "0000111122223333"; Cash ]
|> List.map describe
|> List.iter (printfn "%s") // Card ending in 3333, then Paid in cashReaders ask
What is the difference between F# and C#?
Both run on .NET and can use the same libraries. C# is object-oriented first with a C-style syntax, while F# is functional first, uses indentation instead of braces, and makes immutability, type inference and pattern matching the default style.
Is F# still maintained?
Yes. F# is open source, ships with the .NET SDK and receives a new version alongside each major .NET release.
Can I use F# and C# in the same solution?
Yes. They compile to the same .NET intermediate language, so an F# project can reference C# projects and the other way around, which lets teams use F# for specific parts such as domain logic or data processing.
See also
- C#Programming Languages, p. 4C# is a statically typed, object-oriented language that runs on the .NET runtime with garbage collection and is used for web backends, desktop apps, and games.
- Functional ProgrammingProgramming Fundamentals, p. 22Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
- Type InferenceProgramming Fundamentals, p. 55Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.
- HaskellProgramming Languages, p. 14Haskell is a purely functional, statically typed language with lazy evaluation, known for its expressive type system and mathematically precise code.
- ImmutabilityProgramming Fundamentals, p. 27Immutability means a value cannot be changed after it is created, so every update produces a new value instead of modifying the original in place.
- Pure FunctionProgramming Fundamentals, p. 47A pure function always returns the same output for the same input and has no side effects, meaning it does not change anything outside itself.
Spotted a mistake or something missing on this page?Suggest an edit