2017-10-05 16 views
0

I(これは方法より簡単である可能性があるという事実を無視してください)OCamlで一致するレコードをパターン化する方法は?

type tKey = Key of int;; 

type tBST = Null | Pos of node ref 
     and node = {mutable key : tKey; 
      mutable left : tBST; 
      mutable right : tBST};; 

私は私のパターンマッチングのように見えるこの機能では、次のエラーが生じていますが、右ではない、次のデータ型を持つ

let rec string_of_tree = function 
    Null -> "()" 
    | Pos (ref {key; left = Null; right = Null}) -> Printf.sprintf "(%s)" (string_of_root (root tree)) 
    | Pos (ref {key; left; right}) -> Printf.sprintf "(%s %s %s)" 
          (string_of_root (root tree)) 
          (string_of_tree (leftLeaf tree)) 
          (string_of_tree (rightLeaf tree));; 

Error: Syntax error: ')' expected 
Error: This '(' might be unmatched 

エラーは以下の括弧を参照します(参照{キー;(...)})

+0

:-)最初のものだけ 'NULL' WIH扱うようにあなたはおそらく、二つにあなたの一致式を分割する必要があるでしょう残念ながら{ contents = ... }

を使用することができます参照照合するために、 'Pos'タグで動作し、' Pos'タグで動作します。 – didierc

答えて

4

| Pos (ref { ... }) -> ... 

を交換してみてください、あなたはrefを使用することはできません。 refはコンストラクタではなく、実際には参照を作成する関数です。あなたは、このコードをさらに密

3

私はこの問題は、あなたが本当に含むレコードのためだけの砂糖である、refでパターンマッチを試みるということだと思います変更可contentsフィールド `。

は参照照合するには

| Pos { contents = { ... }} -> ... 
関連する問題