F#值限制

877*_*877 5 f# value-restriction

我已经阅读了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)]
Run Code Online (Sandbox Code Playgroud)

并且编译器显示错误:

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.
Run Code Online (Sandbox Code Playgroud)

谁能帮我?非常感谢你;)

kvb*_*kvb 11

请允许我使用我的通灵调试技巧.您无法调用,flatten Nil因为正如编译器指示的那样,结果可能是'a list任何类型的结果'a.您必须添加类型注释,例如(flatten Nil : int list).

在一个不相关的注释中,你在flatten定义中的第二个案例是不必要的,可以删除,因为它也被第三个案例所涵盖.