geeko
geeko

Reputation: 2852

Why there is no F# immutable arrays?

I am new to F# and I noticed that arrays are still mutable. Is this because of the performance implications or because the inherent .NET CLR type system? I am aware that immutable lists do exist.

Thank you

Upvotes: 6

Views: 900

Answers (2)

Jwosty
Jwosty

Reputation: 3644

Many people agree with you that F# should have immutable arrays, including the language creators; there's an issue covering it in the fslang-suggestions clearing house, currently in approved-in-principle state.

As for why it wasn't there from the start, it was likely just a low priority compared to other features.

Upvotes: 5

citykid
citykid

Reputation: 11070

Like OCaml, F# is a pragmatic instead of a pure language. This is what some people, like me, like the most on these languages.

In OCaml, arrays are mutable, and F# bridges the OCaml and the .NET CLR libaries. It makes perfect sense, to have mutable arrays then.

Why does ocaml have mutable arrays?

For beginners, this bridging between the 2 worlds is certainly a bit disturbing, the best is to take things as they are and carry on with learning the language. As said at the beginning, think pragmatic not pure.

I am not sure about your saying about mutable lists. F# lists are immutable. You can use .Net CLR System.Collection.Generic.List which is mutable. But when talking about lists, FSharpers mean the immutable [], [1..3], or by name: Microsoft.FSharp.Collections.FSharpList`1 .

F# collections:

https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/fsharp-collection-types

Immutable core types are list, map and set.

Upvotes: 1

Related Questions