如何用莫扎特盎司编写一个简单的高阶函数? [英] How to write a simple higher order function in mozart oz?

查看:128
本文介绍了如何用莫扎特盎司编写一个简单的高阶函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mozart oz的初学者,我想编写一个简单的高阶函数,例如{{Add 1} 2},其结果必须为3.我猜这有点像嵌套调用C,函数可以在哪里调用自身?我不确定如何定义此函数,我应该写

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}

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.

推荐答案

类似的事情应该起作用(未经测试):

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

这篇关于如何用莫扎特盎司编写一个简单的高阶函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