【问题标题】:Compound function in MLML 中的复合函数
【发布时间】:2013-11-11 08:58:20
【问题描述】:

我需要在 ML 中编写一个函数复合,其行为如下:

Compound 接受 bin op f、int n 和值 x,并将运算符应用于 x N 次。这是一个例子:

compound f 0 x = x
compound f 1 x = x f x

我是 ML 的新手,所以我很难理解如何编写这个函数。我将非常感谢任何帮助!

【问题讨论】:

    标签: ml infix-notation


    【解决方案1】:

    您必须编写一个简化版的foldlfoldr 函数,具体取决于f 关联性。但是,由于您在示例中遗漏了括号,我想f 是右结合的。

    代码将用于您的foldr 案例:

    fun compound f x 0 = x
      | compound f x n = f(x, compound f x (n-1));
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2014-12-22
      • 1970-01-01
      • 2017-02-18
      • 2013-04-14
      • 2013-11-16
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多