相关疑难解决方法(0)

F#Quotations - 遍历由Value表示的函数调用

我花了几个小时试图掌握F#Quotations,但我遇到了一些路障.我的要求是从一个有区别的联合类型中取出简单的函数(只是整数,+, - ,/,*)并生成一个最终将用于生成C代码的表达式树.我知道这可以使用带有"直接"功能的报价.

我的问题是表达式树似乎以"值"终止,我无法弄清楚如何遍历该值.

我的问题是在这种情况下这是否真的可行?还是有其他值得考虑的方法.

type FuncType = 
| A of (int -> int -> int)
| B
| C

[<ReflectedDefinition>]
let add x y = x + y


let myFunc1 = A (fun x y -> x + y )
let myFunc2 = A add 

let thefunc expr = 
    match expr with
    | A(x) ->
        <@ x @>
    | _ ->
        failwith "fail"

printfn "%A" (thefunc myFunc1) // prints "Value (<fun:myFunc1@14>)"
printfn "%A" (thefunc myFunc2) // prints "Value (<fun:myFunc2@15>)" …
Run Code Online (Sandbox Code Playgroud)

f#

4
推荐指数
1
解决办法
494
查看次数

标签 统计

f# ×1