Hello World in F#

Our first program will print the classic “hello world” message. Here’s the full source code.

open System

[<EntryPoint>]
let main argv =
    printfn "hello world"
    0 // return an integer exit code

To run the program, save the code in a file named HelloWorld.fs and use dotnet to execute it.

$ dotnet fsi HelloWorld.fs
hello world

Sometimes we’ll want to build our programs into binaries. In F#, we can use the .NET SDK to achieve this.

First, create a new console project and replace the generated code with your F# script:

$ dotnet new console -lang F# -o HelloWorld
$ cd HelloWorld
$ nano Program.fs
# Paste the F# hello world code here and save.

Then, build and run the project:

$ dotnet build
$ dotnet run
hello world

Now that we can run and build basic F# programs, let’s learn more about the language.