5

当我有这个代码时:

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

编译器给出以下错误:

递归值不能直接作为递归绑定中“HtmlNode”类型的构造出现。此功能已从 F# 语言中删除。考虑改用记录。

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

4

1 回答 1

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
于 2014-05-01T19:02:19.183 回答