As many of you know, I'm a guy from C #. Therefore, if I just want to try something, I usually just open LINQPad and try something there. If things are growing and I need more control over the experiments, I will give up LINQPad and deploy the ConsoleApplication application in Visual Studio. Both of these approaches work fine, but it would be nice if there was some kind of environment in which you could try something inside Visual Studio itself without even creating a new project.
Fortunately, there is one in F #.
There is a completely brilliant tool called the F # Interactive Window (FSI), which you can find using Visual Studio (I'm using VS 2012), for example:
As soon as you open the F # interactive window, you should see something like this:
Now this may not seem so grandiose, but this window allows you to quite easily do a lot of things. It can be used for rapid prototyping and testing, if you like.
Thus, you can see that you have a prompt
> that is awaiting your input. Let's introduce something?
Give input
All you need to do in the F # interactive window to get any input is to end your line with two semicolons โ;;โ.
Some valid examples of correct entry are given below.
Declaring a function that will take a value and square it (note, we did not specify the type x):
let squarer x = x * x;;
Now let's declare a variable that will contain the return value of the call to the squarer function. This is easy to do as follows:
let squared = squarer 10;;
Data display
If we want to look at the data, we can also evaluate things in a similar way. Thus, assuming we just entered the values โโabove, we will have the following:
- Square function
- Int type returned by function
Thus, to evaluate one of them, say, โsquared,โ we can simply type something like this into the FSI input line:
squared;;
When we run this line in FSI, what happens is that the value is displayed in the FSI window, something like this:
val it : int = 100
Where you can see that the value is really 100, which is the result of an earlier call to the squarer function that was created in the FSI window.
Another cool thing before ending
Before we finish this article, I just would like to mention that you can also use the FSI window along with the F # script file if you want to try something fast.
Say I had an F # (FSX) script file with the following code in it:
open System let squareRootMe x = System.Math.Sqrt(x)
And I wanted to try this in FSI, I can just select the lines / section of the code and select its execution in the FSI window.
Now it is in the scope of the FSI window. Which means that I can use the function as shown here:
> let x = squareRootMe 144.0;; val x : float = 12.0