I am writing a piece of code in SML/NJ and need, at some point, to access a list, which I have already created. I know that, in C, for example, accessing an array takes constant time. So, I thought that that would be the case for ML as well. Apparently, however, the built-in List.nth(l,i) function has a complexity that is linear to the size of the list given as argument.
I then turned to arrays, but I think that the Array.sub function has, also, linear complexity.
So, given the fact the accesing a tuple, like #2(12,5.6,"foo"), has an O(1) complexity, I would like to ask whether there is a way, I could use a tuple, instead of a list,but access it dynamically.
For example, say I want to write a function that takes a tuple, with only boolean values, and an integer n, and returns True if the n-th element of the tuple is true.Something like:
fun isTrue (n,tup) = if #n(tup) then true else false;
I know this isn't valid SML, so is there a way to write such a function?
Thanks a lot in advance for your time!