当前位置: 动力学知识库 > 问答 > 编程问答 >

functional programming - Why ocamlc says I mismatched {} while I don't?

问题描述:

I have written 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


Please ignore the package MyUnionFind package. It is just a homemade implementation for union-find algorithm.

when I try to compile the myPercolation.ml, I got such an error:

$ 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

I think the error is talking about let {n;_;union} = p in function of let open_site p i j.

I have read through that line and all code many times, but I still don't see any mismatched {} in that line.

can anyone help please?

网友答案:

The expression let {n; _; union} = p is not well formed OCaml. I think what you want is let {n; union} = p. The way to handle fields you don't care about in a record pattern is not to mention them.

Update:

As rgrinberg points out, a much better way to describe the problem is that the _ has to appear as the last field. That's why the compiler is expecting to see } afterward. It might be good style to include the _ as an indicator that you're purposely matching only a subset of the fields of the record. You can, in fact, turn on a compiler option that checks for this.

Update 2:

The warning for incomplete record patterns is warning number 9, and also is associated with the letter R. Here's how to use R:

$ ocaml -w +R
        OCaml version 4.00.0

# type r = { a: int; b: char };;
type r = { a : int; b : char; }
# let {a} = {a=3; b='x'} in a;;
Warning 9: the following labels are not bound in this record pattern:
b
Either bind these labels explicitly or add '; _' to the pattern.
- : int = 3

The command-line syntax is the same for the compiler.

网友答案:

Another possible error: {n;_;_} should be {n;_} Only 1 underscore is necessary. Think of it like the _ wildcard in a match statement.

分享给朋友:
您可能感兴趣的文章:
随机阅读: