gpt4 book ai didi

syntax - Ocaml 语法错误

转载 作者:行者123 更新时间:2023-12-02 07:15:14 26 4
gpt4 key购买 nike

我正在使用惰性列表的实现,其中类型可以是 NilCons (value, thunk),其中 thunk是从单元到列表其余部分的函数。

我正在尝试编写一个函数 cross,它的功能与 List.combine 一样。不幸的是,我有语法错误。

open Sequence;;
let rec (cross : 'a Sequence.t -> 'b Sequence.t -> ('a * 'b) Sequence.t) = match seq1 with
Nil -> match seq2 with
Cons (value2, thunk2) -> Cons ((Nil, value2), function () -> (cross Nil (thunk2 ())))
| Cons (value1, thunk1) -> match seq2 with
Nil -> Cons ((value1, Nil), function() -> (cross Nil (thunk1 ())))
| Cons (value2, thunk2) -> Cons ((value1, value2), function() -> (cross (thunk1 ()) (thunk2 ())))

这会产生错误:

Error: Unbound value seq1

我做错了什么?

更新:

此类型检查,但不是我要查找的类型。

let rec cross (seq1 : 'a Sequence.t) (seq2 : 'b Sequence.t) : ('a * 'b) Sequence.t = match seq1 with
Nil -> match seq2 with
Cons (value2, thunk2) -> Cons ((Nil, value2), function () -> (cross Nil (thunk2 ())))
| Cons (value1, thunk1) -> match seq2 with
Nil -> Cons ((value1, Nil), function() -> (cross Nil (thunk1 ())))
| Cons (value2, thunk2) -> Cons ((value1, value2), function() -> (cross (thunk1 ()) (thunk2 ())))

val cross :
'a Sequence.t Sequence.t ->
'a Sequence.t Sequence.t -> ('a Sequence.t * 'a Sequence.t) Sequence.t =
<fun>

这不是我想要的十字架类型。我在寻找:

'a Sequence.t -> 'b Sequence.t -> ('a * 'b) Sequence.t

最佳答案

你要踢自己了...seq1 在哪里定义的?

let rec (cross : 'a Sequence.t -> 'b Sequence.t -> ('a * 'b) Sequence.t) =

你定义了交叉的类型,但你没有将变量绑定(bind)到任何东西(我想,你可以这么说)。

let rec cross (seq1:'a Sequence.t) (seq2:'a Sequence.t) :('a * 'b) Sequence.t =

编辑:

我觉得你的匹配很好,不匹配。在案例周围使用 begin ... end block ,我认为正在发生的事情(并且由于我没有序列,我无法验证)是您打算用于外部匹配的匹配案例正在应用于内部,匹配seq2。例如,

match x with
| 0 -> match y with
| 1 -> "x:0, y:1"
| 2 -> match y with
| 0 -> "y:0, x:2"

虽然在空间上看起来不错,但第二个匹配项 match y with| 绑定(bind)2 -> ... 匹配大小写。这是一个包含匹配案例的 being ... end 关键字的版本。第二个 begin ... end 不是必需的,但为了清楚起见,无论如何这样做可能是个好主意。

match x with 
| 0 -> begin match y with
| 1 -> "x:0, y:1" end
| 2 -> begin match y with
| 0 -> "y:0, x:2" end

关于syntax - Ocaml 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1645775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com