Monday, October 13, 2008

Getting Started With F#

First retrieve and install the latest release of F# (the Sept. 2008 CTP can be found here).

I'm a believer in TDD (Test Driven Development), so our first step after installation of the CTP is to create a test.

Step 1: Creating the Test Project:

1. Open Visual Studio and type Ctrl+Shift+N to create a new project.
2. Create a Test project by selecting a project type of Visual C#\Test\Test Project. ( Note: I'm using a C# test project in VS2008, but any other test project will do.)
3. Set the location to any desired path with a new destination folder of "FSharpGettingStarted".
4. Change the Name to "FSharpTests".
5. Ensure that "Create New Solution" is selected and that "Create Directory For Solution" is checked.
6. Click OK.

Step 2: Creating the First Test:

1. Delete the default class that was generated in the test project.
2. Right click the FSharpTests project and select "Add\New Test...".
3. Select the Unit Test template and set the Test Name to "FSharpSampleTests.cs" then click OK.
4. In the newly generated class, locate the method "TestMethod1" and rename it to CanCalculateNumberSquared.
5. Remove the text that was generated in the method and type:
int result = FSharpSample.CalculateNumberSquared(2);
Assert.AreEqual(result, 4);

Step 3: Creating the F# project:
1. Type Ctrl+Shift+N and select the project type of Visual F#\F# Library.
2. Change the name to "FSharpSample".
3. Change the Solution option to "Add to Solution" and click OK.

Step 4: Adding the F# Function From Our Test:
1. Delete the "Module1.fs" file that was generated.
2. Right click on the FSharpSample project and select Add\New Item\F# Signature File.
3. Name the file "FSharpSample.fsi".
4. Right click on the FSharpSample project and select Add\New Item\F# Source File.
5. Name the file "FSharpSample.fs". (Note: if you add the ".fs" file before the ".fsi" file you may see an error message that states "An implementation of file or module FSharpSample has already been given. Compilation order is significant in F# because of type inference. You may need to adjust the order of your files to place the signature file before the implementation.". If you see this error, simply delete the ".fs" file and recreate it or manually edit the ".fsproj" file so that the ".fsi" file reference is above the ".fs" file reference in the ItemGroups.)
6. Open the FSharpSample.fsi file and type "val CalculateNumberSquared: int -> int" in the line directly below the line that states "#light".
7. Open the FSharpSample.fs file and type "let CalculateNumberSquared x = x*x;;" in the line directly below the line that states "#light".
8. Add the FSharpSample project as a reference to your Test project.
7. Run your test and it should now pass.

That's all there is to setting up your first F# library created with TDD. Next we will start talking about what some of this all means.

2 comments:

  1. Consider yourself being followed! Good stuff... keep it up!

    ReplyDelete
  2. Consider yourself being followed! Good stuff... keep it up!

    ReplyDelete