[英]CPS in OCaml: type doesn't check
I'm working on a very simple OCaml exercise on CPS. 我正在CPS上进行非常简单的OCaml练习。 Line 8-10 is to convert two recursive calls into one tail recursion.
第8-10行是将两个递归调用转换为一个尾递归。 However, the compiler complains the type of Line 8:
但是,编译器抱怨第8行的类型:
File "tmp.ml", line 8, characters 9-14:
文件“ tmp.ml”,第8行,字符9-14:
Error: This expression has type int -> int -> (int -> int) -> int but an expression was expected of type int
错误:此表达式的类型为int-> int->(int-> int)-> int,但应使用int类型的表达式
I understand that the compiler expects an int
at line 8 because line 6 returns an int
. 我知道编译器期望第8行是
int
,因为第6行返回int
。 But can someone illustrate why the type of Line 8-10 is not an int? 但是有人可以说明为什么8-10行不是int类型吗?
4 let rec f i n k (i:int) (n:int) (k:int->int) :int =
5 if i + n < 0 then
6 k 1
7 else
8 (f i (n-1) (fun v ->
9 f (i-1) n (fun vv->
10 k (v + vv))))
11 in f 1 1 (fun x -> x)
fi n-1
is parsed as (fin)-1
rather than the fi (n-1)
you presumably expect. fi n-1
被解析为(fin)-1
而不是您可能期望的fi (n-1)
。
Additionally, 另外,
let rec f i n k (i:int) (n:int) (k:int->int) :int
means that your function takes 6 arguments: i
, n
, k
, i
, n
and k
. 表示您的函数采用6个参数:
i
, n
, k
, i
, n
和k
。 You probably meant to write: 您可能打算写:
let rec f (i:int) (n:int) (k:int->int) :int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.