got net?

Kevin Hazzard's Brain Spigot

Exploring the F# Language Series Part 2 - Installation and Configuration

clock August 3, 2008 17:30 by author kevin

Throughout this series, I will be exploring the F# (pronounced F Sharp) language as a beginner. Perhaps you're just like me in that you've never worked with the F# language before but you are very curious about it. You may not understand the hype you've been hearing about so-called functional languages. But that's OK. If you want to learn along with me, that would be great.

Along the way, I welcome your comments and feedback, both to instruct me and other readers. You can get an overview of the complete series by visiting the series index. Enjoy.

 

Part 2 - Installation and Configuration

To get started with F#, you need to get the compiler installed. Go to the Microsoft Research F# Downloads Page and download the latest release. If you are using Windows, download the MSI installer. If you're on Windows and want the Visual Studio experience but can't afford those tools, there is another interesting option. You can first install the Visual Studio 2008 Shell which is a free Integrated Development Environment (IDE) that Microsoft made available to help people integrate with specialized tools like F# or IronPython, for example. Whether you are using the full edition of Visual Studio 2003, 2005 or 2008 or the free Visual Studio 2008 Shell, the F# installer will affect all of the necessary changes to make working with F# in the IDE a smooth experience. If you don't have Visual Studio and don't care to use an IDE, you can always run the F# tools from the command line. And if you are using Mono runtime on Linux or Mac OS, download the ZIP file instead and follow the instructions in the README file to complete the installation.

At the time of this writing, I am working with version 1.9.4.19 released on May 1, 2008. Today, the F# installer requires one of Windows 2000, Windows XP, Windows Server 2003 or Vista and the .NET Framework 2.0. I've been running F# on Windows Server 2008 Standard Edition with no problems although Microsoft doesn't officially claim support for that operating system yet. If you are using Linux or Mac OS, you must install the Mono runtime.

If you aren't going to be using F# through the Visual Studio integration, you can skip all the way to the end of this article to the section entitled Using F# Interactive from the Command Line.

Installation on Windows

When you run the MSI installer you downloaded on Windows, you'll see something like this:

The installation usually takes about 5 minutes although your experiences may vary. After you're done with the installer, if you have Visual Studio, start it up. There's an add-in for F# Interactive that's installed but not enabled by default. You may want to turn it on. To do that within Visual Studio, select the Add-in Manager... choice from the Tools menu. You'll see a dialog that looks like this:

Enable the add-in entitled F# Interactive for Visual Studio by checking the box on the left. Press OK to close that dialog and save your changes. You'll see a tool window appear for the F# Interactive that looks like this:

This interactive F# interpreter is very handy. You can type or paste F# code into the interpreter window to try things out. But what's even better is that when you highlight code in the Visual Studio text editor and press the combination Alt+Enter keys, you can send the selected F# source over to the interpreter and run it. That's very handy, indeed.

Changing the Alt+Enter Key Assignment (ReSharper Users Only)

If you are using ReSharper, however, you may run into a problem here. If you are not using ReSharper, you can skip ahead to the next section entitled Using Alt+Enter and Alt+' to Evaluate F# Code in the Interpreter. If you use the standard IdeaJ/ReSharper keyboard settings, the Alt+Enter key combination is set by ReSharper to perform a different function within the scope of the text editor. So, when you press Alt+Enter in the text editor, the associated ReSharper function will run instead. To fix this, let's take a look at the keyboard settings. Here's the Text Editor scope key setting that shows how ReSharper's QuickFix function is assigned to the Alt+Enter key combination. This dialog is shown by clicking the Options... choice on the Tools menu in Visual Studio.

And here's Global scope key setting that shows how F#'s MLSendSelection function is assigned to the Alt+Enter key combination.

