DEV Community

Mohamed Dahir
Mohamed Dahir

Posted on • Updated on

Getting Started with F# the Easy Way

If you want to try F# but without the project setup it requires, then you might try one these which are easier:

Online dotnet compiler

Try F# using the online dotnet compiler. Don't forget to choose F# from the language dropdown on the left-hand side.

To follow the rest of this guide, you will need to install dotnet cli.

F# repl

You can write F# code interactively by using repl. The repl can be started by running dotnet fsi in the command-line. Each statement you write should be terminated with double semicolons. To exit the repl, type Ctrl+D.

F# repl

Use .fsx extension

Typing directly into the repl can be hard, that is why F# lets you edit your code in a .fsx file with your favorite editor and load it in the repl afterwards.
To do this, run dotnet fsi --use:<filename> (Don't include < >).

Alt Text

Vscode with Ionide

Install Vscode and its Ionide extension. With this setup, you can highlight any code you want to test then hit alt-enter to send it to the repl.

Alt Text

You could also use the arrow button in the top right-hand corner to send the whole file to the repl.

arrow button in right corner

If you get fsi not found error, you can fix it by enabling Fsharp: Use Sdk Scripts in your Vscode settings.

Extra: Using multiple .fsx files

To import some .fsx file into your other .fsxfile, you can use #load directive. For example, if you want to load B.fsx in A.fsx

// B.fsx

let rec calculateFuel mass =
    let fuel = (mass / 3) - 2
    if fuel > 0 then fuel + calculateFuel fuel
    else 0
Enter fullscreen mode Exit fullscreen mode
// A.fsx

#load "B.fsx" // this loads B.fsx file

// Now you can either
B.calculateFuel 100

// or
open B
calculateFuel 100
Enter fullscreen mode Exit fullscreen mode

Extra: Unit Testing .fsx files

Simple testing can be done manually via the repl. In the event you need to do several tests after each change, you might write your tests in another .fsx file. For Example, this is how I tested my code for day1 puzzle of advent of Code 2019

// Day01.fsx

let calculateFuel1 mass = (mass / 3) - 2

let rec calculateFuel2 mass =
    let fuel = (mass / 3) - 2
    if fuel > 0 then fuel + calculateFuel2 fuel
    else 0
Enter fullscreen mode Exit fullscreen mode
// Day01Test.fsx

#load "../Day01.fsx"
open Day01

let ``calculateFuel1: mass of 12`` = (calculateFuel1 12) = 2
let ``calculateFuel1: mass of 14`` = (calculateFuel1 14) = 2
let ``calculateFuel1: mass of 1969`` = (calculateFuel1 1969) = 654
let ``calculateFuel1: mass of 100756`` = (calculateFuel1 100756) = 33583

let ``calculateFuel2: mass of 14`` = (calculateFuel2 14) = 2
let ``calculateFuel2: mass of 1969`` = (calculateFuel2 1969) = 966
let ``calculateFuel2: mass of 100756`` = (calculateFuel2 100756) = 50346
Enter fullscreen mode Exit fullscreen mode

Next, we load our test file in the repl. Our tests pass when all bindings in our test file evaluate to true. Notice how F# let us write descriptive names using double backticks.

Alt Text

Extra: Some Useful F# resources

Top comments (1)

Collapse
 
rickbradford profile image
RickBradford

Another way to test out F# is to use dotNetCore.

This lets you develop full compilable (console) F# apps.