OCaml模式匹配

时间:2016-04-11 16:15:47

标签: ocaml

在ast.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
在printer.ml中的

,我使用它如下:

Error: This pattern matches values of type 'a list
   but a pattern was expected which matches values of type
     Bean_ast.fieldsrec

然而它说print_fieldsrec中的不同模式匹配

node

如何更改printer.ml?

1 个答案:

答案 0 :(得分:0)

您似乎对类型fieldsrec = { fields : field list }感到困惑。您应该遵循Jeffrey关于使用| Fields of field list的建议。

fieldsrec不是列表,它是包含列表的记录,所以

print_fieldsrec fmt = function f :: fe -> ...

没有其名称所暗示的类型。

您也忘记了递归print_fieldsrec的基本情况。

相关问题