gpt4 book ai didi

functional-programming - 为什么 ocamlc 说我不匹配 {} 而我没有?

转载 作者:行者123 更新时间:2023-12-02 07:37:23 24 4
gpt4 key购买 nike

我写了myPercolation.ml

open MyUnionFind

module type MyPercolationSig = sig
type percolation
val create_percolation : int -> percolation
val open_site : percolation -> int -> int -> unit
val is_open : percolation -> int -> int -> bool
val is_full : percolation -> int -> int -> bool
val can_percolates : percolation -> bool
end

module MyPercolation : MyPercolationSig = struct

exception IndexOutOfBounds;;

type percolation =
{n : int;
sites: bool array;
union : MyUnionFind.union_find};;

let create_percolation n =
{n = n; sites = Array.make (n*n) false; union = MyUnionFind.create_union (n*n)};;

let open_site p i j =
let {n;_;union} = p
in
if not (is_open p i j) then
begin
sites.(index_of n i j) <- true;
if i - 1 >= 1 && i - 1 <= n && is_open n (i-1) j then
MyUnionFind.union union (index_of n i j) (index_of n (i-1) j)
else if i + 1 >= 1 && i + 1 <= n && is_open n (i+1) j then
MyUnionFind.union union (index_of n i j) (index_of n (i+1) j)
else if j - 1 >= 1 && j - 1 <= n && is_open n i (j-1) then
MyUnionFind.union union (index_of n i j) (index_of n i (j-1))
else if j + 1 >= 1 && j + 1 <= n && is_open n i (j+1) then
MyUnionFind.union union (index_of n i j) (index_of n i (j+1))
end;;

let index_of n i j = n * (i - 1) + j;;

let is_open {n;sites;_} i j =
if i < 1 || i > n || j < 1 || j > n then
raise IndexOutOfBounds
else
sites.(index_of n i j);;

let is_full {n;_;union} i j =
let rec is_connected_top j' =
if j = 0 then false
else
if MyUnionFind.is_connected union (index_of n i j) (index_of n 0 j') then true
else is_connected_top (j'-1)
in is_connected_top n;;

let can_percolates p =
let {n;_;_} = p
in
let rec is_full_bottom j =
if j = 0 then false
else
if is_full p n j then true
else is_full_bottom (j-1)


end

请忽略包 MyUnionFind 包。它只是union-find算法的自制实现。

当我尝试编译 myPercolation.ml 时,出现这样的错误:

$ ocamlc -c myPercolation.ml
File "myPercolation.ml", line 25, characters 11-12:
Error: Syntax error: '}' expected
File "myPercolation.ml", line 25, characters 8-9:
Error: This '{' might be unmatched

我认为错误是在 let open_site p i j 的函数中谈论 let {n;_;union} = p

我已经多次阅读该行和所有代码,但我仍然没有看到该行中有任何不匹配的 {}

有人可以帮忙吗?

最佳答案

另一个可能的错误:{n;_;_} 应该是 {n;_} 只有 1 个下划线是必要的。可以把它想象成匹配语句中的 _ 通配符。

关于functional-programming - 为什么 ocamlc 说我不匹配 {} 而我没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947719/

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