2013-02-19 40 views
2

我寫了myPercolation.ml爲什麼ocamlc說我不匹配{}而我沒有?

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

我已經通讀了該行和所有代碼多次,但我仍然沒有看到該行中有任何不匹配的{}

任何人都可以幫忙嗎?

回答

3

表達式let {n; _; union} = p沒有很好地形成OCaml。我想你想要的是let {n; union} = p。處理記錄模式中你不關心的字段的方式更不用說了。

更新

由於rgrinberg指出,一個更好的方法來描述問題是_有顯示爲最後一個字段。這就是爲什麼編譯器希望以後看到}。將_作爲一個指標,表明您只是故意匹配記錄的一部分字段,這可能是一種很好的風格。實際上,您可以打開一個編譯器選項來檢查這一點。

更新2

不完整的記錄圖形警示警告9號,並且還以字母R.下面是如何使用[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 

命令行語法與編譯器相同。

+0

'{n; union}'會給我一個編譯器警告。如果你認爲編譯警告是不必要的,你可以使用'{n;聯盟; _}'但是下劃線必須是最後一個。 – rgrinberg 2013-02-19 00:58:51

+0

我沒有看到OCaml 4.00.0默認設置的任何警告。但是你是對的,如果它是最後一個字段,則允許'_'。 – 2013-02-19 01:07:48

+1

我認爲必須啓用警告。請參閱:http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual021.html#toc78 – rgrinberg 2013-02-19 01:15:49

4

另一個可能的錯誤:{n;_;_}應該是{n;_}只需要1個下劃線。在比賽聲明中將其想象爲_通配符。

相關問題