function - OCaml - 打印递归fatorial函数返回值不起作用
问题描述
我有这个代码:
let n = read_int()
let rec fact n = if n=0 then 1 else n*fact(n-1)
let () = Printf.printf "%d factorial is %d.\n" n fact(n)
我编译然后编译器说:
File "fat.ml", line 3, characters 23-46:
Error: This expression has type
('a -> 'b, out_channel, unit, unit, unit, 'a -> 'b)
CamlinternalFormatBasics.fmt
but an expression was expected of type
('a -> 'b, out_channel, unit, unit, unit, unit)
CamlinternalFormatBasics.fmt
Type 'a -> 'b is not compatible with type unit
如何打印返回值?
解决方案
问题是缺少括号fact n
:
let () = Printf.printf "%d factorial is %d.\n" n (fact n)
作品。
您得到的复杂类型错误背后的原因是编译器读取
let () = Printf.printf "%d factorial is %d.\n" n fact(n)
作为
let () = Printf.printf "%d factorial is %d.\n" n fact n
换句话说,对于编译器,函数应用于printf
4 个参数:"%d factorial is %d.\n"
、n
和。fact
n
但是格式字符串,我们称之为fmt
,只包含两个%d
说明符。因此编译器也知道printf fmt
应该有两个参数,然后返回单位。存在差异:Printf.printf fmt n fact
预计返回一个可应用于最后一个参数的函数,n
但它返回单位。或者换句话说,
Type 'a -> 'b 与 type unit 不兼容
错误的前半部分
此表达式的类型为 ('a -> 'b, out_channel, unit, unit, unit, 'a -> 'b) CamlinternalFormatBasics.fmt 但表达式应为 ('a -> 'b, out_channel, unit, unit , 单位, 单位) CamlinternalFormatBasics.fmt
这是由于格式字符串的类型非常灵活,因此类型检查器仅在发现无法打印格式字符串并返回带有提供的参数的单位时才会失败。
推荐阅读
- node.js - 使用 ESP-IDF 从 node.js 服务器发送图像到 ESP32
- python - 在没有 Selenium Python 的情况下获取当前浏览器 URL
- file-transfer - 远程接收文件进行数据分析
- java - com.microsoft.sqlserver.jdbc.SQLServerException:列、参数或变量 #9:找不到数据类型 json
- docker - Docker 无法运行
- google-compute-engine - 在偶尔完整备份 Google Cloud Compute Engine 之前删除增量快照
- c++ - C++:使用常量引用结构创建 getter
- html - 汉堡图标动画的动画不起作用
- c - 在 C 中使用格式说明符打印
- python - 是否有 Python 编程可以在不使用 CMD 的情况下连接到路由器的 ssid?