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

戏子无情 提交于 2019-12-13 19:05:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!