Ocaml 合并2个延迟列表类型冲突

Ocaml 合并2个延迟列表类型冲突,ocaml,lazy-evaluation,lazy-sequences,Ocaml,Lazy Evaluation,Lazy Sequences,为什么我的merge函数抱怨它的类型 我的xatype'a seq type 'a seq = Stop | Cons of 'a * (unit -> 'a seq) let rec linear start step= (*builds a seq starting with 'start'*) Cons (start, fun () -> linear (start+step) step) let rec take n seq = match seq with (*

为什么我的
merge
函数抱怨它的类型

我的
x
a
type'a seq

type 'a seq = Stop | Cons of 'a * (unit -> 'a seq)

let rec linear start step= (*builds a seq starting with 'start'*)
    Cons (start, fun () -> linear (start+step) step)

let rec take n seq = match seq with (*take first n elem from seq*)
| Stop -> []
| Cons (a, f) -> if n < 1 then [] else a::(take (n-1) (f ()))

let rec merge seq1 seq2 = match seq1, seq2 with 
    | Stop, _ -> seq2
    | _, Stop -> seq1
    | Cons(h1, tf1), _ as x -> 
        Cons(h1, fun () -> merge (x) (tf1 ()))

let l1 = linear 1 1
let l2 = linear 100 100 
let l3 = interleave l1 l2
int list=[1;100;2;200;3;300;4;400;5;500]

编写函数的另一种方法是

let rec merge seq1 seq2 = match seq1 with
| Stop -> Stop
| Cons (h, tf) -> Cons(h, fun () -> merge seq2 (tf ()))
但是我不明白,为什么第一次合并不起作用

谢谢。

只需编写
(uu as x)
,因为在这里,您的
as x
捕获了整个模式

那么,你所看到的是:

| Cons(h1, tf1), (_ as x) -> ...
实际上被解析为

| (Cons(h1, tf1), _) as x -> ...
你可以写:

| Cons(h1, tf1), x -> ...
哪个更好;-)

甚至

| Cons(h1, tf1), _ -> Cons(h1, fun () -> merge seq2 (tf1 ()))
只需编写
(\uasx)
,因为在这里,您的
asx
捕获了整个模式

那么,你所看到的是:

| Cons(h1, tf1), (_ as x) -> ...
实际上被解析为

| (Cons(h1, tf1), _) as x -> ...
你可以写:

| Cons(h1, tf1), x -> ...
哪个更好;-)

甚至

| Cons(h1, tf1), _ -> Cons(h1, fun () -> merge seq2 (tf1 ()))