ocaml - 我在使用 ocaml 多态函数时遇到了一些麻烦

标签 ocaml ml caml toplevel

我需要你的帮助,我的代码错误在哪里?

let create = Array.make_matrix 10 10;;

let assoc int = create int,create (char_of_int int);;

错误是

3 | let assoc int = create int,create (char_of_int int);;
                                      ^^^^^^^^^^^^^^^^^
Error: This expression has type char but an expression was expected of type
         int

最佳答案

当你在 Ocaml 上隐式定义一个多态函数时,它有一个“弱类型”,这意味着在你调用它一次之后,类型肯定会被分配给该函数,所以因为你调用了 int 上的 create,所以现在它具有 int -> int 数组类型,并且不接受 char 作为参数。

关于ocaml - 我在使用 ocaml 多态函数时遇到了一些麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67040484/

相关文章:

f# - 在 F#/OCaml 中实现类似快速排序的函数的尾递归版本

load - 可编译但不符合顶层的 Ocaml 代码

sharepoint - 具有给定 URL 的 CAML 查询项

sharepoint - CAML > 通过 URL 获取项目

ocaml - 输入Ocaml的一个奇怪示例

sml - 问ML递归函数

OCaml 物理和结构平等

haskell - 为什么 ML/Haskell 数据类型对于像算术表达式一样定义 "languages"很有用?

sharepoint - 为列表定义中的 View 启用内联编辑

sorting - OCaml - 如何对对进行排序?