I have some background in Algorithms and Data Structures. I've also spent some time writing programs in Object Oriented and Procedural ways(using C, C++, Java, etc.), but Functional way of thinking is quite new for me.
Almost every program uses classic data structures like Array(solid piece of memory), List(chunks of memory, connected by pointers), Set(based on a hash table or a tree-like structure), Map(based on a hash table or a tree-like structure).
I figured that purely functional environment has only 3 types out of these 6 classic data structures: List, Set(based on tree), Map(based on tree). I know, that some functional languages actually have mutable Array and even may be Set and Map(based on hash table), but I'm talking about purely functional approach.
Well, may be the lack of hash-based Set/Map is not very perceptible, but without my good old mutable array I feel quite uncomfortable. Consider an example:
Suppose I have a list of pairs ((1,3) (2,2) (1,4) (2,1) (0,9) ...) where each pair is (knapsack_number, weight_of_item) - basically each such pair is associated with an item, item has to go in particular knapsack(all knapsacks are enumerated) and it has some weight. And based on that I want to get a list of pairs ((0,27) (1,33) (2,18) ...) - where each pair is (knapsack_number, total_weight_of_items_in_that_knapsack).
If I were able to use mutable array, I could easily iterate over my incoming list once and have as a result an array of pairs quite efficiently. But I don't have mutable array in purely functional environment. So, what is the best solution can I come up with ? (The best thing I have in mind so far is to emulate an array using immutable map by having 0, 1, 2, ... as keys, is this the way to go when I need an array in purely functional environment?)