The types used for storing collections of values are list and array. F# list is a typical linked-list type known from many functional languages – it can be either an empty list (written as []) or a cell containing a value and a reference to the tail, which is itself a list (written as value::tail). It is also possible to write a list using a simplified syntax, which means that you can write [1; 2; 3] instead of 1::2::3::[] (which is exactly the same...
Tuesday, December 30, 2008
The next data type that we will look at is a record type. It can be viewed as a tuple with named members (in case of record these are called labels), which can be accessed using a dot-notation and as mentioned earlier it is good to use this type when it would be difficult to understand what the members in a tuple represent. One more difference between a record type and a tuple is that records have to be declared in advance using a type construct:>...
Monday, December 8, 2008
In the next sample we demonstrate working with the discriminated union type. This type is used for representing a data type that store one of several possible options (where the options are well known when writing the code). One common example of data type that can be represented using discriminated unions is an abstract syntax tree (i.e. an expression in some programming language):> // Declaration of the 'Expr' typetype Expr =| Binary of string...
Saturday, December 6, 2008
TuplesThe first example demonstrates constructing and deconstructing a tuple type. Tuple is simple type that groups together two or more values of any (possibly different) types, for example int and string:>let tuple = (42, "Hello world!");;val tuple : int * string> let (num, str) = tuple;;val num : int val str : stringAs you can see, the compiler deduced a type of the expression that is present on the right side of the equals sign and the...
As already mentioned, F# is a typed functional language, by which I mean that types of all values are determined during the compile-time. However, thanks to the use of a type inference, the types are explicitly specified in the code very rarely as we will see in the following examples. The type inference means that the compiler deduces the type from the code, so for example when calling a function that takes int as an argument and returns string...
In one of the papers about F#, the F# designers gave the following description: "F# is a multi-paradigm .NET language explicitly designed to be an ML suited to the .NET environment. It is rooted in the Core ML design and in particular has a core language largely compatible with OCaml". In other words this means that the syntax of the F# language is similar to ML or OCaml (don’t worry if you don’t know these...
This text is based on a short overview of the F# language that was included in my bachelor thesis, but I extended it to cover all the important F# aspects and topics. The goal of this article is to introduce all the features in a single (relatively short) text, which means that understanding of a few advanced topics discussed later in the text may require some additional knowledge or previous experience with functional programming.Anyway,...
Subscribe to:
Posts (Atom)