Reputation: 2279
I am porting some Haskell code to F# but a bit confused about the collections available and their signatures in F#. My understanding is that elements in lists are enclosed between [ ], sequences between { }, and arrays between [| |]. In F# Interactive, I get the following when I create each container with 5 ints.
// List
> [1..5]
val it : int list = [1; 2; 3; 4; 5]
// Seq
> {1..5}
val it : seq<int> = seq [1; 2; 3; 4; ...]
// Array
> [|1..5|]
val it : int [] = [|1; 2; 3; 4; 5|]
What confuses me is that the type signature for arrays is int []
. May be type int array
or int [||]
would be less confusing? Looking at the type signature, I tend to think it is a list, especially because an empty list is also represented as [].
One question here: why is sequence type seq<int>
and not int seq
. I am assuming this may be syntactic sugar but may be there is something else behind this.
Now creating new types using the collection:
type T1 = T1 of int list
type T2 = T2 of int seq // becomes type T2 = T2 of seq<int>
type T3 = T3 of int array
type T4 = T4 of int []
Is T3 and T4 the same thing?
Upvotes: 4
Views: 1678
Reputation: 13862
F# allows you to use .Net style type definitions and ocaml style type definitions. These are the same.
int list
list<int>
int seq
seq<int>
int array
array<int>
Some Expressions in F# are
[||] is an array
[] is a list
In a type definition you can also do int []
. Which is C like syntax sugar. Yep, T3 and T4 are the same.
You can't however do this []<int>
. []
is a bit strange since it behaves as the type array in a type definition and as op_Nil in an expression.
Upvotes: 6