I was suprised to learn that Array
and List
were two different types in Elm:
Array
List
In my case,
List
is a linked list which provides O(n) lookup time based on index. Getting an element by index requires traversing the list over n
nodes. An index lookup function for List
isn't available in the core library but you can use the elm-community/list-extra package which provides two functions for lookup (varying by parameter order): !! and getAt.
Array
allows for O(log n) index lookup. Index lookups on Array
can be done using Array.get. Arrays are represented as Relaxed Radix Balanced Trees.
Both are immutable (all values in Elm are immutable), so you have trade-offs depending on your situation. List
is great when you make a lot of changes because you are merely updating linked list pointers, whereas Array
is great for speedy lookup but has somewhat poorer performance for modifications, which you'll want to consider if you're making a lot of changes.