Partager via


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.

Comments

  • Anonymous
    August 29, 2008
    The comment has been removed

  • Anonymous
    August 29, 2008
    Don Syme just announced today the F# Community Technical Preview (CTP) Release September 2008 which is

  • Anonymous
    August 29, 2008
    Don Syme just announced today the F# Community Technical Preview (CTP) Release September 2008 which is

  • Anonymous
    August 29, 2008
    Don Syme just announced today the F# Community Technical Preview (CTP) Release September 2008 which is

  • Anonymous
    August 29, 2008
    I’m very pleased to announce the availability of the F# September 2008 CTP Release , launched via the

  • Anonymous
    August 30, 2008
    Oh dear, how about: template< class T > T reverse( const T& other ) {   return T( other.rbegin(), other.rend() ); } That's the entire point of BCL has missed. On generics and so much more. F# leading nowhere too it seems. Oh yes, did I say you have the same mechanics for COMPILE-TIME. All this runtime nonsense is so off the mark, it is incredible it improved only so slightly on Java.. life goes on.

  • Anonymous
    August 30, 2008
    Hi Qw, Thank you, I'd forgotten that I wanted to mention that in the real world you should just use the built in function:    List.rev mylist My code in the article is O(n) but why reinvent the wheel? My code in the article is successful if it can show the power and expressiveness of F# language primitives. Thanks!

  • Anonymous
    September 24, 2008
    First, let me remind you that in my new ongoing quest to read source code to be a better developer ,

  • Anonymous
    December 03, 2008
    How can one add references to assemblies in a script?