# Tutorial :lists in structs in F#? ### Question:

I'm very new to F# and I'm trying to make a struct for storing polygons, and it has to contain a list of coordinates:

``type Polygon =      struct          val Coords : list          new(list_of_Coords) = { Coords = list_of_Coords }      end  ``

but Visual studio says "The type 'Microsoft.FSharp.Collections.list<_>' expects 1 type argument(s) but is given 0"

I think not as I don't intend to initialise the list in the struct - just declare it.

### Solution:1

See

http://cs.hubfs.net/forums/thread/11377.aspx

for the answer.

(Repeated here:

You need to specify the type of list, e.g. list<float>.

``type Polygon =      struct          val Coords : list<float>          new(list_of_Coords) = { Coords = list_of_Coords }      end  ``

)

### Solution:2

In addition to Brian's anwer: You can also make the structure generic when you don't know the type of your coordinates in advance (even if `string Polygon` wouldn't make much sense)

``type 'a Polygon =      struct          val Coords : 'a list          new(list_of_Coords) = { Coords = list_of_Coords }      end  ``

Generally, you can declare a record type like this (assume you have a `Coord` type)

``type Polygon = { Coords : Coord list }    // Code ...    let myPolygon = { Coords = [ ... ] }  ``

### Solution:3

For case you want generate as float as int and other type polygons, you can use following code:

``type Polygon<'a> =      struct          val Coords : list <'a>          new(list_of_Coords) = { Coords = list_of_Coords }      end    let inline genPolygon (a: 'a list) =    new Polygon<'a> (a)    > genPolygon [1;2;3];;  val it : Polygon<int> = FSI_0002+Polygon`1[System.Int32] {Coords = [1; 2; 3];}  > genPolygon [1.0;2.0;3.0];;  val it : Polygon<float>  = FSI_0002+Polygon`1[System.Double] {Coords = [1.0; 2.0; 3.0];}  ``

### Solution:4

``> type 'a F = { coords: 'a list };;    type 'a F =    {coords: 'a list;}    > let dd = {coords=[1.;2.]};;    val dd : float F    > let dd = {coords=[[1.;2.];[1.;2.]]};;    val dd : float list F  ``

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »