Adapt from
[1] http://www.tryfsharp.org/Tutorials.aspx
[2] http://www.tryfsharp.org/Tutorials/QuickLanguageOverview/Section0.html
[3] http://www.tryfsharp.org/Tutorials/QuickLanguageOverview/Section1.html
[4] http://www.tryfsharp.org/Tutorials/QuickLanguageOverview/Section2.html
[5] http://www.tryfsharp.org/Tutorials/QuickLanguageOverview/Section3.html
[6] http://msdn.microsoft.com/en-us/library/dd233154.aspx
The Rise of F#
F# is a programming language that provides support for functional programming in addition to traditional object-oriented and imperative (procedural) programming. F# is a first-class member of the .NET Framework languages and retains a strong resemblance to the ML family of functional languages [6].
Features
- fully type checked at compile time (eliminating many kinds of run-time error)
- a compiled language (which delivers good performance)
- combines its functional nature with the imperative paradigm, with the object-oriented paradigm, and with the concurrent paradigm
- integrates with the .NET Framework and other implementations of the CLI (Common Language Infrastructure) (asynchronous and parallel class libraries is supported)
Example – First program
let x = 7
let y = 6
let z = (float 6.0)
let s = "life the universe and everything"
printfn "The answer to %A is %A" s (x*y)
Key takeaway
- A
let statement is not an assignment statement but is used to associate a name with a value. In functional programming, such an association is called a binding. The new binding simply hides the old one if the binding already exists.
- There is no declarations. The name
x does represent an int value but it does not need to be declared as int because the type can be easily deduced from its associated value 7. Whenever datatypes can be inferred from the code, they can be omitted. The complete form is shown in let z = (float 6.0).
printfn is a built-in function for outputting lines of text; it is modelled after the printf function of C. The printfn function recognizes many of the same formatting codes as printf in C; however %A is an extra one which works with any datatype.
- Function calls are written in this format — the function name, followed by the arguments which are separated by white space. (No comma and brackets)
Example – Association statement and assignment statement
[Incorrect-Error: Duplicate definition of value 'n']
let n = 2
let n = n + 1
printfn "n = %A" n
[Incorrect-Give "n = 2"]
let n = 2
n = n + 1
printfn "n = %A" n
[Incorrect-Give "n = 2"]
let mutable n = 2
n = n + 1
printfn "n = %A" n
[Correct-Give "n = 3"]
let mutable n = 2
n <- n + 1
printfn "n = %A" n
The associatation statement binds n to a value, which is not mutable. However, mutable variable is required to do the assignment of value. If an assignment statement is really needed, F# does provide it by a special operator (the left arrow <-).
Example – Indentation and broken a line of code
[Version 1]
let AVeryLongVariableName = 1
let AnotherLongVariableName = 2
let variableNumberOne = AVeryLongVariableName + AnotherLongVariableName
[Version 2]
let variableNumberOne =
AVeryLongVariableName +
AnotherLongVariableName
[Version 3]
let variableNumberOne = (AVeryLongVariableName
+ AnotherLongVariableName)
[Version 4]
let variableNumberOne =
AVeryLongVariableName + AnotherLongVariableName
All four versions are identical. Indentation matters in F# code. It is used instead of a statement terminator (such as the semicolon in C, C# or Java code) and instead of statement grouping symbols (such as the left and right brace characters in C, C# or Java) to indicate where a F# language construct begins and ends.