function - 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.
解决方案
您的递归调用power
括号不正确。你要这个:
power x (n - 1)
你所拥有的解析是:power ((x n) - 1)
. 换句话说,正如编译器告诉你的那样,它试图x
像一个函数一样应用。
推荐阅读
- android - 如何在 Android 中获取 SPEED Dial 联系人列表?
- javascript - Meteor / React:通过表单输入和“点击”输入创建 MongoDB 条目
- kubernetes - 混合来自 kubernetes 部署和 docker 映像的环境变量
- mysql - 关于数据不一致的MYSQL数据库结构的建议
- docker - 在 docker-compose 文件中为我的服务选择重启策略
- javascript - react-calendar onChange 不要将日历设置为实际日期
- python - 有没有办法将我的 Python OLS 回归保存为 csv?
- reactjs - 如何动态禁用 Fluent UI 按钮?
- python - 将 kivy 文件 (.py) 转换为 apk 时,小部件不会自动调整到设备的窗口大小
- javascript - JavaScript中关于回调的树形图方法代码