The problem is that your getting mixed up between System.Generics.List<A> (the list in BCL) and Microsoft.FSharp.Collections.List<A>, F#'s native list. In F# its common to refer to System.Generics.List<A> as ResizeArray to avoid this sort of confusion. You can only pattern match over the F# native list. These two types are not comptable, both implement the IEnumerable interface so its easy to transform between the two types. Use "List.of_seq" to create a F# native list from a ResizeArray and use "new ResizeArray(list)" to create a ResizeArray from a list. Cheers, Robert

By on 11/28/2008 2:56 AM ()

Hi.
Thanks for the reply. I've changed my code to use ResizeArray() instead. When using a list, you can say

let l = List.empty<string>
let h,t = (List.hd l), (List.tl l)
t |> List.fold_left (fun ...) h

The ResizeArray also has the fold_left function.
1. Is there a way to get the head and tail?
2. Currently, I'm using
let r = new string ResizeArray()
... add items ...
let l = List.of_seq r
... same as above ..
Is this the way to go?

Thank you.

By on 11/28/2008 6:11 AM ()

A ResizeArray is an array like structure, so while you can get it's head using Seq.hd, it doesn't really make sense to get it tail.

What your doing makes some sense, but it maybe simpler just stick with native F# list and define the contents as a literal:

let l = [ "toto"; "titi"; "tata" ]

Or use a Seq.map or a list comprehension if your reading the items from a file/database.

Cheers,

Rob

By on 11/28/2008 7:50 AM ()

Hi,
I really want to stick to the f# list as I'm new to f# and I want to use its types as often as possible. The issue is I have a class [Table] that contains columns [Column]. I used the column list at first but then in order to add new columns, I added the method addColumn. The definition is like

type Column(name) =
let mutable name = name
...
member x.Name
with get() = name
and set y = name <- y
....

type Table(name) =
let name = name
let mutable columns = List.empty<Column>
...
member x.Name ... same as above...
member x.addColumn col =
//this is where I have the issue

//option 1: This appends the new item to the front. Not what is required
columns <- col::columns

//option 2: This works fine except I read here that the @ operator is pretty expensive
columns <- columns@[col]

//option 3: change to a more familiar environment - .net list
[if .net list] columns.Add(col)

... later on ...
pattern matching on the list.

I'm currently learning f# so I really do not know most of the methods available for use. I'm currently familiar with map, fold_left, generate_using, iter and choose

Thank you.

By on 11/29/2008 4:09 PM ()
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