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};; allow create_percolation n = {n = n; sites = array.make (n*n) false; union = myunionfind.create_union (n*n)};; allow open_site p j = allow {n;_;union} = p in if not (is_open p j) begin sites.(index_of n j) <- true; if - 1 >= 1 && - 1 <= n && is_open n (i-1) j myunionfind.union union (index_of n j) (index_of n (i-1) j) else if + 1 >= 1 && + 1 <= n && is_open n (i+1) j myunionfind.union union (index_of n j) (index_of n (i+1) j) else if j - 1 >= 1 && j - 1 <= n && is_open n (j-1) myunionfind.union union (index_of n j) (index_of n (j-1)) else if j + 1 >= 1 && j + 1 <= n && is_open n (j+1) myunionfind.union union (index_of n j) (index_of n (j+1)) end;; allow index_of n j = n * (i - 1) + j;; allow is_open {n;sites;_} j = if < 1 || > n || j < 1 || j > n raise indexoutofbounds else sites.(index_of n j);; allow is_full {n;_;union} j = allow rec is_connected_top j' = if j = 0 false else if myunionfind.is_connected union (index_of n j) (index_of n 0 j') true else is_connected_top (j'-1) in is_connected_top n;; allow can_percolates p = allow {n;_;_} = p in allow rec is_full_bottom j = if j = 0 false else if is_full p n j true else is_full_bottom (j-1) end please ignore bundle myunionfind package. homemade implementation union-find algorithm.
when seek compile mypercolation.ml, got such 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: '{' might unmatched i think error talking let {n;_;union} = p in function of let open_site p j.
i have read through line , code many times, still don't see mismatched {} in line.
can help please?
the look let {n; _; union} = p not formed ocaml. think want let {n; union} = p. way handle fields don't care in record pattern not mention them.
update:
as rgrinberg points out, much improve way describe problem _ has appear lastly field. that's why compiler expecting see } afterward. might style include _ indicator you're purposely matching subset of fields of record. can, in fact, turn on compiler alternative checks this.
update 2:
the warning incomplete record patterns warning number 9, , associated letter r. here's how utilize r:
$ ocaml -w +r ocaml version 4.00.0 # type r = { a: int; b: char };; type r = { : int; b : char; } # allow {a} = {a=3; b='x'} in a;; warning 9: next labels not bound in record pattern: b either bind these labels explicitly or add together '; _' pattern. - : int = 3 the command-line syntax same compiler.
functional-programming ocaml
No comments:
Post a Comment