DEV Community

Cover image for Write your first code in F#
Microsoft Azure

Write your first code in F#

Chris Noring Cloud Developer Advocate at Microsoft, Google Developer Expert
Updated on ・2 min read

F# is an open-source, cross-platform programming language that makes it easy to write succinct, performant, robust, and practical code.


 Why F#?

There are many language features and aspects of the F# language that make it easy to be productive when writing code:

  • Succinct: You write less code with F# that's also expressed in a clear manner.
  • Performant: F# comes with built-in parallelism and concurrency. It also uses the fact that it's part of the .NET runtime to speed things up.
  • Robust: There are language constructs that makes the code fault tolerant and robust like immutable by default, null value management and more.
  • Supports multiple programming paradigms: F# lets you choose the patterns and practices most effective for solving problems by providing strong support for functional and object programming paradigms.

How do I get started?

There are a few things you need to get started.

  • .NET SDK. You need to install the .NET SDK to be able to buiild, run, test and also create F# projects. You can find the F# SDK install here
  • Ionide. It's a VS Code plugin. It's not necessary to install, but it will give you things like Intellisense, tooltips, codelens, error highlight and more.

Your first code

You can start writing F# by using the REPL called FSI, you can also scaffold an F# project using the dotnet executable.

Create a project

You create a project using dotnet like so:

dotnet new console --language F# -o MyFSharpApp
Enter fullscreen mode Exit fullscreen mode

Then you get a project with files:

  • Program.fs, your entrypoint file
  • MyFSharpApp.csproj, your project file that contains everything it needs to know to build the project like dependencies for example.

What does the code look like?

Here you have the code of Program.fs. Itt contains the functions from() and main(). main() is the entry point of the app, as seen by [<EntryPoint>].

open System

// Define a function to construct a message to print
let from whom =
    sprintf "from %s" whom

let main argv =
    let message = from "F#" // Call the function
    printfn "Hello world %s" message
    0 // return an integer exit code
Enter fullscreen mode Exit fullscreen mode

The from() function takes the arg whom and the function body prints a string using the function sprintf.

How do I learn more?

Discussion (0)