Languages - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Languages arrow Page 2 - Exploring the New F# Language
Exploring the New F# Language
By John Mueller

Rate This Article: Add This Article To:

Exploring the New F# Language - 'Getting F#'
( Page 2 of 4 )

 

Getting and Installing Your Copy of F#

You won’t find F# at the command line or within Visual Studio; you must download and install it. The main prerequisite is that you must have some form of the .NET Framework installed on your system, (it also works with Mono (http://www.mono-project.com/Main_Page).

ADVERTISEMENT

The inclusion of Mono as part of the picture means that you can use F# on Linux and the Macintosh. You can also use it with Windows Server 2008 Server Core version, which currently doesn’t support the .NET Framework, interestingly enough. (Check out this revelation at http://www.microsoft.com/technet/community/chats/trans/windowsnet/07_0626_tn_ws08.mspx. According to Microsoft, they do plan to create a light version of the .NET Framework for Server Core in the future, but for now, Mono does provide a good alternative.)

Microsoft also recommends that you have a copy of Visual Studio to work with F#. However, this isn’t actually a requirement. You can create perfectly acceptable F# applications using nothing more than your favorite text editor and the command line tools that come with the F# installation. In fact, if you plan to use F# on Linux/Macintosh, you’ll need a good text editor because Visual Studio doesn’t work on Linux/Macintosh.

You can find the actual download at http://research.microsoft.com/fsharp/release.aspx. Download the latest version of F# in either MSI (Microsoft Installer) or ZIP form. Use the MSI version if you want to obtain full F# support for Visual Studio because the installer adds projects to the Visual Studio IDE for you.

Creating a Simple Command Line Script

As previously mentioned, you can use F# for scripting purposes; you don’t have to have a copy of Visual Studio to use it. This section shows how to use the command line approach.

To begin working with F# at the command line, you’ll want to add the commands to your path. Using the current version, that means adding a path of C:\Program Files\FSharp-1.9.3.14\bin when you use the default installation.

After you have the correct path in place, let’s open a copy of Notepad or another text editor. Type the following code:

let displayMsg = "Hello World!";;

printf "Message: %A" displayMsg

The first line defines an identifier named displayMsg. You use the let keyword to create new identifiers. The displayMsg identifier has a value of Hello World!. Notice that you haven’t defined any type information; F# will infer the type from the output of the equation. All identifiers have an output in F#, even if that output is null (or void). Notice that you must end the let statement with a double semicolon.

The second line tells F# to print the value of displayMsg, whatever that value happens to be at the moment. The text within double quotes tells what to print. F# replaces the %A with the output of displayMsg.

Save the file you’ve created as Test.fs. All F# code files have a .fs extension. (If you’re using Notepad, remember to choose the All Files option in the Save As Type field of the Save As dialog box, or the file will have a .txt extension, and you’ll end up with Test.fs.txt.)

It’s time to run the test application. Open a command prompt. (If you changed the path and already had a command prompt open, you’ll want to close the command prompt and restart it so the path changes pick up.) Change directories to the location you saved the Test.fs file. F# has a number of command line utilities. For now, you need to consider two utilities. The first, fsc, will compile the example. You can then run the resulting executable anytime you want. The second, fsi, provides an interactive environment where you can try commands out before committing them to a code file. Let’s try fsc first. Type fsc Test.fs and press Enter. When the command line returns, you can type Test and press Enter to see the output shown in Figure 1.

Figure 1: Output from the compiled F# application.

 

The FSI utility works a bit differently. Type FSI Test.FS and press Enter. The FSI utility will load and execute the file. At this point, you should see similar output to Figure 1, but you can also modify the code as desired. For example, try typing let displayMsg msg = msg;; and pressing Enter. This creates a new displayMsg identifier that accepts a single variable, msg, as input and outputs that value unmodified. Now, you wouldn’t actually create an identifier like this in the real world, but hang in there with me for a while.

Of course, the problem now is how to send the value you want to display to displayMsg. F# uses a special pipeline operator |> to perform this task. To try it out, type printf "Message: %A" "Hello World!" |> displayMsg;; and press Enter. You can send any message to displayMsg this way, try it out to see how functions work in F#. When you finally finish working with the function, type #quit;; and press Enter to exit the interactive mode. Figure 2 shows the entire interactive sequence.

Figure 2: Interactive sessions let you discover how to work with F# with ease.

 



 
 
>>> More Languages Articles          >>> More By John Mueller
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.