Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you make a growing collection in F#

Tags:

f#

Is there a collection type in f# that has an "append" method, such that I can add an element to that collection as such:

(pseudo code)

let list = [1,2,3,4]
list.append( 5 )
print list

result : 1,2,3,4,5

Basically i need a collection that can grow at run time, I cant assume what size it will be. I couldn't find a way to do this with list or array in f#

UPDATE: I dont want to create a new list/array every time this will be happening many times. I want to append the already existing collection. I need to use the same name/symbol for the collection. F# doesn't allow me to redeclare or over write a collection, so this wouldnt work:

let A = [1,2,3]
let A = A.append 4  or A <- Array.appen A 4
like image 863
Siavash Avatar asked Dec 03 '25 01:12

Siavash


1 Answers

You can use ResizeArray:

let list = ResizeArray([1;2;3;4])
list.Add( 5 )
like image 105
Lee Avatar answered Dec 06 '25 05:12

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!