通过索引变量访问 SML 元组
问题很简单.
如何在 SML 中使用索引变量访问元组?
How to access a tuple by using Index variable in SML?
val index = 5;
val tuple1 = (1,2,3,4,5,6,7,8,9,10);
val correctValue = #index tuple1 ??
我希望有人能够提供帮助.提前致谢!
I hope, somebody would be able to help out. Thanks in advance!
不存在接受整数值和元组并从元组中提取该元素的函数.当然有 #1
, #2
, ... 函数,但这些函数不接受整数参数.也就是说,函数"的名称是#5
,它不是应用于值5
的函数#
.因此,您不能用名称 index
代替 5
.
There doesn't exist a function which takes an integer value and a tuple, and extracts that element from the tuple. There are of course the #1
, #2
, ... functions, but these do not take an integer argument. That is, the name of the "function" is #5
, it is not the function #
applied to the value 5
. As such, you cannot substitute the name index
instead of the 5
.
如果您事先不知道您想要的元素将位于元组中的哪个位置,那么您可能正在以不打算使用它们的方式使用它们.
If you don't know in advance at which place in the tuple the element you want will be at, you're probably using them in a way they're not intended to be used.
您可能需要一个值列表,对于这些值,'a list
类型更自然.然后,您可以使用 List.nth
访问 n
th 元素.
You might want a list of values, for which the 'a list
type is more natural. You can then access the n
th element using List.nth
.