元组项1的f#Seq

元组项1的f#Seq,f#,f#-interactive,F#,F# Interactive,我不明白为什么会出现以下错误: > let x = "ABCDAACCECFG"|>Seq.sort|>Seq.groupBy (fun x->x);; val x : seq<char * seq<char>> > x;; val it : seq<char * seq<char>> = seq [('A', seq ['A'; 'A'; 'A']); ('B', seq ['B']); ('

我不明白为什么会出现以下错误:

> let x = "ABCDAACCECFG"|>Seq.sort|>Seq.groupBy (fun x->x);;
val x : seq<char * seq<char>>

> x;;
val it : seq<char * seq<char>> =
  seq
    [('A', seq ['A'; 'A'; 'A']); ('B', seq ['B']);
     ('C', seq ['C'; 'C'; 'C'; 'C']); ('D', seq ['D']); ...]

> (x|> Seq.head).GetType();;
val it : System.Type =
  System.Tuple`2[System.Char,System.Collections.Generic.IEnumerable`1[System.Char]]
    {Assembly = mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;
     AssemblyQualifiedName = "System.Tuple`2[[System.Char, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IEnumerable`1[[System.Char, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
...
     DeclaredProperties = [|Char Item1;
                            System.Collections.Generic.IEnumerable`1[System.Char] Item2;
                            Int32 System.ITuple.Size|];
...;}

> x|> Seq.map (fun x -> x.Item1, x.Item2)|>dict;;

  x|> Seq.map (fun x -> x.Item1, x.Item2)|>dict;;
  ------------------------^^^^^

stdin(123,25): error FS0039: The field, constructor or member 'Item1' is not defined.
>让x=“ABCDAACCECFG”|>Seq.sort |>Seq.groupBy(fun x->x);;
val x:序号
>x;;
val it:seq=
序号
[('A',seq['A';'A';'A']);('B',seq['B']);
('C',seq['C';'C';'C';'C']);('D',seq['D']);…]
>(x |>Seq.head).GetType();;
valit:System.Type=
System.Tuple`2[System.Char,System.Collections.Generic.IEnumerable`1[System.Char]]
{Assembly=mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a561934e089;
AssemblyQualifiedName=“System.Tuple`2[[System.Char,mscorlib,Version=4.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IEnumerable`1[[System.Char,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=B77A561934E089]],mscorlib,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089”;
...
DeclaredProperties=[| Char Item1;
System.Collections.Generic.IEnumerable`1[System.Char]Item2;
Int32 System.ITuple.Size |];
...;}
>x |>Seq.map(乐趣x->x.Item1,x.Item2)|>dict;;
x |>Seq.map(乐趣x->x.Item1,x.Item2)|>dict;;
------------------------^^^^^
stdin(123,25):错误FS0039:未定义字段、构造函数或成员“Item1”。
在我看来,x是一个元组序列,其中每个元组都有一个Item1(Char)和Item2(seqchar)属性。我想把这个变成一本字典

很明显,我遗漏了一些东西。有人能帮我理解我做错了什么以及如何纠正吗?

看起来F#不像C#那样从底层元组中显示Item1和Item2属性。我尝试了以下方法,似乎没有错误

使用函数fst和snd:

x|> Seq.map (fun x -> fst x, snd x) |> dict
使用模式匹配:

x|> Seq.map (function (key, value) -> (key, value)) |> dict
显然,如果不使用Seq.map,它也可以工作:

x |> dict

首先,如果
x
已经是一个两元组,那么您试图做的只是一个身份转换,即等于
x |>Seq.map(fun x->x)|>dict
;-]
dict x
对于您的代码来说已经足够了,但是通常您通过模式匹配/分解来访问元组的元素,而不是通过
ItemN
来访问元组的元素。您可以使用函数
fst
snd
来访问元组元素,但根据第一条注释,你到底想做什么?也许有更好的方法吗?
.Item1
.Item2
属性不会为F#tuple公开;您需要使用模式匹配。例如,
fun(a,b)->……
将确保该函数中的名称
a
b
现在引用元组的第一项和第二项。但正如@ildjarn已经指出的,您的
(fun x->x.Item1,x.Item2)
函数完全没有任何作用,因此可以将其删除。谢谢,这正是我所需要的。我发现第二种(模式匹配)形式特别有用,因为我想做的一件事是创建一个包含值数组的键和计数(Seq.length)的字典。