nic*_*las 2 f# generic-programming functor typeclass f#-unquote
我使用Unquote并没有看到任何近似的内容.所以我决定写一个.
let inline (=~=) x y = abs x-y < 1.E-10
Run Code Online (Sandbox Code Playgroud)
然而,运营商没有映射到Lists上
let test = [1;2] =~= [1;2] //---> error
Run Code Online (Sandbox Code Playgroud)
是否可以声明此运算符流动如何(=)?
或者它需要定义像'StructuralEquality-ishness'这样的新特征?
使用http://code.google.com/p/fsharp-typeclasses/来定义新的运算符是否更好?
我不知道Unquote,但关于近似函数/运算符,我不确定是否有办法通过结构比较来实现它.
如果你想"手动",使用类似于用于F#Typeclasses项目的技术(或技巧),这里是一个例子:
type Approximate = Approximate with
static member inline ($) (Approximate, x:^n ) = fun (y:^n) -> float (abs (x-y)) < 1.E-10
static member inline ($) (Approximate, x:list< ^n>) =
fun (y:list< ^n>) ->
x.Length = y.Length && (List.zip x y |> List.forall ( fun (a,b) -> (Approximate $ a) b))
// More overloads
let inline (=~=) x y = (Approximate $ x) y
Run Code Online (Sandbox Code Playgroud)