Hi,

When you use tuples, you need to know the number of elements at compile-time (number of elements is part of the type).

The clean way to mix different types in a collection is to use a sum type:

type data = Int of int | String of string

let myList = [Int 3; String "foo"; Int 8]

If you prefer to have a list of "anything", you can use the "box" function, but this is less safe (you might get run-time exceptions):

let myList = [box 3; box "foo"; box 8]

Laurent.

By on 9/24/2008 6:08 AM ()

You can't express this function in the F# type system (given an existing string*string tuple, the return type of the function would be dependent on the length of the list passed in - keep in mind that e.g. "(string*string)*int" and "((string*string)*int)*int" are different types), or in most language's static type systems, for that matter. This is not what tuples are useful for; when there is an arbitrary "count" of data, use a list, not a tuple; tuples have fixed counts/types.

Is there a bigger problem/scenario you're trying to tackle here, or was this just the result of experimenting?

By on 9/23/2008 5:49 PM ()

Is there a bigger problem/scenario you're trying to tackle here, or was this just the result of experimenting?

The bigger problem (which itself is really experimentation) is that I want a mixed collection string * int. This certainly seems to me like a tuple. I can't have a list of mixed types, correct?

I need (int * string * string), but I also need a function that will add one item at a time. Perhaps I'm fundamentally confused about tuples.

By on 9/23/2008 8:06 PM ()

You're right that tuples are useful for 'mixed types'. Perhaps you want a tuple of a string list and an int list? (Are there an arbitrary number of strings plus an arbitrary number of ints?) How do you intend to 'consume' the data you're creating in the experiment?

By on 9/23/2008 9:25 PM ()

Perhaps you want a tuple of a string list and an int list?

Yes, this is what I really want; thanks.

How do you intend to 'consume' the data you're

creating in the experiment?

I ultimately want a string list. An intermediate step is to double one of the items (which is an int) before converting to the final list. Actually, the whole thing is a bit contrived - I'm creating a sample app that would do something useful in my group's domain while taking advantage of as many aspects of F# as possible. I want to involve tuples mostly to demonstrate a fairly loose (but still typed) collection of values.

Thanks also to LLB; that's also helpful.

By on 9/24/2008 6:56 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