The easiest way to fix this clash of key assignments is to change one of them. Since I use Alt+Enter in ReSharper already, I decided to change the F# key mapping instead. I did this my clicking the Remove button in the dialog shown above to disconnect Global Alt+Enter key combination from F#'s MLSendSelection method. Next, I picked a new keystroke that makes sense. For me, Ctrl+Alt+Enter is close to the original and not used by any other add-in. Pressing those keys in combination while the text box under the "Press shortcut keys:" text has the focus makes the description of that keystroke appear in the text box. Next, I changed the scope of the assigned key by selecting "Text Editor" from the dropdown list entitled "Use new shortcut in:" and pressed the Assign button. There's really no sense in having that key assignment be Global in scope because, "What would the MLSendSelection method receive if there's no text editor open?" Does that makes sense? The dialog looked like this when I was done.

Using Alt+Enter and Alt+' to Evaluate Code in F# Interactive

Note: If you changed the key combination for invoking MLSendSelection as described in the previous section, anywhere you see Alt+Enter in the remainder of this tutorial, please substitute your selected keystroke instead.

To try out the F# Interactive tool window, you can type some F# code directly into the window or you can use the Alt+Enter and Alt+' (Alt+Single Quote) key combinations to send F# code from an open text editor. Let's try something simple. Open a new text window in Visual Studio and type a single line containing the following text

   printfn "Hello World.";;

This F# statement will invoke the printfn function for printing text to the console window. The literal text that we're going to print is "Hello World" minus the quotes of course. Lastly, the two semi-colons indicate the end of the "batch" if you will. More on that later. If you were to select all of the text you typed in by highlighting it in the text editor and press the Alt+Enter key combination, you would invoke F#'s MLSendSelection function in the IDE. The selected text would be run as a script and the output would show in the F# Interactive window. Here's what you might see:

Hey, you just wrote and executed the proverbial Hello World program using F#. Notice how the text output to the console that we expected to see shows up there. The Alt+Enter key combination you used is appropriate when you want to select only a portion of one line or multiple lines to send to the F# interpreter.

Another key mapping, Alt+' (Alt+Single Quote), exists after installing F# which is bound to a method called MLSendLine instead of MLSendSelection. As its name may imply, pressing Alt+' will cause the line on which the caret currently rests to be selected in its entirety and sent to the F# interpreter. So, the Alt+' key combination is useful for running one line of F# in the current text editor window at a time without having to select any text first.

Using F# Interactive from the Command Line

You don't have to use the F# Interactive add-in for Visual Studion. And if you're working on Mac OS or Linux using the Mono runtime, you need another option. In this case, the command line version of the F# Interactive tool can be used. On a Windows system, the FSI.EXE program is installed in the following folder:

   C:\Program Files\FSharp-<version>\bin

where <version> is the version number of the F# product you installed. For me, since I am using version 1.9.4.19, my binaries folder is:

   C:\Program Files\FSharp-1.9.4.19\bin

On Windows, you can invoke the help for the FSI.EXE by adding the --help flag to the command line.

Running FSI.EXE allows for interactive interpretation of F# code. After running FSI.EXE, enter the following text:

   printfn "Hello World.";;

and press Enter. You've just written the proverbial Hello World program using F#. Here's what it looks like running F# Interactive in PowerShell: 

When you're ready to quit F# Interactive, type #quit;; and press Enter. 

That's all for now. Feel free to take a look at the other parts of this series exploring the F# language by visiting the series index.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Exploring the F# Language Series Part 1 - What is F#?

clock August 3, 2008 12:00 by author kevin

Throughout this series, I will be exploring the F# (pronounced F Sharp) language as a beginner. Perhaps you're just like me in that you've never worked with the F# language before but you are very curious about it. You may not understand the hype you've been hearing about so-called functional languages. But that's OK. If you want to learn along with me, that would be great.

Along the way, I welcome your comments and feedback, both to instruct me and other readers. You can get an overview of the complete series by visiting the series index. Enjoy.

 

Part 1 - What is F#? 

