5 Reasons to use F# Interactive in Visual Studio 2010

Note: While the following post is targeted at Visual Studio 2010 users, most of the points apply even if you aren’t using Visual Studio 2010. F# Interactive (FSI) is easy (and free) to install for Visual Studio 2008 users and command line users running in Windows or Mono. Details are available at the F# Developer Center.

1) You already have it

F# comes standard with Visual Studio 2010, and it includes F# Interactive. There’s nothing to install, and no configuration is required. You don’t even need to start a F# project in order to use FSI. From anywhere inside Visual Studio, select View | F# Interactive, or just press Ctrl + Alt + F to bring up an FSI instance.

2) Performance Analysis

With the #time option enabled, F# Interactive is a surprisingly useful tool for performance analysis. A few days ago, coworkers Jay Wren and Ben Barefield asked me to help determine why a LINQ statement they wrote was running slowly. After spending time looking over the code and running it through a profiler, we wanted to see how LINQ’s distinct statement behaved with different inputs. Within a couple minutes, I was able to get information using F# Interactive.

First, I wrote a simple setup script:

open System
open System.Linq

#time

let randomizer = Random()

let sequenceSize = 1000000

let sequential = Enumerable.ToList(Seq.init sequenceSize (fun x -> x.ToString()))
let random = Enumerable.ToList(Seq.init sequenceSize (fun _ -> randomizer.Next(1000)))

Then I ran couple of statements to test timing:

> sequential.Distinct().ToList();;
Real: 00:00:00.336, CPU: 00:00:00.296, GC gen0: 1, gen1: 1, gen2: 1
val it : Collections.Generic.List<string> = seq ["0"; "1"; "2"; "3"; ...]
> random.Distinct().ToList();;
Real: 00:00:00.119, CPU: 00:00:00.031, GC gen0: 0, gen1: 0, gen2: 0
val it : Collections.Generic.List<int> = seq [959; 18; 824; 585; ...]

F# Interactive is no substitute for more sophisticated performance analysis techniques, but when it comes to getting fast answers to test bottlenecks, it’s a great tool to have at your disposable.

3) Verifying the Behavior of Base Class Library Functions

There are a lot of APIs in the Base Class Library, and it can be tough to remember exactly how everything works. Let’s say you can’t remember if the Insert function on List<T> inserts elements before or after the input index. You could write a small console application or a throwaway test to verify the behavior, but it’s a lot faster to use FSI:

> open System.Collections.Generic;;
> let list = new List<int>();;

val list : List<int>

> list.Add(0);;
val it : unit = ()
> list.Add(1);;
val it : unit = ()
> list.Add(2);;
val it : unit = ()
> list.Insert(1, 99);;
val it : unit = ()
> list;;
val it : List<int> = seq [0; 99; 1; 2]
>

4) Learning F# and Functional Programming

Learning any language teaches you new coding techniques. Learning a functional language teaches you new problem solving techniques. F# Interactive lets you do both without leaving Visual Studio or closing your open project. Whenever you have a few minutes to kill during development, you can easily open a FSI window and play around with F#. You can also use it to deep dive and explore syntax and techniques with a more extended session. Finally, using F# Interactive while programming in another .NET language is a great way to keep your F# skills sharp even if you aren’t writing F# on a daily basis.

5) Spikes and Scripting

This is probably the first use case that people think of when they see FSI (or other REPLs). In practice, I find that I use F# Interactive more for performance analysis, learning F#, and verifying Base Class Library behavior than for spiking or scripting. However, it’s worth pointing out that F# Interactive is a powerful tool for quickly exploring problem domains. By creating script files, you can build up situations to evaluate different approaches without investing a lot of time setting up a dummy project or a clumsy test harness.

This entry was posted in F#. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. john
    Posted October 26, 2011 at 12:58 am | Permalink

    Now thanks to Roslyn (still CTP), you can have access to C# Interactive
    http://www.microsoft.com/download/en/details.aspx?id=27746

4 Trackbacks

  1. [...] This post was mentioned on Twitter by Daniel Mohl, Chris Marinos. Chris Marinos said: Blogged: 5 Reasons to Use F# Interactive in Visual Studio 2010 http://bit.ly/a9I8oq #fsharp [...]

  2. [...] Chris Marinos's 5 Reasons to use F# Interactive in Visual Studio 2010 [...]

  3. [...] //TODO: – Chris Marinos' Blog Skip to content About « 5 Reasons to use F# Interactive in Visual Studio 2010 [...]

  4. [...] I could get my feet wet with, I realized the possibilities for automating VisualStudio by using F# Interactive (a F# console that can also be used inside Visual Studio – the other way would be to call fsi [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>