未解析的 flex 记录(需要知道此上下文中所有字段的名称) [英] unresolved flex record (need to know the names of ALL the fields in this context)
问题描述
我一直在尝试使用元组列表作为参数创建一个函数,但我不断收到错误消息:未解析的弹性记录(需要知道此上下文中所有字段的名称)" 我的代码是:
I've been trying to create a function with a tuple-list as an argument but I keep getting the error: "unresolved flex record (need to know the names of ALL the fields in this context)" My code is:
fun convert d = ( (map (#1) d) , (map (#2) d) );
这基本上是试图将一对列表转换为一对列表.我还尝试将 d
的类型声明为 :('a * 'b) list
但这导致了更多错误.我认为这与元组的未知大小有关,可以使用一些帮助来了解它.
This is basicaly trying to convert a list of pairs into a pair of lists.I've also tried to declare the type of d
as :('a * 'b) list
but that resulted in even more errors.
I assume that this has something to do with the unknown size of the tupple and could use some help on how to make it known.
推荐答案
我怀疑您在注释 d
类型时遇到的问题实际上只是您没有用括号,这对我有用:
I suspect that the issue you ran into with annotating the type of d
was actually just that you weren't surrounding the annotation with parens, this works for me:
fun convert (d : ('a * 'b) list) = ( (map (#1) d) , (map (#2) d) )
然而,这不是很好的 SML 风格.不鼓励使用 #1
、#2
、#n
等,因为它会导致这样的问题,你已经失去了一些类型推断为您提供的通常简洁性.
However, this isn't great SML style. Using #1
, #2
, #n
and so on is somewhat discouraged since it leads to issues like this, where you've lost some of the usual succintness that type inference gives you.
相反,您可以为对定义一些显式选择函数:
Instead, you can define some explicit selection functions for pairs:
fun first (a, _) = a
fun second (_, b) = b
fun convert d = (map first d, map second d)
(请注意,我还从 convert
的主体中删除了一些多余的括号,因为函数应用程序的优先级高于元组构造,而且我还删除了分号这仅在对命令式代码进行排序或在 REPL 中键入代码时才是真正需要的)
(Note that I've also dropped some superfluous parenthesis from the body of convert
, since function application has higher precedence then tuple construction, and I've also dropped the semi-colon which is only really necessary when sequencing imperative code or when typing code into the REPL)
这是一个非常好的标准风格指南ML,来自哈佛(或塔夫茨大学)的课程.该文档的旧版本在要避免的常见错误"下特别提到了这一点.
This is a pretty good style guide for Standard ML, from a course at Harvard (or maybe Tufts). An older version of that doc mentioned this specifically under "Common mistakes to avoid".
一些初学者认为获得一对 p
的第二个元素的好方法是编写 #2 p
.
Avoid #1 and #2
Some beginners pick up the idea that a good way to get the second element of a pair
p
is to write#2 p
.
这种风格不符合习惯或可读性,它可能会混淆类型检查器.处理对的正确方法是通过模式匹配,所以
This style is not idiomatic or readable, and it can confuse the type checker. The proper way to handle pairs is by pattern matching, so
fun first (x, _) = x
fun second (_, y) = y
是首选,而不是
fun bogus_first p = #1 p (* WRONG *)
fun bogus_second p = #2 p
(由于我不想讨论的原因,这些版本甚至不进行类型检查.)
(For reasons I don’t want to discuss, these versions don’t even type-check.)
如果您的对或元组不是函数的参数,请使用 val
进行模式匹配:
If your pair or tuple is not an argument to a function, use val
to do the pattern matching:
val (x, y) = lookup_pair mumble
但通常你可以在普通有趣的匹配中包含匹配.
But usually you can include matching in ordinary fun matching.
这篇关于未解析的 flex 记录(需要知道此上下文中所有字段的名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!