【发布时间】:2013-06-14 22:50:27
【问题描述】:
我正在尝试使用模式匹配来编写一个计算器应用程序。
两大类定义如下:
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();;
但是,如果我有这样的状态:
let initial_state = { lcd=0; lka=Equals; loa=Equals; vpr=0 } ;;
然后当我调用函数时:
print_state initial_state;;
会有编译错误。任何人都可以说出编译失败的原因。感谢您的建议。
Error: Syntax error
unexpected token "("
【问题讨论】:
-
但是为什么要对记录进行模式匹配?要从
initial_state中取出lcd,请使用initial_state.lcd。
标签: types pattern-matching ocaml record