Setting up Visual Studio Code for Cross Platform F# Development

Setting up Visual Studio Code for Cross Platform F# Development

Hi its Jeremy. Today I am going to talk about another way that you can write F# code. This is my preferred way of writing F# code, especially if I am just trying to write a simple F# script file or solve a programming challenge on the internet.

It is a program called Visual Studio Code. I'm not sure if you are familiar with it or not, but Microsoft released it several months ago. A lot of people complain about the full version of Visual Studio because it is VERY big and VERY slow. It takes forever to download it or do updates. Even opening it up takes a long time. A lot of people don't like that. They want something that is fast, lightweight and easy to use. Microsoft created Visual Studio Code to fulfill this need.

Unlike Visual Studio, Visual Studio Code is a light-weight code editor and it is very fast and small. It is also cross platform so you can use it on Windows, Mac, or Linux and it is completely free. I have been using it for a couple of months now and I really like it. I highly recommend it. Not only do I use it for F#, but I also use it for all of my front end development such as Angular and Javascript.

Installation

The best way to install it is to use Chocolatey. Chocolatey is like apt-get for Windows. If you have worked with Linux before, you are probably familiar with apt-get. However, if you haven't used Linux or are not familiar with apt-get, then think of Chocolatey as nuget but for installing applications on Windows.

First go to Choclatey's website and install Chocolatey. They have instructions on how to get it installed.

https://chocolatey.org/install

After you have Chocolatey installed, open a command prompt and type

choco install visualstudiocode

Once you have Visual Studio Code installed, go ahead and open in up.

F# Extensions

Before you can use F# within Visual Studio Code you need to install a few extensions first. You can open the Extensions window by either pressing Ctrl + Shift + X or by clicking on the Extensions icon in the top left toolbar.

Search for "ionide" and you will get 3 results:
1. Ionide-fsharp
2. Ionide-FAKE
3. Ionide-packet

The only one that is required is Ionide-fsharp. This is the extension that you need in order to run F# code within Visual Studio Code. Click the install button and it will install the extension for you.

While you are here, you can go ahead and install the other 2 Ionide extensions if you want to, but they are not required. Ionide-FAKE is a build tool that allows you to create build scripts in F#. Ionide-Packet is an alternative to Nuget that a lot of F# developers prefer.

Open an F# Solution

I'm going to open the F# Solution that I created from Visual Studio in my last post on Setting Up Visual Studio for F# Development.

Go to File > Open Folder > Select the FSharpDemo folder.

In the explorer Window you can see that it has my solution file, the Program.fs and the Script file as well. Double-click the Program.fs file and then double-click the Script1.fsx file also. You will see that Visual Studio Code has a nice tabbed interface.

Also, if you drag one of the tabs down and to the right, it will open up a split screen editor so that you can view two files side by side which is very nice.

I'm not going to worry about the Program.fs file for now, so go ahead and close that.

F# Interactive Window

Open the Visual Studio Command Palette by pressing Ctrl + Shift + P and then type

fsi start

and press Enter.

This will open the F# Interactive Window. If you are not familiar with the F# Interactive Window then go read my last post on Setting Up Visual Studio for F# Development.

Create a Script File

Either open Script1.fsx or create a new file and call it Script1.fsx. Type the following code in your script file:

let a = 5  
let b = 7  
printfn "%i" (a + b)  

Line 1 declares and integer a and sets the value to 5. Line 2 declares a variable b and sets the value to 7. Line 3 prints a + b to standard out.

Press Ctrl + a to select all and hit Alt + Enter to run the code in F# Interactive.

12

val a : int = 5
val b : int = 7
val it : unit = ()

Just like in Visual Studio, it prints out 12 and says that you have an integer a with a value of 5 and an integer b with a value of 7.

If you click down in the F# Interactive Window you can execute code directly. Type a + b;; and press Enter.

val it : int = 12

The ;; are required to terminate a command when working within F# Interactive directly.

Code Lens

One of the things that I really like about using Visual Studio Code for F# is that it comes with Code Lens that gives you more information about the code that you are working with. If you look at the script file above Line 1 & 2 you will see some small grey text that says each of those variables are of type int.

For simple variables it isn't that big of a deal, but once you get into more complex code, it becomes very helpful.

Delete the content of your Script1.fsx file and paste the following code into it.

open System

let rec printPascalsTriangle rows iter lastRow =  
    let previousListLength = List.length lastRow
    let current = 
        match iter with
        | 1 -> [1]
        | _ -> 
            [0 .. previousListLength]
            |> List.map(fun index -> 
                match (index = 0 || index = previousListLength) with
                | true -> 1
                | _ -> lastRow.[index - 1] + lastRow.[index])

    current
    |> List.map(string)
    |> String.concat " "
    |> printfn "%s"

    if iter < rows then printPascalsTriangle rows (iter + 1) current

let main _ =  
    let rows = Console.ReadLine () |> int    
    printPascalsTriangle rows 1 []

    0 // return an integer exit code

main ()  

Don't worry about the details of this script for now. Just know that it accepts a number and then prints out Pascal's Triangle with that number of rows.

If you look above line 3 you will see the Code Lens help with the following text

int -> int -> int list -> unit

This is the signature for the method printPascalsTriangle. The last part of the signature is always the return type. In this case, it has a return type of unit which is equivalent to void in C#. All of the other items are the parameters to the method. So, the first and second parameter are integers and the third parameter is a list of integers.

Type System

One of the great things about F# is that it has a fantastic type system. When writing F# code, very rarely do you actually have to declare the types of any variables. The type system looks at the code and looks at how it is being used and it figures out what the types need to be.

In the code above I declared the printPascalsTriangle method with the following code:

let rec printPascalsTriangle rows iter lastRow =

In C#, the method would look like this:

public void PrintPascalsTriangle(int rows, int iter, List<int> lastRow)

You notice that in C# I had to declare all of the following types:
1. Return type of the method - void
2. Type of rows parameter - int
3. Type of iter parameter - int
4. Type of lastRow parameter - List<int>

In the F# code, I did not have to declare any of these things. The type system just figured it out on its own based on how the code was used.

Run the Code

Line 1 opens the System namespace.
Lines 3 - 20 creates a function called printPascalsTriangle.
Lines 22 - 26 creates a function called main.
Line 28 executes the main function.

Select all the code by pressing Ctrl + A and then hit Alt + Enter to send the code to the F# Interactive window which will execute the main method. The main method expects an integer as input.

Go down to the F# Interactive Window. Type in the number 10 and press enter.

The program will print out the first 10 rows of Pascal's Triangle:

Summary

In today's post I showed you how to install Visual Studio Code and get it setup for working with F#. I also gave a very quick introduction on how to work with F# Script files and the F# Interactive window.

I hope that you found this post helpful.

What's Next?

In my next post I'm going to introduce you to a website that I have been using that has helped me to learn F# and become a better F# Developer.

If you have any comments, questions, or suggestions, please leave a comment below.

Also, don't forget to subscribe so that you can be notified whenever I post new content.

Get Notified of New Posts

* indicates required

Related Article