Chapter 2. How to Obtain, Install, and Use F#

This chapter is designed to get you up and running with F# as quickly as possible. You'll look at how to obtain F#, how to install it on both Windows and Linux, and how to use the compiler in various ways. I'll also discuss what version of software was used to test the examples in this book.

Obtaining F#

F# is now included in Visual Studio 2010 by default, so if you have this installed on your machine you may already have F# installed. If you have Visual Studio 2010 installed but you can't see F#, then you need to ensure that you installed the package. This can be done through the Add/Remove Programs or Programs section of the control panel (see Figure 2-1).

If you're not a Visual Studio user or would prefer to use Visual Studio 2008 rather than 2010, then you'll have to download the F# distribution separately. The best place to look for all F#-related information is the MSDN F# resource center at http://msdn.microsoft.com/fsharp/. A link to the compiler distribution is included in the top, left-hand corner of the F# resource center page. There are two versions—an MSI version, which will automatically install F# Visual Studio integration if Visual Studio is installed, and a ZIP version of the distribution, which is primarily targeted at non-Windows users. The package includes the compiler fsc.exe, as well as fsi.exe (the F# interactive console), some F#-based parsing tools, the F# base class libraries, the F# documentation, and some F# samples.

Enabling F# in Visual Studio 2010

Figure 2.1. Enabling F# in Visual Studio 2010

Installing F# on Windows with Visual Studio 2008

