The difference between the list and [] in F #

I am porting some Haskell code to F #, but I'm a bit confused about the collections available and their signatures in F #. I understand 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 int.

// 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 bothers me is that the type signature for arrays is int [] . Maybe the type int array or int [||] will be less confusing? If you look at the type signature, I tend to think that this is a list, especially because an empty list is also represented as [].

One question here: why is the sequence type seq<int> and not int seq . I suppose it might be syntactic sugar, but maybe there is something else behind it.

Now create 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 [] 

Are T3 and T4 the same?

+4
source share
1 answer

F # allows you to use .Net type type definitions and ocaml style type definitions. It is the same.

 int list list<int> int seq seq<int> int array array<int> 

Some Expressions in F #

 [||] is an array [] is a list {} is a sequence 

In the type definition, you can also do int [] . What is C like syntactic sugar. Yep, T3 and T4 are the same.

However, you cannot do this []<int> . [] little strange, because it behaves like an array of types in a type definition and like op_Nil in an expression.

+5
source

Source: https://habr.com/ru/post/1401087/


All Articles