问题
I am a beginner in mozart oz, and I would like to write a simple higher order function, like {{Add 1}2}, the result of which has to be 3. I guess this is something like nested call in C, where a function could call itself? I am not sure how to define this function, should I write
declare
fun {Add I}
or
declare
fun {{Add I}J}
? And I really don't know how to finish such a function. I have tried several times, but I never have it worked.
回答1:
Something like this should work (untested):
declare
fun {Add I}
% define a local function which adds I to its argument
fun {Adder J}
J + I
end
in
% returns this new function
Adder
end
{Show {{Add 1} 2}} % should print 3
% or more verbose:
declare
Add1 = {Add 1}
{Show {Add1 2}}
回答2:
concise code:
fun{Add I}
fun{$ J} I+J end
end
来源:https://stackoverflow.com/questions/14316965/how-to-write-a-simple-higher-order-function-in-mozart-oz