This section is for people using an older version of Visual Studio. (As noted, Visual Studio 2010 users will already have F# installed.) Installing F# on Windows with Visual Studio 2008 is straightforward. You need to be running an account with system administrator privileges, then download the MSI version as described in the previous section, and execute it.

Please note that at the time of this writing the free Express Editions of Visual Studio do not support plug-ins, so you cannot use F# integration with them. However, you can install F#'s plug-in on top of the free Visual Studio 2008 Shell. See the MSDN Visual Studio Extensibility center for more details on Visual Studio 2008 Shell at http://msdn.microsoft.com/vsx2008/.

Installing F# on Linux

If you are unfamiliar with Linux and would like to try Mono, the simplest way is to download the SUSE Linux virtual machine (VM) image available on the Mono web site at http://www.go-mono.com/mono-downloads.

Note

Mono is a free, open-source, multi-platform implementation of the Common Language Runtime (CLR), which is compliant with the ECMA specifications of the Common Language Infrastructure (CLI) and compatible with Microsoft .NET. It is implemented by Novell with the aid of a number of community volunteers.

This VM image comes with the latest Mono runtime and development tools preinstalled, so there's no need to worry about setting up any of these. But the image does not currently include the F# compiler, so you will need to install it using the following instructions. I performed all of these steps as the root account.

  • Still in the /usr/lib/fsharp directory, run the command sh install-mono.sh.

  • Unpack the F# distribution and copy the resulting files to /usr/lib/fsharp.

  • In the /usr/lib/fsharp directory, run chmod +x install-mono.sh.

  • Run the dos2unix tool on the text file install-mono.sh.

  • Still in the /usr/lib/fsharp directory, run the command sh install-mono.sh.

After performing those steps, I was able to use F# from the command line of any account by running mono/usr/lib/fsharp/bin/fsc.exe, followed by the command-line options. Obviously, this was inconvenient to run every time, so I created a shell script file in /usr/bin and as fsc.

#!/bin/sh
exec /usr/bin/mono $MONO_OPTIONS /usr/lib/fsharp/bin/fsc.exe "$@"

I then ran chmod +x fsc to give users permission to execute it. After this, running the F# compiler was as simple as typing fsc at the command line. The F# interactive compiler, fsi.exe, the shell script for this is as follows:

#!/bin/sh
exec /usr/bin/mono $MONO_OPTIONS /usr/lib/fsharp/bin/fsi.exe --no-gui "$@"

Figure 2-2 shows F# interactive running under Mono and Linux.

F# interactive running under Mono and Linux

Figure 2.2. F# interactive running under Mono and Linux

Using F# in Different Ways

F# programs are just text files, so you can use any text editor to create them. Just save your program with the extension .fs, and use fsc.exe to compile them. For example, if you had the following program in the text file helloworld.fs:

printfn "Hello World"

you could just run fsc.exe helloworld.fs to compile your program into helloworld.exe, which would output the following to the console:

Hello World

Visual Studio

In my opinion, the easiest and quickest way to develop F# programs is in Visual Studio in conjunction with the F# interactive compiler (see Figure 2-3). You can type F# programs into the text editor, taking advantage of syntax highlighting and IntelliSense code completion; compile them into executables; and debug them interactively by setting breakpoints and pressing F5. Also, you can execute parts of your code interactively using F# interactive. Just highlight the code you want to execute and press Alt+Enter; F# interactive will execute the code and show the results. This is great for testing snippets individually.

Visual Studio 2010 hosting F# interactive

Figure 2.3. Visual Studio 2010 hosting F# interactive

SharpDevelop

SharpDevelop is an open-source IDE with F# bindings that can be used for .NET and Mono development. The F# bindings are packaged with SharpDevelop, so to use them all you need to do is ensure that F# is installed then install SharpDevelop. After that it's just a matter of creating a new F# project and off you go. The F# bindings for SharpDevelop do not offer as much functionality as Visual Studio does—only syntax highlighting and F# interactive are available. However, the development environment is still very useable, and the bindings are open source. So if you wish to extend them, you can help out with the project. Figure 2-4 shows SharpDevelop with an F# project open.

SharpDevelop with an F# project open

Figure 2.4. SharpDevelop with an F# project open

F# Interactive Command-Line

If you prefer, you can type your programs into the F# interactive console directly when it's running in stand-alone mode, as shown in Figure 2-5.

The F# interactive console running in stand-alone mode

Figure 2.5. The F# interactive console running in stand-alone mode

When you use the interactive console, you type the code you want. When you've completed a section, you use two semicolons (;;) to indicate that the compiler should compile and run it.

F# interactive responds to commands in two ways: If you bind a value to an identifier, it prints the name of the identifier and its type. So, typing the following into F# interactive:

> let i = 1 + 2;;

gives the following:

val i : int

However, if you just type a value into F# interactive, it will respond slightly differently. Typing the following into F# interactive:

> 1 + 2;;

gives the following:

val it : int = 3

This means the value has been bound to a special identifier called it that is available to other code within the F# interactive session. When any expression is evaluated at the top level, its value is also printed after the equal sign (note the 3 in the previous example). As you get to know fsi.exe and F# in general, using F# interactive will become more and more useful for debugging programs and finding out how they work. (I discuss values, identifiers, and types in more detail in Chapter 3.)

You can get code completions by pressing Tab. I find this mode of working useful in testing short programs by copying and pasting them into the console or for checking properties on existing libraries. For example, in Figure 2-2 I checked the System.Environment.Version property. However, I find this mode inconvenient for creating longer programs since it's difficult to store the programs once they're coded; they have to be copied and pasted from the console. Using Visual Studio, even if you don't intend to just run them interactively, you can still easily execute snippets with Alt+Enter.

The Examples in This Book

The code in this book will focus on using fsc.exe rather than fsi.exe. Although fsi.exe is great for testing code, running simple scripts, and running experiments, I believe fsc.exe is more useful for producing finished software. Since there's little difference between the syntax and the commands, most examples will work with little or no adaptation in fsi.exe, and I'll warn you when any changes are necessary.

The samples can be downloaded from http://bfs.codeplex.com/. All the samples in this book were tested using .NET 4.0 running on Windows 7. A subset has also been tested running under Mono 2.4.2.3 on Linux.

Summary

This chapter described how to install and run F# and the different ways you can work with it. The following chapters will explain how to program with F#, starting in Chapter 3 with functional programming in F#.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset