How to write a simple higher order function in mozart oz?

1.3k views Asked by At

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.

2

There are 2 answers

4
wmeyer On BEST ANSWER

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}}
0
yakoudbz On

concise code:

fun{Add I}
   fun{$ J} I+J end
end