I'm starting a brand new series of short articles about
. The plan is
to describe features that, for me, make F# a compelling and enjoyable .NET language.
So far, I have 10-15 articles in mind, but I'm open to suggestions. If you have any ideas
for additional topics, please email them to dustin AT diditwith.net
The Interactive Environment
Ruby and many other programming
languages, F# provides an interactive scripting environment. However, F# is
different in that the interactive environment is not an
interpreter. Instead, it dynamically compiles code on-the-fly.
There are two ways to load this environment:
- Run fsi.exe from the bin subdirectory of the F# distribution.
- Load the F# Interactive for Visual Studio add-in from the Visual Studio
Once the environment is loaded, a splash screen is displayed. (NOTE: the
examples here use fsi.exe.)
MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 126.96.36.199, compiling for .NET Framework Version v2.0.50727
NOTE: See 'fsi --help' for flags
NOTE: Commands: #r <string>;; reference (dynamically load) the given DLL.
NOTE: #I <string>;; add the given search path for referenced DLLs.
NOTE: #use <string>;; accept input from the given file.
NOTE: #load <string> ...<string>;;
NOTE: load the given file(s) as a compilation unit.
NOTE: #time;; toggle timing on/off.
NOTE: #types;; toggle display of types on/off.
NOTE: #quit;; exit.
NOTE: Visit the F# website at http://research.microsoft.com/fsharp.
NOTE: Bug reports to firstname.lastname@example.org. Enjoy!
At this point, it's easy to start typing F# code. To execute code, type a
double semi-colon. The following bit of code, when typed into the interactive
environment, will instantiate and display a new .NET Windows Form:
> open System.Drawing
- open System.Windows.Forms;;
> let myForm = new Form(Text = "Hello, World!", Visible = true);;
val myForm : Form
The first two lines open the System.Drawing and System.Windows.Forms
namespaces. This is analogous to C#'s using and VB's Imports statements.
It isn't necessary to reference the System.Drawing.dll or
System.Windows.Forms.dll assemblies because they are implicitly referenced by
The third line instantiates a new Form, sets its Text and
Visible properties, and binds it to the name myForm.
Because the code is dynamically compiled and executed, the form is displayed
Now that the form is instantiated, it can be manipulated at runtime.
> myForm.BackColor <- Color.Blue;;
val it : unit = ()
When executed, the above code changes the form like so:
The F# Interactive Environment is a great way to break out of the standard
edit-compile-debug rut and prototype some code. It can even output to a .NET assembly. Run "fsi.exe --help" to see more
ways in which the interactive environment can be used.