【发布时间】:2018-03-16 12:32:43
【问题描述】:
我有以下数据类型(请忽略这可能更简单的事实)
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
错误指的是以下括号:(ref {key; ( ... )})
【问题讨论】:
-
您可能应该将匹配表达式一分为二,这样第一个只处理
Null和Pos标签,第二个处理引用值 i> 在Pos的情况下,而不是匹配整个ref。
标签: pattern-matching ocaml record