创建递归区分联合值

Gus*_*rra 5 recursion f# discriminated-union

当我有这个代码时:

type HtmlNode = 
    | HtmlElement of name:string * attribute:HtmlAttribute list
    | HtmlText of content:string

and HtmlAttribute =  
    | HtmlAttribute of name:string * value:string * parent:HtmlNode

let createElement name attrs =
    let toAttributes element = [ for name, value in attrs -> HtmlAttribute(name, value, element)]
    let rec element = HtmlElement(name, attributes)
    and attributes = toAttributes element
    element
Run Code Online (Sandbox Code Playgroud)

编译器给出以下错误:

递归值不能直接显示为递归绑定中"HtmlNode"类型的构造.此功能已从F#语言中删除.请考虑使用记录.

这是为什么?let rec应该支持递归值的创建,类似的东西也适用于记录.

Dan*_*iel 2

我不知道为什么要改变这一点,但一种解决方法是使用seq而不是list.

type HtmlNode = 
    | HtmlElement of name:string * attribute:HtmlAttribute seq
    | HtmlText of content:string

and HtmlAttribute =  
    | HtmlAttribute of name:string * value:string * parent:HtmlNode

let createElement name attrs =
    let rec element = HtmlElement(name, attributes)
    and attributes = seq { for name, value in attrs -> HtmlAttribute(name, value, element) }
    element
Run Code Online (Sandbox Code Playgroud)