F# Interactive (fsi.exe) is used to run F# code interactively at the console, or to execute F# scripts. In other words, F# interactive executes a REPL (Read, Evaluate, Print Loop) for the F# language.
Interactive Programming with F#
F# Interactive can be used to write code at the console or in a window in Visual Studio.
To run F# interactive from the console, run fsi.exe. For information about command line options available, see F# Interactive Options.
To run F# Interactive through Visual Studio 2010, you can click the appropriate toolbar button labeled F# Interactive, or use the keys Ctrl+Alt+F. Doing this will open the interactive window, a tool window running an F# Interactive session. You can also select some code that you want to run in the interactive window and hit the key combination ALT+ENTER. F# Interactive starts in a tool window labeled F# Interactive.
Whether you are using the console or Visual Studio 2010, a command prompt appears and the interpreter awaits your input. You can enter code just as you would in a code file. To compile and execute the code, enter two semicolons (;;) to terminate a line or several lines of input.
F# Interactive attempts to compile the code and, if successful, it executes the code and prints the signature of the types and values that it compiled. If errors occur, the interpreter prints the error messages.
Code entered in the same session has access to any constructs entered previously, so you can build up programs. An extensive buffer in the tool window allows you to copy the code into a file if needed.
When run in Visual Studio, F# interactive runs independently of your project, so, for example, you cannot use constructs defined in your project in F# interactive unless you copy the code for the function into the interactive window.
Scripts use the file extension .fsx or .fsscript. Instead of compiling source code and then later running the compiled assembly, you can just run fsi.exe and specify the filename of the script of F# source code, and F# interactive reads the code and executes it in real time.
Differences Between the Interactive, Scripting and Compiled Environments
When compiling code in F# Interactive, whether running interactively or running a script, the symbol INTERACTIVE is defined. When compiling code in the compiler, the symbol COMPILED is defined. Thus, if code needs to be different in compiled and interactive modes, you can use preprocessor directives for conditional compilation to determine which to use.
Some directives are available when executing scripts in F# Interactive that are not available when you are executing the compiler. For example, #r and #load.
The #load directive includes another source file, but invokes the compiler to compile the code rather than interpreting it. You can use the #r directive in interpreted code to reference an assembly and you can use the #I directive to specify an assembly search path.
One of the differences between compiled and interactive mode is the way you access command line arguments. In compiled mode, use GetCommandLineArgs. In scripts, use fsi.CommandLineArgs.
The following code illustrates how to create a function that reads the command line arguments in a script and also demonstrates how to reference another assembly from a script. The first code file, MyAssembly.fs, is the code for the assembly being referenced. Compile this file with the command line: fsc -a MyAssembly.fs and then execute the second file as a script with the command line: fsi --exec file1.fsx test
// MyAssembly.fs
let myFunction x y = x + 2 * y
// file1.fsx
#r "MyAssembly.dll"
printfn "Command line arguments: "
for arg in fsi.CommandLineArgs do
printfn "%s" arg
printfn "%A" (MyAssembly.myFunction 10 40)
The output is as follows: