Another item that was on the F# Infographic was the use of the F# Interactive or also called the FSI. The interactive window is basically a REPL for F#. A few languages have these of their own, including Python and Ruby.

If you’ve used the Intermediate Window when debugging in C#, then you’ll feel right at home with the F# Interactive. However, there’s one big difference…you need to be debugging and at a breakpoint in order to use the Intermediate Window. The F# Interactive can be used anytime.

Although, I’m sure most folks in C# make their changes within the app, compile, and then run or attach to do their debugging. Depending on where in the app you are trying to debug, it may take up to a few minutes before you make the change and the breakpoint gets hit. With F#, either in a compiled .fs file or in a script .fsx file, you can always send items to the interactive.

The best way to get to the interactive is, in Visual Studio, is to go to Views -> Other Windows -> F# Interactive. Depending on how you have Visual Studio set up, it may just be in the View menu.

If you’re using Xamarin Studio you also have an F# Interactive windows. Go to View -> Pads -> F# Interactive. I’ll be showing examples in Visual Studio.

The main advantage of the F# Interactive, I believe, is the opportunity to run the code as soon as you write it. As an example, l’s say you want to implement and learn how to use FAKE and create a build script.

You may have something to get you started with it similar to what’s in the docs:

#I @"../packages/FAKE.3.8.5/tools/"
#r "FakeLib.dll"
open Fake

Target "Default" (fun _ ->
    printfn "Hello F# Interactive!"
)

RunTargetOrDefault "Default"

When I highlight all of the code and hit Alt + Enter (or right click ) to send to the interactive we can see in our output we have the below:

The resulting target order is:
 - Default
Starting Target: Default 
Hello F# Interactive!
Finished Target: Default

---------------------------------------------------------------------
Build Time Report
---------------------------------------------------------------------
Target     Duration
------     --------
Default    00:00:00.0005253
Total:     00:00:00.1050778
Status:    Ok
---------------------------------------------------------------------

val it : unit = ()

The interactive is very useful when playing around with different frameworks and libraries to learn how to work with them. Another example can be when creating algorithms or generic functions and making sure they start working the way you want them to.


Hopefully this helped to illustrate how powerful and useful the F# Interactive can be during coding or even for doing a proof of concept. Imagine if you had this when doing your C# coding.