> Languages that support it via square brackets: Rust, Ruby, Javascript, 
> Python, C, Julia.

All of these languages (other than maybe Julia? I have not used it at all.) are 
actually using arrays and not lists. It is fairly natural to have easy index 
based lookup for arrays. After all, it is meant to be a contiguous block of 
memory with each element having a known size. At least for Rust and C, other 
languages may be more loose in terms of implementation.

In fact, when you use std::collections::LinkedList in rust, you do not have 
this kind of syntax. If you want to find the element at index i you would need 
to iterate over the list just like ocaml. There is no provided function to my 
knowledge to even access a random element.

When you choose a random element from an array, you just need to do some math 
in order to find the offset in memory for where the element you are looking for 
resides. This means that random access is constant time or O(1) if you prefer. 
Having a convenient syntax for this makes sense since the time complexity is so 
low.

Linked lists, which Elixir and Erlang use, are not necessarily stored as a 
single contiguous block of memory. It is essentially a value and a pointer to 
the next element in the list. This means you actually need to traverse the list 
to the point that you want to access. This means that random access is 
considered to be linear time or O(n). Having a convenient syntax for this makes 
less sense since the time complexity is significantly higher. This is the 
tradeoff that most functional languages have taken, make it harder to do the 
expensive thing and get people to think about the data structures being used.

Depending on what exactly you are doing, and the size of your lists, it may be 
worth taking a look at the :array module provided by Erlang. It will give you 
logarithmic access times, or O(log n). But it still has the downside of not 
having [] type access.

Justin

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/311c75ed-b0fc-4660-945d-b190e294b939%40app.fastmail.com.

Reply via email to