我试图编写Batteries.LazyList.lazy_fold_right
的修改版本。我想要一个类似的函数折叠在两个惰性列表而不是一个惰性列表上。但是,我收到的错误对我没有任何意义。
以下是我从batLazyList.ml开始的原始电池定义:
let lazy_fold_right f l init =
let rec aux rest = lazy begin
match next rest with
| Cons (x, t) -> f x (aux t)
| Nil -> Lazy.force init
end in
aux l
这是我的版本:
let lazy_fold_right2 f l1 l2 init =
let open Batteries.LazyList in
let rec aux rest1 rest2 =
lazy begin
match next rest1, next rest2 with
| Cons (x1, t1), Cons (x2, t2) -> f x1 x2 (aux t1 t2)
| Nil, Nil | Nil, _ | _, Nil -> Lazy.force init
end
in
aux l1 l2
错误发生在行尾的变量init
上,有多个Nil
s:
Error: This expression has type int -> (int -> 'a) -> 'a t but an expression was expected of type 'b lazy_t
咦?代码中有哪些与int
s相关的内容?我没看到什么?
(Cons
,Nil
和next
都是在batLazyList.ml中为LazyList
定义的。)
答案 0 :(得分:1)
错误来自Batteries.LazyList
init
中隐藏本地Batteries.LazyList
变量的let lazy_fold_right2 f l1 l2 init =
let module L = Batteries.LazyList in
let rec aux rest1 rest2 =
lazy begin
match L.next rest1, L.next rest2 with
| L.Cons (x1, t1), Cons (x2, t2) -> f x1 x2 (aux t1 t2)
| Nil, Nil | Nil, _ | _, Nil -> Lazy.force init
end
in
aux l1 l2
函数。避免此问题的两种可能性是激活警告44(对于带阴影的本地标识符)或为{{1}}定义短别名而不是本地打开:
{{1}}