Record-Typ pattern matching in Ocaml

Ich versuche, mithilfe der Mustererkennung wird ein zu schreiben ein-Rechner-Anwendung.

Zwei Haupttypen wie folgt definiert:

type key = Plus | Minus | Multi | Div | Equals | Digit of int;;

type state = {
    lcd: int; (* last computation done *)
    lka: key; (* last key actived *)
    loa: key; (* last operation actived *)
    vpr: int (* value print on the screen *)
};;

let print_state s =
    match s with
     state (a,_,_,d) -> print_int a; //Here has the compile error
                print_newline();
                print_int d;
                    print_newline();;

Allerdings, wenn ich einem Staat wie:

let initial_state = { lcd=0; lka=Equals; loa=Equals; vpr=0 } ;; 

Dann, wenn ich zum aufrufen der Funktion:

print_state initial_state;;

Wird es die compile-Fehler. Jeder kann sagen, was der Grund für misslungene Zusammenstellung. Vielen Dank im adv.

Error: Syntax error
unexpected token "("
  • Aber warum sind Sie pattern-matching auf Aufzeichnungen? Um die lcd aus initial_state verwenden initial_state.lcd.
InformationsquelleAutor yjasrc | 2013-06-18
Schreibe einen Kommentar