Domanda

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.

È stato utile?

Soluzione

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}}

Altri suggerimenti

concise code:

fun{Add I}
   fun{$ J} I+J end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top