Home

Introduction to Data Types in F#

 

Values Fundamentals

 

Introduction to Variables

A variable is a section of the computer memory used to temporarily hold a value. Reserving a portion of memory is referred to as declaring a variable. There are rules you must follow.

 

Let a Variable Bind

To declare a variable, use an operator named let. Based on this, declaring a variable in F# is also referred to as let binding.

Follow the let keyword by a name for the variable. Here is an example:

let name

You must assign a value to the variable. This is done using the assignment operator "=". The formula to follow is:

let VariableName = Value

Here is an example:

let x = 16

A declaration can end with a semicolon or not. The above declaration can also be written as:

let x = 16;

One of the types of values used in F# is called a string. It is simply a group of letters or symbols. The value of a string must start with a double-quote and must end with a double-quote. Here is an example:

let title = "Application Programming"

Remember that you can end the declaration with a semi-colon if you want. After declaring the variable, you can use it any way you want. One option is to use it where a string would have been used, such as the "Something" string we used in the previous lesson. Here is an example:

open System
open System.Windows.Forms

let title = "Exercise"

let exercise = new Form()
exercise.Text <- title

Application.Run(exercise)

This would produce:

Introduction to Variables

If you already know the value you want to use and you know where you want to use it, you don't have to first declare a variable for the value. You can just directly use it. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Application Programming"

Application.Run(exercise)

In most cases, you will declare a variable only if you plan to use a value more than once

Names in F#

The names of variables must follow some rules. For example, avoid using the following names for variables:

Also avoid using the following words when naming anything:

asr atomic break checked component const constraint
constructor continue eager event external fixed functor
include land lor lsl lsr lxor method
mixin mod object parallel process protected pure
sealed sig tailcall   trait virtual volatile
 
   
 

Introduction to Data Types

 

Fundamentals of Data Types

A data type is the amount of memory a computer needs in order to store the value of a variable. As there are various types of values, there are also different types. In fact, F# supports different types of values.

An Infered Language

When you declare (or bind) a variable and assign a value to it, if you don't explicitly indicate its type, the compiler implicitly determines the variable's type. F# is an infered language. This means that, when declaring a variable, you don't have to specify its data type but you must initialize it. When you do this, the compiler will "infer" the data type; that is, the compiler will decide about the appropriate type to apply to the variable.

As a result, you can omit specifying the data type of a variable and let the compiler take care of it, or you can specify it. If you decide to specify the data type of a variable, follow its name with a colon and the data type. The formula to follow is:

let VariableName : DataType

One of the types of values used for numbers is called int. Here is an example of declaring a variable of type int:

let age : int = 16

Another type of value is called a string. A string is simply a group of letters or symbols. Here is an example of a string variable:

let title : string = "Application Programming"

Options on Declaring and Using Variables

 

Spanning a Line to Declare a Variable

So far, we had declared and initialized our variables on one line. If you want, you can put the initialization on another line. If you decide to do this, the code on the next line must be indented. Here is an example:

let x =
    16

or

let x =
    16;

Declaring Various Variables

Instead of declaring one variable, you may want to declare many. To do this, you can declare each on its line, using its own let keyword and its own initialization. Here are examples:

let x = 16;
let y = 24;
let z = 58;

As an alternative, you can use one let keyword and list the variables separated by commas with the group followed by the assignment operator "=". After the assignment, give the list of values for each variable in the order they appear in the initialization. Here is an example

let x, y, z = 16, 24, 58

The variables don't have to be of the same type. Here is an example:

let x, y, z = 16, "John", 'M'

Remember that if the line of initialization is too long, you can continue it on the next line and remember to indent it. Here is an example:

let x, y, z =
   16, "John", 'M'
   
 

Home Copyright © 2009-2015, FunctionX Home