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!
The sml function has O(1) complexity, so feel free to use it! e.x.
`fun isTrue (n,tup) =
if Array.sub(tup,n) then true
else false;`
As far as the tuple is concerned, you can only use a specific number, not a variable in a tuple.
e.x.
fun isTrue (n,tup) =
if #2 (tup) then true
else false;