F# Scripts--Zero to Execute in Ten Seconds

Jomo Fisher--F# is about conciseness. Consider the interview question that asks you to reverse a singly-linked-list. I’ve been asked this question twice in interviews and even the cleanest implementation in the interviewer’s chosen language was long and it was hard to show that I’d covered the corner cases. The F# answer is concise and self-evidently correct now that I’ve learned F# well enough:

// Take two lists, source and target, and append all the items from source

// to target in reverse order.

let rec AppendReverse source target =

    match source with

    | [] -> target

    | (head::tail) -> AppendReverse tail (head::target)

// Call AppendReverse with the empty list for target.

let Reverse list =

    AppendReverse list []

 

We’ve tried to bring this same philosophy of power and economy when integrating F# into Visual Studio. While F# does offer traditional MSBuild-based project support, sometimes you just want to write some code and not worry about projects and solutions. This is what F# scripts are for.

Once you have F# installed, you’re free to just start coding. These steps take about ten seconds:

- Press Ctrl+N (new file) and select Script\F# Script File

 

- In the opened .fsx file add:

System.Windows.Forms.MessageBox.Show("Hello")

 

- One time only, press Ctrl+Alt+F to bring up the F# interactive window.

- Press Ctrl+A (select everything) and then Alt+Enter (execute)

image

That’s it. We’ve tried to eliminate every piece of friction between you and the code you want to write.

We call these files scripts, but they’re not watered down in any way. They are actual F# which means they are compiled, statically typed and have access to .NET. There’s just not a project or even an explicit build step. In fact, this is one of the reasons F# needed a Visual Studio language service that can actively display errors and warnings as you type.

You can pick up the CTP of F# 1.9.6.0 here: https://msdn.com/fsharp

This posting is provided "AS IS" with no warranties, and confers no rights.