首页 > 解决方案 > OCaml 幂函数

问题描述

我正在尝试编写 Ocaml 幂函数,但出现错误。下面是我的代码。

let rec power x n = 
if n = 0 then 1 
else x * power (x n-1)

Error: This expression has type int
       This is not a function; it cannot be applied.

标签: functionrecursionocaml

解决方案


您的递归调用power括号不正确。你要这个:

power x (n - 1)

你所拥有的解析是:power ((x n) - 1). 换句话说,正如编译器告诉你的那样,它试图x像一个函数一样应用。


推荐阅读