>> In this video, we're going to be taking
a look at F# Interactive,
an interactive programming a scripting environment for F#.
We'll also be using F# Interactive for our first lines of F# code.
Any code that you see this video can
be found at aka.ms/f#-101-samples.
Now, you maybe asking yourself,
what is the F# Interactive?
Well, as the name implies,
it's an interactive programming and scripting environment for F#.
What this means is that it executes or read,
evaluate, print, loop or REPL.
What this REPL does, is it reads code,
evaluates it, and immediately
prints out the output of that code to the console.
Now, it does so on a loop, which means that you can continuously
check whether your code is behaving as expected.
Now that you know what F# Interactive is, let's see it in action.
F# Interactive can be started using the .Net CLI.
In this case, I'm using the VS Code Integrated Terminal
running Ubuntu 20.10.
Now, because I'm just cross-platform,
we're seeing on Mac or Windows.
To start F# Interactive enter
the dotnet fsi command into the terminal.
Doing so will start the REPL.
Once F# Interactive starts,
you can begin writing code.
Let's start up by printing "Hello world" out to the console.
Now this code uses a printfn function which takes a string,
in this case "Hello world",
and prints it out to the console.
In F# Interactive,
you signify the end of an expression by using two semi-colons.
You can add multiple expressions in the terminal.
However, they're not going to be evaluated
until you add the two semicolons.
Let's take a look at how that works in action.
Let's say I want to print out "Hello World".
But I also want to print out "Hello F#".
Now as you can see, nothing has actually happened at this point.
However, when I add the two semi-colons at the end,
both "Hello World" and "Hello
F#" are immediately printed out to the console.
Once you're done using the REPL and F# Interactive,
you can simply exit it by doing pound or
a number sign followed by q and the two semi-colons.
That's all there is to it. Evaluating your code
and getting immediate feedback is incredibly useful as you
build out your applications because you're able to tell right
away whether your code is
behaving the way that you would expect it to.
However, typing code out into the terminal isn't
exactly the easiest way that you
can create code that's easy to share,
reproduce, and keep different versions off.
You also don't get the benefits of a text editor or IDE,
such as syntax highlighting and intellisense.
That's where scripts come in. Let's take
a look at what those look like in F#.
F# Interactive script files use the FSX file extension.
Let's take a look at what our previous example of printing
strings out to the console looks like inside of a script file.
Now in this case, I call my script HelloWorld.FSX.
In my script I'm printing out
these strings "Hello world" and "Hello F#".
But with script files, you don't have to
include the semi-colons at the end of your expressions.
To run a file use dotnet fsi,
followed by the file name.
If you'd prefer to use the UI,
you can use this icon up here to run your script.
It will effectively give us the same thing as as
the dotnet fsi followed by the file name.
You're going to notice that after a few seconds,
the expected output is printed out to the console.
In our case, "Hello World" and "Hello F#".
Now in this case, I run the entire script.
You also have the option to run it line by line by
using Ionide's F# Interactive support.
To do so, place your cursor on the line that
you want to run and press "Alt" "Enter".
Now what this is going to do is it will open
an F# Interactive REPL and it evaluate the line of code.
You're going to see that once REPL starts,
since the line of code that you want to
evaluate and it evaluates it for you.
Just like if you were using to terminal directly.
Now, for our multiples lines of code,
you can highlight the lines of code that you want to
evaluate and do the same thing, "Alt",
"Enter", which will also send it
to F# Interactive REPL and evaluate your code.
Chances are you're going to be using
a external packages inside of your applications.
F# Interactive makes it extremely easy to manage
your dependency inside of your script files.
Let's take a look at how you can use
external packages inside of F# Interactive scripts.
In this example, I have a script called packages.fsx.
Let's go ahead and run the script using
the dotnet fsi command and look through the code.
To reference packages,
use the pound bar or a number sign r followed by a string.
Or where your package is stored locally,
this will be the path to your DLL.
You also have the option of using NuGet packages.
If you're not familiar with NuGet,
NuGet is a package manager for dotnet.
When using NuGet packages,
you need to have the prefix NuGet,
followed by the package name, followed by the version.
The version is optional and if
left out, the latest version is used.
When you run this line, you package is
downloaded and made available for use inside the script.
In this example, I'm using
the package F# data to
download the HTML from the F# documentation site.
Then I'm printing that using
a feature known as an interpolated string,
which enables you to embed F# expressions in your strings.
If we take a look at the output,
we can see a few HTML tags,
which means that our script works.
Now that you know what F# Interactive is
and how you can use it to write and evaluate code,
we're going to begin to explore
different language concepts. See you in the next video.
[MUSIC]