我读过所有关于F#中值限制的文章,但我仍然不理解它。我有以下代码:
type tree<'a> =
| Nil
| Node of (tree<'a> * 'a * tree<'a>)
let rec flatten = function
| Nil -> []
| Node ( Nil, b, Nil ) -> [b]
| Node ( l, h, p ) -> List.concat [(flatten l);[h];(flatten p)]
并且编译器显示一个错误:
error FS0030: Value restriction. The value 'it' has been inferred to have generic type
val it : '_a list
Either define 'it' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.
有谁可以帮我?非常感谢;)
发布于 2010-11-01 19:21:03
请允许我使用我的通灵调试技能。不能调用flatten Nil
,因为正如编译器所指示的那样,结果可能是任何类型'a
的'a list
。必须添加类型批注,如(flatten Nil : int list)
。
在不相关的注释中,flatten定义中的第二个case是不必要的,可以删除,因为它也包含在第三个case中。
https://stackoverflow.com/questions/4072167
复制相似问题