2

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

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

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

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

4

1 回答 1

1

您必须编写一个简化版本的函数foldlor foldr,具体取决于f关联性。但是,由于您在示例中错过了括号,我想f是正确的关联。

该代码将适用于您的foldr情况:

fun compound f x 0 = x
  | compound f x n = f(x, compound f x (n-1));
于 2013-11-11T00:58:20.777 回答