TheSharperDev

A site containing some of Morgan’s thoughts

Lately I’ve been looking into using F# in Jupyter Notebooks. I downloaded it and attempted to follow the tutorials, but got very little to actually work.

One of my issues I faced was that F# scripting, graphing, paket and Jupyter Notebooks were all new to me. When I encountered an error, I wasn’t sure which technology was the problem.

I decided to take a step back and examine each of these topics separately. That way if anyone encounters the same issue there’s a gentler introduction to each of these technologies individually.

First thing to examine is using the REPL and Scripting in F#. Lets dive in.

The first thing we need to do is make sure that fsi.exe (F# Interactive) is in your path. The quickest way to check that is open up some command prompt (cmd, powershell, etc) and type in fsi.exe.

If text with Microsoft and F# Interact version is printed to the console, it’s already on the path and you can skip to the next section. Otherwise lets get that setup.

On my machine, I installed F# along with Visual Studio 2019. If you don’t already have it, please see Microsoft’s instructions for installing.

Then add the location of fsi.exe into your environment variables, a microsoft doc gives the location if you installed it through Visual Studio 2019 like me.

C:\Program Files (x86)\Microsoft Visual Studio\2019\<sku>\Common7\IDE\CommonExtensions\Microsoft\FSharp

I have the community version installed, so my path ended up being:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp.

After that, restart your command prompt to make sure it has the new path variable and type in fsi.exe to launch the F# REPL.

Playing with the REPL

Once you’ve got the REPL going, for the most part it’s the same F# you use in a standard .fs file, with one notable exception. You must end each expression is two semi-colons, ;;.

Here are some samples so you can start to see how to use it.

Remember, two semi-colons ;; to end each expression. That takes a while to get used to.

You can type #help;; to see the short help menu and #quit;; in order to exit the REPL.

F# Scripting

After playing around with the REPL a bit, lets move onto using F# scripting.

Call fsx file from REPL

Create a file called SimpleFunctions.fsx and include the following:

Then if you start a REPL in the same directory as that file, you can import and use the functions:

Things to remember:

1. You have to both load a file (line 1) and then open (line 7) a namespace.
2. The feedback from the loading is a bit odd (lines 2-5, changes the namespace and doesn’t include the module), but calling it is as you would expect.
3. Double semi-colons!

Call fsx file from another fsx file

Now lets call our “SimpleFunctions.fsx” from another script file in the same directory:

Very similar to using it in the REPL, make sure you load and then open.

Then run fsi.exe .\RunningFunctions.fsx to see the following print to your console:

Hello Functional! Goodbye OOP!*

Pretty snazzy. I also want to point out that you can load dlls using the #r "path/to/dll/sample.dll" at the top of your script and then open the namespace that you care about. I’m waiting to talk more in depth about that because I haven’t figured out how to get paket working yet. And paket is the best way to get the particular dlls that you care about.

Closing Thoughts

The F# REPL and scripts are both something I’ve never used before, but can be pretty quick way to test some F#. Now that I’ve got a baseline understanding, these are both things that I’ll be using more of in the future.

I’ll also expand more on this subject by delving further into how to use paket during F# scripting in one of my next articles.

References:

*I’m not saying goodbye to OOP, it was a light hearted joke.

Categories

Tags