Walkthrough: Your First F# Program

Visual Studio 2010 includes a new programming language, F#. F# is a multiparadigm language that supports functional programming in addition to traditional object-oriented programming and .NET concepts. The following examples introduce some of its features and syntax. The examples show how to declare simple variables, to write and test functions, to create tuples and lists, and to define and use a class.



To create a new console application


  2. If you cannot see Visual F# in the Templates Categories pane, click Other Languages, and then click Visual F#. The Templates pane in the center lists the F# templates.

  3. Look at the top of the Templates pane to make sure that .NET Framework 4 appears in the Target Framework box.

  4. Click F# Application in the list of templates.


  6. Click OK.

    The new project appears in Solution Explorer.

To use the let keyword to declare and use identifiers

  • Copy and paste the following code into Program.fs. You are binding each identifier, anInt, aString, and anIntSquared, to a value.

    let anInt = 5
    let aString = "Hello"
    // Perform a simple calculation and bind anIntSquared to the result.
    let anIntSquared = anInt * anInt

    If you cannot see the code in Classic view, make sure that the Language Filter in the header below the topic title is set to include F#.

To see results in the F# Interactive window

  1. Select the let expressions in the previous procedure.

  2. Right-click the selected area and then click Send to Interactive. Alternatively, press ALT+ENTER.

  3. The F# Interactive window opens and the results of interpreting the let expressions are displayed, as shown in the following lines. The types are inferred from the specified values.

    val anInt : int = 5

    val aString : string = "Hello"

    val anIntSquared : int = 25

To see the results in a Command Prompt window

  1. Add the following lines to Program.fs.

  2. Press CTRL+F5 to run the code. A Command Prompt window appears that contains the following values.




    You can verify the inferred types by resting the mouse pointer on the identifier names anInt, aString, and anIntSquared in the previous WriteLine statements.

To define and run a function

  1. Use a let expression to define a squaring function, as shown in the following code. The function has one parameter, n, and returns the square of the argument sent to n.

    let square n = n * n
    // Call the function to calculate the square of anInt, which has the value 5.
    let result = square anInt
    // Display the result.
  2. Press CTRL+F5 to run the code. The result displayed is 25.

  3. A recursive function requires a let rec expression. The following example defines a function that calculates the factorial of parameter n.

    let rec factorial n = 
        if n = 0 
        then 1 
        else n * factorial (n - 1)
    System.Console.WriteLine(factorial anInt)
  4. Press CTRL+F5 to run the function. The result displayed is 120, the factorial of 5.

To create collections: lists and tuples

  1. One way to aggregate values is by using a tuple, as shown in the following code.

    let turnChoices = ("right", "left")
    // Output: (right, left)
    let intAndSquare = (anInt, square anInt)
    // Output: (5,25)
  2. Another way to aggregate values is by using a list, as shown in the following code.

    // List of best friends.
    let bffs = [ "Susan"; "Kerry"; "Linda"; "Maria" ] 

    Add a new best friend to the list by using the "cons" operator (::). Note that the operation does not change the value of bffs. The value of bffs is immutable and cannot be changed.

    // Bind newBffs to a new list that has "Katie" as its first element.
    let newBffs = "Katie" :: bffs

    Use printfn to display the lists. Function printfn shows the individual elements that are contained in structured values.

    printfn "%A" bffs
    // Output: ["Susan"; "Kerry"; "Linda"; "Maria"]
    printfn "%A" newBffs
    // Output: ["Katie"; "Susan"; "Kerry"; "Linda"; "Maria"]
  3. You can view the results either by pressing CTRL+F5 or by selecting a section of the code and then pressing ALT+ENTER.

To create and use a class

  1. The following code creates a Person class that has two properties, Name and Age. Name is a read-only property. Its value is immutable, as are most values in functional programming. You can create mutable values in F# if you need them, but you must explicitly define them as mutable. In the following class definition, the value of Age is stored in a mutable local variable, internalAge. The value of internalAge can be changed.

    // The declaration creates a constructor that takes two values, name and age.
    type Person(name:string, age:int) =
        // A Person object's age can be changed. The mutable keyword in the
        // declaration makes that possible.
        let mutable internalAge = age
        // Declare a second constructor that takes only one argument, a name.
        // This constructor calls the constructor that requires two arguments,
        // sending 0 as the value for age.
        new(name:string) = Person(name, 0)
        // A read-only property.
        member this.Name = name
        // A read/write property.
        member this.Age
            with get() = internalAge
            and set(value) = internalAge <- value
        // Instance methods.
        // Increment the person's age.
        member this.HasABirthday () = internalAge <- internalAge + 1
        // Check current age against some threshold.
        member this.IsOfAge targetAge = internalAge >= targetAge
        // Display the person's name and age.
        override this.ToString () = 
            "Name:  " + name + "\n" + "Age:   " + (string)internalAge
  2. To test the class, declare two Person objects, make some changes, and display the results, as shown in the following code.

    // The following let expressions are not part of the Person class. Make sure
    // they begin at the left margin.
    let person1 = Person("John", 43)
    let person2 = Person("Mary")
    // Send a new value for Mary's mutable property, Age.
    person2.Age <- 15
    // Add a year to John's age.
    // Display results.
    // Is Mary old enough to vote?

    The following lines are displayed.

    Name: John

    Age: 44

    Name: Mary

    Age: 15


To view other examples in the F# tutorial




  4. Click F# Tutorial in the list of templates.

  5. Click OK.

  6. The tutorial appears in Solution Explorer.

For more information about functional programming and additional examples, see Functions as First-Class Values (F#). For more information about tuples, lists, let expressions, function definitions, classes, members, and many other topics, see F# Language Reference.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft