【发布时间】:2017-07-28 06:42:25
【问题描述】:
如果我这样定义一个反向函数:
let reverse =
let rec helper out = function
| [] -> out
| a :: l -> helper (a :: out) l
in helper []
然后reverse (List.map reverse xs) 不进行类型检查,出现错误
Error: This expression has type 'a list list
but an expression was expected of type 'a list
The type variable 'a occurs inside 'a list
但是用明确的参数定义它
let reverse l =
let rec helper out = function
| [] -> out
| a :: l -> helper (a :: out) l
in helper [] l
让事情顺利进行。
这是怎么回事?
【问题讨论】:
标签: types functional-programming polymorphism ocaml