F# 101 F#, the latest member of the Visual Studio family of languages, offers some enticing advantages over C# and Visual Basic, stemming from its functional-object fusion nature. Originally a research language from Microsoft Research, F# has long been a “secret weapon” in the arsenal of .NET programmers for doing statistical- and mathematical-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, particularly on the heels of an announcement last year from the head of the Microsoft Developer Division, 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 that languages like C# and Visual Basic are, with a parser, 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. | " | F# espouses several core key ideas: namely that 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), that 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), and that functional design permits and encourages composition and substitutability.
| " |
As it turns out, F#, being derived from a long line of functional languages, 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 the basic syntax of F#, paying close attention to both those concepts that closely mirror that of the traditional O-O languages, as well as 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, F# should at least be recognizable and readable, leading the .NET programmer into diving more deeply into F# if and when appropriate. Concepts 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, namely that 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), that 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), and that 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 it 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. Setup Before any F# coding can take place, you must install the F# binaries. As of this writing, F# still resides on the Microsoft Research Web site, the latest version is 1.9.4.17 and is most easily found via the F# Web site there-simply visit http://research.microsoft.com/fsharp/fsharp.aspx and find the latest release link from the announcements column on the right-hand side (the actual download URL changes with each release). When installed, F# will not only put several executables, assemblies, and assorted samples into the installation directory, but will also register 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 the text will be automagically pasted and executed in the Interactive Window. (This is by far the easiest way to explore the language and its concepts.) Note that 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. 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: #light
let language = "F#" printfn "Hello, %A" language
Congratulations! You are now an F# programmer. | & | | 
By: Ted Neward
Ted Neward is a Principal Consultant with ThoughtWorks, a global consulting firm focusing on .NET, Java and Ruby enterprise development. He is an internationally recognized speaker, instructor, consultant, and mentor and spends a great deal of time these days focusing on languages and execution engines like the JVM and CLR.
ted@tedneward.com | Fast Facts | | F# is a functional programming language for the CLR based on another functional/object language, OCaml. | |
|