Getting Started with F# the Easy Way
Tags: fsharp
Published: 2019-12-25 21:53:55

If you want to try F# but without the [project setup](https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line) it requires, then you might try one these which are easier:

# Online dotnet compiler
Try F# using the [online dotnet compiler](https://dotnetfiddle.net/). 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](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install).

# F# repl
You can write F# code interactively by using [repl](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). 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](https://thepracticaldev.s3.amazonaws.com/i/p4mrmxm43mxzqhbuoesg.jpg)

# 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](https://thepracticaldev.s3.amazonaws.com/i/46d3lon4mk4h71chyxoi.jpg)

# Vscode with Ionide
Install [Vscode](https://code.visualstudio.com/) and its [Ionide](http://ionide.io/docs/#plugin-installation) 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](https://thepracticaldev.s3.amazonaws.com/i/30pl0n2fos7rso5940gg.png)

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](https://thepracticaldev.s3.amazonaws.com/i/jm0o89wl4bvj0vdnciy8.PNG)

If you get `fsi not found` error, you can fix it by enabling [`Fsharp: Use Sdk Scripts`](https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-vscode#configure-f-interactive) in your Vscode settings.

# Extra: Using multiple `.fsx` files
To import some `.fsx` file into your other `.fsx`file, you can use `#load` directive. For example, if you want to load `B.fsx` in `A.fsx`

```fsharp
// B.fsx

let rec calculateFuel mass =
    let fuel = (mass / 3) - 2
    if fuel > 0 then fuel + calculateFuel fuel
    else 0
```

```fsharp
// A.fsx

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

// Now you can either
B.calculateFuel 100

// or
open B
calculateFuel 100
```

# 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](https://adventofcode.com/2019)

```fsharp
// 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
```

```fsharp
// 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
```

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](https://thepracticaldev.s3.amazonaws.com/i/3q541aewf2ql9ehhfulh.png)

# Extra: Some Useful F# resources
* [F# for fun and profit](https://fsharpforfunandprofit.com/) is a comprehensive site for everything F# related. It also hosts many [F# talks](https://fsharpforfunandprofit.com/video/). Two videos which I recommend are [Functional programming design patterns](https://www.youtube.com/watch?v=E8I19uA-wGY) and [Domain Modeling Made Functional](https://www.youtube.com/watch?v=Up7LcbGZFuo).

* [F# collections docs](https://msdn.microsoft.com/visualfsharpdocs/conceptual/microsoft.fsharp.collections-namespace-%5bfsharp%5d) I use this whenever I need to find a function such as map, scan, fold, pairwise, etc for operating on collections (List, Map, Seq, Set, etc).

* [Coursera's Programming Languages, Part A](https://www.coursera.org/learn/programming-languages). This course teaches functional programming using standard ML, a language that shares many similarities with F#.