If you are like me, a software developer with a C++/C# background, you may have heard about the F# language but you haven't had a chance to learn anything about it yet. You may also be a Java developer or a Visual Basic developer who's curious. Don't let the name fool you. You don't have to be a C# developer to love F# once you start learning about it. As we'll see, the F# language doesn't have strong roots or ties to languages like C, C++, Java, C# or Visual Basic. As such, I think it can teach us a lot about alternative ways of thinking.

An FAQ (of Sorts) for Defining the F# Language

Here are some questions I've accumulated about the F# language. My answers below aren't definitive, by any means. But this is what I've learned so far. If you have other questions that you think belong in an entry-level FAQ for the F# language, let me know and I'll add them.

  • With a name like that, is F# a derivative of C#?
  • What does the F in the F# language's name mean?
  • What is a functional programming language?
  • What is an imperative programming language?
  • Is F# object-oriented?
  • Can F# be integrated into Visual Studio?
  • Are there command line tools for working with F#?
  • Is F# a portable language?
  • Does F# require me to deploy a runtime library?
  • Is F# a scripting language or a compiled language (or both)?
  • How does F# perform in terms of speed as compared to other languages?
  • Where can I go to find out more about F#?

With a name like that, is F# a derivative of C#?

Not at all. F# comes from a family of languages that were spawned by a language created in the late 1970s known as ML or MetaLanguage. The ML language has a special type inference algorithm built into it that allows it to infer the types of most expressions automatically. This allows the language to feel dynamically typed like many scripting languages while actually being statically typed. F# behaves this way as well. In F#, you can declare types but you don't often need to do it.

More recently, F# is a derivative of a language known as Objective Caml (or OCaml) which extended Caml (Categorical Abstract Machine Language), an ML derivative, by adding certain object-oriented capabilities to it. F# has a high degree of compatibility with OCaml source code as a result.

What does the F in the F# language's name mean?

I'm sure it's debatable but the F in F# mostly likely stands for the term Functional. It's hard to get an exact answer on this one but one can assume that since F# is often touted as a functional language, that must be the meaning. F# is not just a functional language, however. It's a multi-paradigm language that allows for functional programming as well as imperative programming. So, it's conceivable that the language could have been called I#. But the functional feature of F# sort of steal the show so, the name makes sense.

What is a functional programming language?

Words are designed to conjure up ideas and images. So, what does the word functional mean in the context of a programming language. Merriam-Webster's dictionary defines functional as:

1 a: of, connected with, or being a function b: affecting physiological or psychological functions but not organic structure <functional heart disease>
2: used to contribute to the development or maintenance of a larger whole <functional and practical school courses>; also : designed or developed chiefly from the point of view of use <functional clothing>
3: performing or able to perform a regular function

As an American English speaker, the third definition is the one that I'm likely to think of when I hear the word functional. If something is functional, it's performing as it is supposed to. For other English dialects, the term functional could conjure up other meanings. But when I hear the term functional programming language, I'm likely to think of a programming language that's able to do what it was designed to do.

Of course, that makes no sense at all. If programming languages didn't perform as we expected them to, we wouldn't call them programming languages at all. We would call them probability languages or gee-i-sure-hope-this-thing-does-something-useful-when-i-press-the-button languages. Instead, the term functional in the realm of programming languages relates to the use of functions (or callable mathematical parts) as the basis for computation rather than changes in state and the mutation of data. So functional programming languages emphasize the second of the three definitions given above, i.e. viewing computation as the sum of the parts (functions) that are used to describe a larger solution.

In the 1930s, something called the Lambda Calculus was conceived to explore how functional decomposition of a problem could be used to more naturally describe potential solutions to it. Nowadays, functional programming languages are just implementations of the Lambda Calculus as a specific system with some extra bells and whistles, as they say. As functional programming languages go, however, F# is not a pure because it includes constructs for mutability, i.e. changing the state of objects in certain cases.

What is an imperative programming language?

Just as we did for the term functional above, it's probably worth investigating what the word imperative means at this point. Merriam-Webster defines imperative this way:

