riginally a research language from Microsoft Research, F# has long been a "secret weapon" in the arsenal of .NET programmers for doing statistically and mathematically heavy coding. More recently, however, a growing number of developers have begun to see the inherent advantages implicit in writing functional code, particularly in the areas of concurrency. The buzz has begun to approach interesting levels following on the heels of an announcement last year from the head of the Microsoft Developer Division, S. "Soma" Somasegar, that F# would be "productized" and fully supported by Microsoft as a whole, suddenly removing the stigma and licensing restrictions that surround research projects.
One of the more recent ideas that's sweeping the industry is the notion of "domain-specific languages," (DSLs) whereby a developer builds a language that others (typically either skilled knowledge workers/subject matter experts, or sometimes even other programmers) will use to solve the particular business problems facing them. As Neal Ford describes, two different kinds of DSLs permeate the space: internal
DSLs, built on top of an existing language's syntax, and external
DSLs, built in some of the same ways as languages such as C# and Visual Basic—with a parser, an abstract syntax tree to store the parsed code, and either an interpreter to run the resulting program, or a code generator to turn it into an executable program.
As it turns out, F#'s derivation from a long line of functional languages means it has many of the tools necessary to make it easy to create simple external DSLs: discriminated unions, pattern matching, and combinatorial processing—all of which will probably be meaningless terms to the reader who's never ventured outside of the object-oriented realm before. In this article, I'll go over F#'s basic syntax, paying close attention to both those concepts that closely mirror that of the traditional O-O languages, and those that are brand new to the C#/VB programmer. While it's entirely unreasonable to expect to master the F# language (or any non-trivial language) in a single column, by the end of this article, you should at least be able to recognize and read F# code, letting you dive more deeply into F# if and when appropriate.
|F# is a functional programming language for the CLR based on another functional/object language, OCaml.|
F# is a CLR-adapted version of the Object-CAML language, also known as OCaml. As a functional language, F# espouses several core key ideas:
- Static typing is a good safeguard to writing good code so long as the type information can stay out of the programmer's way (also known as type inferencing).
- Functions are values and have first-class semantics right alongside integers and strings; that shared and/or mutable state should be minimized (if not avoided outright).
- Functional design permits and encourages composition and substitutability (which is where F# and other functional languages get much of their reputation for being "mathematical" languages).
Some of these concepts are more subtle than they may seem at first. For example, type inferencing means less explicit code needed to make the compiler happy, which in turn not only means that you can express the same concepts in less code in F#, but also that much of the verbosity of generics in C# effectively "fall away" when writing similar code in F#.
Before going too deeply into a more conceptual discussion, a quick glance at some F# code may help put a framework around some of these ideas.
To get started, you must install the F# binaries. As of this writing, F# still resides on the Microsoft Research site, the latest version is 126.96.36.199. You can find the download via the F# site
. Look for the latest release link in the announcements column on the right-hand side (the actual download URL changes with each release).
When installed, F# not only puts several executables, assemblies, and assorted samples into the installation directory, but also registers its Visual Studio language service support. This registration gives the F# developer much the same development experience as the C# or VB developer, with an added bonus: an extra tool window called interactive mode
, which allows the F# developer to type some F# code and execute it immediately, much as the Immediate Window works in the debugger. Simply "swipe" some code from the editor window with the mouse, hit Alt-Enter, and Visual Studio "automagically" pastes and executes the selected code in the Interactive Window. (This is by far the easiest way to explore the language and its concepts.)
|Author's Note: The F# package will also install into the Visual Studio Isolated Shell, so programmers who don't have a copy of the commercial Visual Studio bits can still play along.
|Functional programming languages encourage immutable state and zero "side effects," making them ideal for programming in highly concurrent environments.|
Assuming F# has installed successfully, fire up Visual Studio, create a new F# Project (under the "Other Project Types" node), add a new item ("F# Source file"), and call it file1.fs
(the default). Notice that when you open the new file, the compiler populates it with a large number of F# example snippets, which serve as an easy way of getting started with the language. Have a look at the code in that template file later; for now, delete it all and replace it with the following code: F#'s version of the ubiquitous "Hello world" program:
let language = "F#"
printfn "Hello, %A" language
Congratulations! You are now an F# programmer.
|Editor's Note: This article was first published in the September/October 2008 issue of CoDe Magazine, and is reprinted here by permission.|