- 0 0
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.
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.
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.
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:
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
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.
Ctrl + a to select all and hit
Alt + Enter to run the code in F# Interactive.
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
val it : int = 12
;; are required to terminate a command when working within F# Interactive directly.
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 ->  | _ -> [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.
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 -
2. Type of rows parameter -
3. Type of iter parameter -
4. Type of lastRow parameter -
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
Lines 3 - 20 creates a function called
Lines 22 - 26 creates a function called
Line 28 executes the
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:
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.
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.