1 a: of, relating to, or constituting the grammatical mood that expresses the will to influence the behavior of another b: expressive of a command, entreaty, or exhortation c: having power to restrain, control, and direct
2: not to be avoided or evaded : necessary <an imperative duty>

As an American English speaker, both definitions come to mind. Imperatives are about the will and the duty to change things. The term imperative programming language is understandable in this context because imperative programming is all about state management.

To understand how imperative systems are built, think about the modern computer system. From the hardware up, all computers are somewhat imperative in nature. We store memory as a series of electrical signals and manipulate those signals to change the meaning of the data they represent. Registers on the CPUs change value as programs execute to track things like the position of a stack pointer or the location of the next executable instruction in memory. If you think about it, the entire computer system is nothing more than a very large state machine with a googolplex of possible states.

Imperative systems are, by definition, mutable, meaning that they can mutate or change in some way over time to create state that is representative of the progress of the computation being performed. Imperative programming languages, in particular, usually allow the programmer to define global and/or local state through various memory constructs like variables and classes. Because F# is based on OCaml, it has some object-oriented features that depend on mutable data. As we'll see later, the F# keyword mutable is at the heart of the language's imperative programming model.

Is F# object-oriented?

Yes. F# supports the .NET typing model including:

  • Classes
  • Inheritence
  • Interface implementation
  • Polymorphism

All types in F# ultimately derive from the .NET type System.Object so the model is completely unified and compatible with other .NET languages.

Can F# be integrated into Visual Studio?

The installation package for F# includes complete integration with Visual Studio 2003, 2005 and 2008. Included are:

  • A new F# project type (with file extension .fsharpp)
  • Templates for F# source, scripts and interfaces (including ML-compliant ones)
  • Templates for F# Lex and Yacc source
  • Integrated debugger support
  • A tool window for using the command-line tool called FSI.EXE (F# Interactive) for testing and running F# scripts and code

Are there command line tools for working with F#?

Yes. In fact, many people (like me) prefer working with the F# command line tools most of the time. There are F# command line tools in the installer package for:

  • F# compilation (FSC.EXE)
  • F# interactive interpreter (FSI.EXE)
  • F# Lex compiler (FSLEX.EXE)
  • F# Yacc compiler (FSYACC.EXE)
  • Resource compiler (RESXC.EXE)

Is F# a portable language?

Using the #light directive, F# can compile or interpret most OCaml code. So if you use that directive, your F# code should port back to OCaml without much effort. However, dependence on .NET types that are not available on other platforms will, of course, make your F# code non-portable (or at least portable with a lot of effort to replace the missing types).

Does F# require me to deploy a runtime library?

Yes. There is an assembly called FSharp.Core.dll which is referenced by your compiled F# code. A command line flag for the FSC.EXE compiler exists called --standalone. If you use this flag, the compiler will statically link the core components in so that you don't have to deploy the core assembly with your compiled F# assemblies. Using the --standalone flag will add between one and two megabytes to your F# assemblies, though.

Is F# a scripting language or a compiled language (or both)?

F# is definitely a compiled language. But it also supports  scripting via FSX files. You can run FSX scripts at the command line using the --exec flag of the F# Interactive (FSI.EXE) tool. This is useful when you want to run some F# code without having to compile it first.

How does F# perform in terms of speed as compared to other languages?

Compiled F# runs about as fast as C# or C++. I don't have personal benchmarks yet but empirically and anecdotally, compiled F# seems to be about as fast as other compiled .NET languages. The F# compiler has a cross-module optimizer that can be enabled using the -O command line flag. This flag is turned off by default. Of course, without hard performance data, my assessment remains quite subjective. We will examine F# performance in detail later on in the series.

Where can I go to find out more about F#?

Here are some links I've found useful:

That's all for now. Feel free to take a look at the other parts of this series exploring the F# language by visiting the series index.

Currently rated 3.5 by 4 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Calendar

<<  August 2008  >>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

Archive

Tags

Categories


Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in