The first snippet actually creates an array, not a list. The second one creates an empty mutable list in a mutable variable.
If you want an empty array:

1
 let cpt = [||]

If you want an empty list:

1
let cpt = []

If you want an empty mutable list:

1
let cpt = ResizeArray<Point>()

For more information about F# lists, see [link:msdn.microsoft.com]

By on 9/2/2011 5:01 PM ()

Hello romeo2011,

[| Point( 20,60), Point (40,50) |] is not a List, it is an array of (Point*Point). I do not know, what you need.

If you work with F# lists, create it as follows:

1
2
3
let cpt1=[ Point(20,60); Point (40,50) ; Point (40,50)] // List of Points
let cpt2=[ (Point(20,60), Point (40,50)); (Point(20,60), Point (40,50)) ] // List of (Point*Point)
let cpt0=[] or  let cpt=list.Empty // Emtpy list

F# lists are held in memory. If you have many and large lists you risk to get OutOfMemory.

If you work with large lists use F# sequences (the syntax is similar to F# lists).
Sequences are lazily evaluated types.
So, if you create a list

1
let li=[0..Int32.MaxValue]

you will get OutOfMemory, whereas by a sequence

1
let se={0..Int32.MaxValue}

you will not get OutOfMemory.

with Sequences

1
2
let cpt1=seq[ Point(20,60); Point (40,50) ; Point (40,50)] // Sequence of Points
let cpt2=seq[ (Point(20,60), Point (40,50)); (Point(20,60), Point (40,50) )] // Sequence of (Point*Point)

You can also create a sequence of your array of (Point*Point):

1
let cpt = [| Point( 20,60), Point (40,50) |]
1
2
3
let cpt3 = seq[| Point(20,60), Point (40,50) |] // Sequence of array of (Point*Point)
let cp4 = Seq.append cp2 cp3 // Combine cp2 and cp3
let cpt0=Seq.Empty  // Empty sequence

As you can see, with Sequences, you have a lot of possibilities to combine lists, arrays, and sequences, similar to the System.Collection.Generic lists.

If you work with list types of System.Collections.Generic you can get an empty list as follows:

1
2
let cpt1=new List() // List of Points
let cpt2=new List() // List of (Point*Point)

It is easy to work with these lists, you can add and remove elements dynamically (they are mutable).
For example: If want to add your array of (Point*Point) cpt = [| Point( 20,60), Point (40,50) |], you can easy do it with the AddRange method: cpt2.AddRange(cpt). But it is not pure functional programming.

By on 8/28/2011 11:18 AM ()

A couple of notes about this:

  • [| ... |] is syntax for arrays, not lists.
  • The syntax for lists is [ ... ]
  • Commas are used for tuples, not separating array/list elements. Use semicolons for that, e.g. a list of points would be

    1
    
    [ Point(20,60); Point(40,50) ]
  • F# lists are immutable, unlike

    1
    
    System.Collections.Generic.List<T>
  • Even if you wanted a mutable list, the "mutable" keyword isn't necessary in

    1
    
    let mutable cpt = new List<Point>()

    , unless you wanted to later point cpt to a different instance of

    1
    
    List<Point>
  • Mutables should not be used "by default", i.e. you should have a good reason to make something mutable.
  • "new" is usually only used in F# for IDisposables. You can just create a mutable list with

    1
    
    let cpt = List<Point>()
  • 1
    
    System.Collections.Generic.List<T>

    has a type alias in F# : ResizeArray

  • You can create an empty list with just

    1
    
    let cpt = []

    , F# will infer the element type. If for some reason you want to explicitly state the type, you can say

    1
    
    let cpt : list<Point> = []
By on 8/28/2011 6:50 AM ()

Sequence of points:

1
let cpt1 = seq[ Point(20,60); Point (40,50) ; Point (40,50)]

Sequence of tuple (Point*Point):

1
let cpt2 = seq[ (Point(20,60), Point (40,50)); (Point(20,60), Point (40,50)) ]

You can also create an array of (Point*Point):

1
let cpt = [| Point(20,60), Point (40,50) |]

Sequence of array of (Point*Point):

1
let cpt3 = seq[| Point(20,60), Point (40,50) |]

Combine cp2 and cp3 :

1
let cp4=Seq.append cp2 cp3

Emtpy Sequnece:

1
let cpt0 = Seq.Empty
By on 11/13/2011 2:37 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper