访问F#元组中的特定成员 [英] Accessing a specific member in a F# tuple
本文介绍了访问F#元组中的特定成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在F#代码中,我有一个元组:
In F# code I have a tuple:
let myWife=("Tijana",32)
我想分别访问元组的每个成员.例如,这是我无法实现的目标
I want to access each member of the tuple separately. For instance this what I want to achieve by I can't
Console.WriteLine("My wife is {0} and her age is {1}",myWife[0],myWife[1])
此代码显然无效,因为我认为您可以收集我想要实现的目标.
This code doesn't obviously work, by I think you can gather what I want to achieve.
推荐答案
您想通过使其年龄不变来防止妻子衰老吗? :)
You want to prevent your wife from aging by making her age immutable? :)
对于仅包含两个成员的元组,可以使用fst
和snd
提取该对中的成员.
For a tuple that contains only two members, you can fst
and snd
to extract the members of the pair.
let wifeName = fst myWife;
let wifeAge = snd myWife;
对于更长的元组,您必须将元组解压缩为其他变量.例如,
For longer tuples, you'll have to unpack the tuple into other variables. For instance,
let _, age = myWife;;
let name, age = myWife;;
这篇关于访问F#元组中的特定成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文