gpt4 book ai didi

f# - 无限类型(又名递归类型)在 F# 中是不可能的吗?

转载 作者:行者123 更新时间:2023-12-04 00:53:47 25 4
gpt4 key购买 nike

我正在和Sadek Drobi聊天在 twitter 上提到 F# 似乎不支持无限类型。事实证明,在 C# 中,您可以按照以下方式做一些事情:

delegate RecDelegate<T> RecDelegate<T>(T x);

但是,在对我们双方进行了一些实验之后,我们确定 F# 中的相同似乎是不可能的,无论是隐式还是显式。

显式:
type 'a specialF = 'a->specialF<'a>

error FS0191: This type definition involves an immediate cyclic reference through an abbreviation, struct field or inheritance relation.



隐式:
let rec specialF (x: 'a) = specialF

Type mismatch. Expecting a 'b but given a 'a -> 'b. The resulting type would be infinite when unifying ''b' and ''a -> 'b'.



当然,这些是故意简单的示例。

我想知道我是否弄错了。也许我错过了某种必要的注释?

最佳答案

你也可以做类似的事情

type 'a RecType = RecType of ('a -> 'a RecType)

创建一个命名类型,通过它来执行递归。现在这有效:
let rec specialF = RecType (fun _ -> specialF)

关于f# - 无限类型(又名递归类型)在 F# 中是不可能的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com