在F#中,如何编写泛型数学步骤函数?
(Oliver)Heaviside阶跃函数是函数,如果x为负,则返回零,否则返回一个.
以下是我到目前为止的尝试摘要:
// attempt 1:
let inline stepFct1< ^T when ^T : (static member op_GreaterThan: ^T * float -> bool)
> (x:^T) : ^T =
//if (^T : (static member op_GreaterThan) (x 0.0) ) then x //ouch fails also
if (>) x 0.0 then x
else 0.0
Run Code Online (Sandbox Code Playgroud)
编译器说:错误FS0001:类型参数缺少约束'当^ T:比较'
// attempt 2:
let inline stepFct2<^T when ^T : (static member (>): ^T * ^T -> bool) > (x:^T) : ^T =
match x with
| x when x > …Run Code Online (Sandbox Code Playgroud)