2016-04-11 22 views
0

in ast.ml ist die Struktur unter:OCaml Musterübereinstimmung

type beantype = 
    | Bool 
    | Int 
    | TLr of fieldsrec 
    | TId of ident 
and fieldsrec = { fields : field list } 
and field = 
    | FIe of (ident * beantype) 

in printer.ml, verwende ich es, wie unten:

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 

jedoch gesagt, es die verschiedene Musterübereinstimmung in print_fieldsrec

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

Wie kann ich die printer.ml ändern?

+2

'TLr' enthält eine' fields_rec', keine Liste. Ändere das Muster vielleicht auf '| TLr {Felder = f} '. – RichN

+0

Ich habe versucht, Sie haben Recht –

Antwort

0

Sie scheinen durch den Typ fieldsrec = { fields : field list } verwirrt zu sein. Sie sollten Jeffreys Rat befolgen, statt dessen | Fields of field list zu verwenden.

fieldsrec ist keine Liste, es ist ein Datensatz eine Liste auf, so

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

hat nicht die Art der Name schon sagt.

Auch Sie haben den Basisfall für die rekursive print_fieldsrec vergessen.