Tuesday, October 14, 2008

[2008.10.14] Visual Studio 2008 and F#

This is a quick introduction to F# and Visual Studio 2008. Download and install the latest version of F# [September 2008 CTP as of this post]. Fire up VS2008 and get started!

The first thing to note is that F# will not like it when you use the default 'tab' settings that comes with VS. So go to Tools -> Text Editor -> F# and expand that submenu. Select Tabs and change it to the values shown in the image.
Fig.1 Tab options for F#

Next, go to File -> New -> Project and pull up the New Project dialog. Select the Visual F# option in the left pane and notice the types of projects available to you:
  • F# Application
  • F# Library
  • F# Tutorial
For now select the F# Application option, choose your project name, location, etc, and VS will create the necessary project files. VS then opens up to a blank file with the default name of Program.fs. You can start typing away, but it may be helpful to have the F-Sharp Interactive (FSI) window opened up as well in VS. You can do this by going to View -> Other Windows -> and choose F# Interactive (or Ctrl + Alt + F). This opens up the FSI command line console and pins it by default to the bottom of the VS window. The default VS F# project with the FSI open and pinned is shown in Fig. 2.
Fig.2 VS with default F# file and FSI

Now you can start writing code and executing it by:
[1] Highlighting the code, and hitting Alt + Enter will send your highlighted code to the FSI for execution. The results are then displayed in the FSI window.
[2] Typing your code directly in the FSI window. When you are done typing in the FSI and you are ready to run the code, add two semi-colons (;;) at the end of your code and hit Enter to execute.
Fig.3 Running F# code in VS2008

No comments: