我正在尝试定义一个函数factorize,它使用类似于Seq.sum的结构类型约束(需要静态成员Zero,One,+和/),以便它可以与int,long,bigint一起使用,我似乎无法获得正确的语法,并且无法在该主题上找到很多资源。这就是我所拥有的,请帮助。
let inline factorize (n:^NUM) =
^NUM : (static member get_Zero: unit->(^NUM))
^NUM : (static member get_One: unit->(^NUM))
let rec factorize (n:^NUM) (j:^NUM) (flist: ^NUM list) =
if n = ^NUM.One then flist
elif n % j = ^NUM.Zero then factorize (n/j) (^NUM.One + ^NUM.One) (j::flist)
else factorize n (j + ^NUM.One) (flist)
factorize n (^NUM.One + ^NUM.One) []
答案 0 :(得分:22)
以下是我写的方式:
module NumericLiteralG = begin
let inline FromZero() = LanguagePrimitives.GenericZero
let inline FromOne() = LanguagePrimitives.GenericOne
end
let inline factorize n =
let rec factorize n j flist =
if n = 1G then flist
elif n % j = 0G then factorize (n/j) j (j::flist)
else factorize n (j + 1G) (flist)
factorize n (1G + 1G) []
这里推断出因子分解的类型太笼统了,但是函数会像你期望的那样工作。如果需要,可以通过向某些泛型表达式添加显式类型来强制使用更合理的签名和约束集:
let inline factorize (n:^a) : ^a list =
let (one : ^a) = 1G
let (zero : ^a) = 0G
let rec factorize n (j:^a) flist =
if n = one then flist
elif n % j = zero then factorize (n/j) j (j::flist)
else factorize n (j + one) (flist)
factorize n (one + one) []
答案 1 :(得分:7)
受到kvb使用NumericLiterals的回答的启发,我开始开发一种方法,允许我们强制“理智”类型的签名,而无需添加大量的类型注释。
首先,我们为语言原语定义一些辅助函数和包装器类型:
let inline zero_of (target:'a) : 'a = LanguagePrimitives.GenericZero<'a>
let inline one_of (target:'a) : 'a = LanguagePrimitives.GenericOne<'a>
let inline two_of (target:'a) : 'a = one_of(target) + one_of(target)
let inline three_of (target:'a) : 'a = two_of(target) + one_of(target)
let inline negone_of (target:'a) : 'a = zero_of(target) - one_of(target)
let inline any_of (target:'a) (x:int) : 'a =
let one:'a = one_of target
let zero:'a = zero_of target
let xu = if x > 0 then 1 else -1
let gu:'a = if x > 0 then one else zero-one
let rec get i g =
if i = x then g
else get (i+xu) (g+gu)
get 0 zero
type G<'a> = {
negone:'a
zero:'a
one:'a
two:'a
three:'a
any: int -> 'a
}
let inline G_of (target:'a) : (G<'a>) = {
zero = zero_of target
one = one_of target
two = two_of target
three = three_of target
negone = negone_of target
any = any_of target
}
然后我们有:
let inline factorizeG n =
let g = G_of n
let rec factorize n j flist =
if n = g.one then flist
elif n % j = g.zero then factorize (n/j) j (j::flist)
else factorize n (j + g.one) (flist)
factorize n g.two []
[编辑:由于F#2.0 / .NET 2.0的明显错误,factorizen,factorizeL和factorizeI在发布模式下编译时运行速度明显慢于factorizeG,但运行速度稍快预期 - 见F# performance question: what is the compiler doing?]
或者我们可以更进一步(受专家F#启发,第110页):
let inline factorize (g:G<'a>) n = //'
let rec factorize n j flist =
if n = g.one then flist
elif n % j = g.zero then factorize (n/j) j (j::flist)
else factorize n (j + g.one) (flist)
factorize n g.two []
//identical to our earlier factorizeG
let inline factorizeG n = factorize (G_of n) n
let gn = G_of 1 //int32
let gL = G_of 1L //int64
let gI = G_of 1I //bigint
//allow us to limit to only integral numeric types
//and to reap performance gain by using pre-computed instances of G
let factorizen = factorize gn
let factorizeL = factorize gL
let factorizeI = factorize gI
此外,这里是kvb的NumericLiteralG的扩展版本,它允许我们使用“2G”,“ - 8G”等。虽然我无法弄清楚如何实现一个记忆策略(虽然这对G来说应该是可行的.ANY)。
module NumericLiteralG =
let inline FromZero() = LanguagePrimitives.GenericZero
let inline FromOne() = LanguagePrimitives.GenericOne
let inline FromInt32(n:int):'a =
let one:'a = FromOne()
let zero:'a = FromZero()
let nu = if n > 0 then 1 else -1
let gu:'a = if n > 0 then one else zero-one
let rec get i g =
if i = n then g
else get (i+nu) (g+gu)
get 0 zero
答案 2 :(得分:4)
首先,这是一个简单的例子,展示了语法的外观:
let inline zero< ^NUM when ^NUM : (static member get_Zero: unit-> ^NUM)>
(n:^NUM) =
(^NUM : (static member get_Zero : unit -> ^NUM) ())
在某些情况下,您不需要显式地编写约束(如果您编写上面的内容,F#编译器实际上会警告您),因为编译器知道一些静态成员并且有标准函数使用它们。因此,您可以使用该函数,编译器将推断约束:
let inline zero (n:^T) =
LanguagePrimitives.GenericZero< ^T >
不幸的是,这对你没有帮助,因为递归函数不能被声明为inline
(原因很明显 - 编译器无法在编译时内联函数,因为它不知道多少次) ,因此静态约束可能不足以解决您的问题。
[编辑:这实际上可以用于某些功能(参见kvb的回答)]
我认为你需要NumericAssociations
,这已被讨论in this question(这些是在运行时处理的,因此它们较慢 - 但用于实现例如F#矩阵类型 - 矩阵可以缓存动态获取的信息,因此效率相当高。)