OCaml模式匹配
在ast.ml中,结构如下:OCaml模式匹配,ocaml,Ocaml,在ast.ml中,结构如下: type beantype = | Bool | Int | TLr of fieldsrec | TId of ident and fieldsrec = { fields : field list } and field = | FIe of (ident * beantype) 在printer.ml中,我使用它的方式如下: let rec print_bean fmt = function | Bool -
type beantype =
| Bool
| Int
| TLr of fieldsrec
| TId of ident
and fieldsrec = { fields : field list }
and field =
| FIe of (ident * beantype)
在printer.ml中,我使用它的方式如下:
let rec print_bean fmt = function
| Bool -> put fmt "%s" "bool"
| Int -> put fmt "%s" "int"
| TLr f -> put fmt "%s" "{"; print_fieldsrec fmt f ; put fmt "%s" "}"
| TId id -> put fmt "%s" id
and print_fieldsrec fmt = function
| f :: fe -> print_field fmt f; put fmt "%s" "," ; print_fieldsrec fmt fe
and print_field fmt = function
| FIe (id, beantype) -> put fmt "%s" id; put fmt "%s" ":"; print_bean fmt beantype
然而,该公司表示,不同的图案在印刷领域是匹配的
Error: This pattern matches values of type 'a list
but a pattern was expected which matches values of type
Bean_ast.fieldsrec
如何更改printer.ml?您似乎被类型
fieldsrec={fields:fieldlist}
弄糊涂了。您应该遵循Jeffrey的建议,使用|字段列表的字段
fieldsrec
不是一个列表,它是一个包含列表的记录,所以
print_fieldsrec fmt = function f :: fe -> ...
没有其名称所示的类型
此外,您还忘记了递归的
print\u fieldsrec
的基本情况,您似乎被类型fieldsrec={fields:field list}
搞糊涂了。您应该遵循Jeffrey的建议,使用|字段列表的字段
fieldsrec
不是一个列表,它是一个包含列表的记录,所以
print_fieldsrec fmt = function f :: fe -> ...
没有其名称所示的类型
此外,您还忘记了递归的
打印字段srec
TLr
包含一个字段srec
,而不是一个列表的基本情况。可能会将模式更改为|TLr{fields=f}
。我已经尝试过了,你是对的TLr
持有一个fieldsrec
,而不是一个列表。可能会将模式更改为|TLr{fields=f}
。我已经尝试过了,你是对的