Monday, October 12, 2009

F# is built on the .NET type system, and provides access to any type defined in a .NET assembly. Types can also be defined in F# using the keyword ty

Terse Syntax

One of the most striking features of F# code is that it is very terse - ideas can typically be expressed with a small amount of code. There are a few significant language features which contribute to this:


1. Type Inference: F# is strongly typed, like C#, but instead of having to declare the type of variables, parameters and return types, F# uses type inference to determine these automatically. When the types cannot be inferred, type annotations can be supplied in the code, such as in the definition of the modSquared function above.
2. #light: The #light directive in F# allows code to omit begin...end keywords and some other tokens, and instead relies on indentation to indicate nesting. This is similar to languages like Python, and enables the same kind of syntactic lightness that programs in these languages enjoy.
3. Expressions: F# programs are built out of expressions, which can be composed very simply. For example, if is an expression in F#, as opposed to in C# where it is a statement. This can make code simpler, while also enabling a high degree of flexibility.


Libraries

F# code can use all of the exisiting .NET libraries, such as the Console class used in the code above. But F# also has access to a rich set of F# libraries, providing types that are well suited to functional programming and F# in particular. A few notable libraries:


1. Math: One very useful example is the F# math libraries, representing datatypes such as Vector, Matrix, and BigNum, as well as the Complex numbers used in the code above.
2
Interactive

F# comes with an "F# Interactive" toolwindow for Visual Studio, and also a command line interactive shell (fsi.exe). These are tremendously useful for protyping and exploring, and can also be used as a testbed while working on larger projects. As an example (see screenshot below) the code above can be pasted into an interactive shell to execute immediately. If you want to make changes, just edit the code and paste into the interactive prompt again to run the new version.

No comments:

Post a Comment