Tim Lovell-Smith
Tim Lovell-Smith

Reputation: 16125

What is difference between lists, arrays, and tuples in F#?

What is difference between lists, arrays, and tuples in F#? Is it easy to convert between them when required? They seem all similar in spirit, so what do actually I need to know that lets me understand when to use one versus another?

Upvotes: 10

Views: 11338

Answers (2)

Turkka Mannila
Turkka Mannila

Reputation: 91

About using collection types:

Lists when: You need a dynamic collection that changes in size. List is a one directional linked list so keep in mind that every member of list requires 4 or 8 bytes of additional memory storing pointer to next list element.

Arrays when: You need to store large amount of primitive values, like ints, floats or bytes. Arrays represent a static block of memory reserved for the values, this is efficient when you have to store for example bytes of an image.

Additional useful collections:

Sets when: You need to store, well a set. Set is a collection of unique values. Set has useful functions to calculate difference, union and intersection. For example you might have set of all people (A) and set of people who bought candy (B) so you can now calculate A - B = C, now you have set C which represents people who didn't buy candy.

Maps when: You need a projection from one value to another. Now in previous example you calculated set C (people who didn't buy candy), you probably used some id to describe these people. Now you can project from this id to the person record using Map :).

I hope this was useful.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564433

A tuple is a grouping of unnamed, ordered values. Each value in a tuple does not need to be the same type, which means you can have a tuple defined like:

let someTuple = (1, "foo", 42.3)

This would create a tuple containing an int, string, and float value.

A list is an ordered collection of values of the same type which is immutable. This is probably the most common collection type used in functional programming, as it's completely immutable and has a lot of functionality for creating lists built on top of previously created lists, which allows you to make collections that "grow" (they're actually new collections, however, as the lists are immutable).

An array is a fixed size, mutable collection. They are very efficient to create, but must always be a single type.

Is it easy to convert between them when required?

It's very easy to convert between lists and arrays. You can use List.ofArray, List.toArray, Array.ofList, and Array.toList to convert between the types.

Converting from tuple to array or list is not common, and not always possible, as tuples allow multiple types to be stored within them.

They seem all similar in spirit, so what do actually I need to know that lets me understand when to use one versus another?

Lists and arrays are both used for collections. In general, you'll want to prefer lists if you're going to be making lists that "grow", as making a new list comprised of an element + the original list is much more efficent than doing the same thing with an array.

Arrays are often used for higher performance scenarios (they have better memory locality, are more space efficient, etc), but are mutable and fixed size, so they're often not ideal when you're trying to work with constructing collections.

Tuples are typically used for completely different scenarios. The most common use case for tuples is to pass around multiple items as one value. This happens automatically when using framework methods that have out parameters, for example, so you'll see use cases like:

let (success, value) = int.TryParse(someString)

In this case, the tuple is automatically created and then pattern matched to extract the values in a single line of code. Instead of thinking of a tuple as a "collection", it's more of a way to hold multiple values, often of different types, together.

Upvotes: 14

Related Questions