I have this bit of code:
let rec hnz = if n = 0 then z else <@ (fun x -> %(h (n - 1) <@ x + %z @>)) n @>
converted from MetaOcaml example to http://www.cs.rice.edu/~taha/publications/journal/dspg04a.pdf
The article explains that the above example will give the following parameters 3 and .<1>. (in MetaOcaml notation):
.<(fun x_1 -> (fun x_2 -> (fun x_3 -> x_3 + (x_2 + (x_1 + 1))) 1) 2) 3>.
As you can see, x is replaced with x_1 , x_2 , etc., because x otherwise only refers to x to the innermost fun .
But in F # this is unacceptable. I get a compile-time error: "The variable" x "is bound in the quote, but is used as part of the spliced ββexpression. This is unacceptable as it may go out of scope." So the question is: how can this be changed so that it compiles and has the same semantics as the output of MetaOcaml?
Update comment: I use PowerPack to actually rate the quote. But I do not think that this is due to this, because the error is during compilation. Currently running QuotationEvaluation. However, I know that this is not the most efficient implementation.
Update to Thomas' answer: I really don't want x be global, or to avoid scope. But I want it equivalent
let rec hnz = if n = 0 then z else (fun x -> (h (n - 1) (x + z))) n
with quotes. Your answer gives (h 3 <@ 1 @>).Eval() = 4 , where the above gives h 3 1 = 7 . And here I want 7 be the answer.
f # metaprogramming metaocaml
Lasse espeholt
source share