Interactive Session #1: variables

The best way to start with F# is to launch the interactive session and enter some of the idioms to get a feeling how F# looks like.

~> fsi

Microsoft (R) F# 2.0 Interactive build
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;


To assign values to variables you need the ‘let’ keyword; variables in F# are similar to variables in mathematics, you may assign any value you like (therefore the term ‘variable’), but once assigned you can no longer change its value.

> let a = 0;;

val a : int = 0

However, you may be surprised when trying out the following in your interactive session:

> let b = 1;;

val b : int = 1

> let b = 2;;

val b : int = 2

> b;; (* asking the interactive for the value of b *)
val it : int = 2

It looks like you actually can change the value of variables, however, this is not true, every new line in the interactive session creates a new scope. Inside the new scope you may define again a variable with the same name as already defined in the previous scope.

Now, enter two definitions of the same variable name by entereing the second line using Shift-Return instead of Return.

> let c = 1  
- let c = 2;;

 let c = 2;;

~/stdin(19,5): error FS0037: Duplicate definition of value 'c'
This entry was posted in Getting Started. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s