ZetCode

Introduction to F#

last modified December 2, 2026

In this article we install F#, try F# Interactive, and write and run the first F# programs.

F# is a functional-first, statically typed language for the .NET platform. It was created at Microsoft Research by Don Syme and first released in 2005; today it is an open source language developed by Microsoft and the F# Software Foundation. Values are immutable by default, functions are ordinary values, and the compiler infers most types, so programs are short without losing type safety. Because F# runs on .NET, it can use every .NET library and mix functional code with objects and mutable state where they fit better.

Installing the .NET SDK

F# is part of the .NET SDK; there is no separate F# download. The SDK contains the F# compiler, the F# Interactive tool, and the dotnet command that drives them. Installers for Windows, macOS and Linux are available from the .NET website, and many Linux distributions package the SDK as well. After installation we check the version.

λ dotnet --version
11.0.100

F# Interactive

F# Interactive is a read-eval-print loop. It is started with dotnet fsi; we type an expression, end it with two semicolons, and F# Interactive evaluates it and prints the type and the value of the result. The #quit;; directive ends the session.

In the session below we bind the value 5 to the name x, compute an expression, and ask a string for its length. F# Interactive answers with val lines: the name, the type inferred by the compiler, and the value. A result that we have not named is called it.

λ dotnet fsi

Microsoft (R) F# Interactive version 15.2.101.0 for F# 11.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> let x = 5;;
val x: int = 5

> x * 3;;
val it: int = 15

> "falcon".Length;;
val it: int = 6

> #quit;;

The first program

The examples in this series are F# scripts. A script is a text file with the .fsx extension; F# Interactive compiles and runs it in one step, and no project file is needed.

first.fsx
printfn "Hello there!"

The program prints a message to the console. printfn is a function that prints formatted text followed by a new line. The function and its argument are separated by a space; F# does not put arguments in parentheses.

We run the script by passing its name to dotnet fsi. Throughout the series, the λ character stands for the shell prompt.

λ dotnet fsi first.fsx
Hello there!

Values and formatted output

The let keyword binds a value to a name. The format string of printfn contains placeholders that are replaced by the following arguments.

values.fsx
let name = "Lucia"
let age = 34

printfn "%s is %d years old" name age
printfn "Next year she will be %d" (age + 1)

We bind a string and an integer and print them.

let name = "Lucia"
let age = 34

F# infers that name is a string and age an int. The bindings are immutable; we cannot assign a new value to them later.

printfn "%s is %d years old" name age
printfn "Next year she will be %d" (age + 1)

The %s placeholder takes a string and %d an integer. The types are checked by the compiler: passing an integer for %s is a compile error. An argument that is an expression, like age + 1, must be put in parentheses; otherwise printfn would receive age and 1 as two arguments.

λ dotnet fsi values.fsx
Lucia is 34 years old
Next year she will be 35

Comments

Comments are ignored by the compiler. F# has single-line comments, multi-line comments, and documentation comments.

comments.fsx
// A single-line comment

(*
   A multi-line comment.
   It may span several lines.
*)

/// A documentation comment for the binding below
let speed = 120

printfn "Speed: %d km/h" speed // a comment at the end of a line

A single-line comment starts with // and runs to the end of the line; it can also follow code. A multi-line comment is placed between (* and *). A comment starting with /// documents the declaration that follows it; editors show it as a tooltip, and the compiler can collect it into an XML documentation file.

λ dotnet fsi comments.fsx
Speed: 120 km/h

Code layout

F# uses indentation to group code, like Python. The lines that belong to a definition are indented more than the line that starts it, with spaces; tab characters are not allowed. Each line holds one expression, so no semicolons are needed at the ends of lines. Indentation and blocks are covered in F# code blocks; the explicit syntax that does not depend on indentation is shown in F# verbose syntax.

Creating a project

Scripts are fine for small programs and experiments. Applications are built as projects, which are created with the dotnet new command. The dotnet new console -lang "F#" -o greeting command creates a console application in the greeting directory and downloads its dependencies.

The project has two files: the source file Program.fs and the project file greeting.fsproj. We replace the content of Program.fs with a single line.

Program.fs
printfn "Hello there from a project!"

A compiled F# file has the .fs extension. The code in the last file of a project is the entry point of the program.

greeting.fsproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

</Project>

The project file says that we build an executable for .NET 11. The Compile items list the source files. Unlike in most languages, their order matters: a file can only use code from the files listed before it. The dotnet run command compiles the project and runs it.

λ dotnet run
Hello there from a project!

Source

What is F#

Interactive programming with F#

Install .NET

In this article we have installed F#, used F# Interactive, and written our first scripts and project. The next articles cover variables, data types, and print functions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all F# tutorials.