How to concatenate lists (and other collections) in F#?
sequence and list together
There is no special function for this. If the sequence is first and the list is the second, then you have to choose between converting the first one to list (and then copying it when appending using List.append
) or using Seq.append
followed by List.ofSeq
which will copy both lists.
So it would make sense to write your own function.
list and list together into a list? (non-destructive)
List.append
does this.
list and list together into a list if it is destructive
Lists are immutable, so there is no destructive append.
mutable arrays together, destructively, into another mutable array?
In .NET, you cannot resize arrays, so there is no destructive way of doing that. Array.append
creates a new array (and would be faster than other options, because it knows the size of the result in advance).
And can you concatenate tuples too?
No. The type system does not let you express the type of a function that would append tuples (they have to have a statically known size).
The @ operator is a simple & tidy way to join multiple lists:
let allElements = list1 @ list2 @ list3 @ list4 @ list5 @ list6