在OCaml中具有选项类型条目的记录上的模式匹配 [英] Pattern match on records with option type entries in OCaml
本文介绍了在OCaml中具有选项类型条目的记录上的模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,我已经定义了这样的记录类型:
basically I have defined a record type like this:
and exp = Bil_t.exp = {
var: var option;
binop: binop option;
load: load option;
store: store option;
cast: cast option;
inte: inte option;
let_exp: let_exp option
}
我正在考虑使用模式匹配来处理它,就像这样:
And I am thinking to use a pattern match to process it, something like this:
match rexp with
| {None;binop;None;None;None;None;None} -> trans_binop @@ strip binop
| {var;None;None;None;None;None;None} -> BU.inte_to_string @@ strip @@ mark inte
| _ -> failwith "undefined"
很抱歉上面的代码混乱.所以基本上我编译了上面的代码,并且得到了错误:
Sorry for the messy code above. So basically I compile the above code, and I get the error:
Error: Syntax error
任何人都可以在这方面给我一些帮助..我只是不知道这里出了什么问题...
Could anyone give me some help on this.. I just don't know what is wrong here...
推荐答案
记录模式需要包含字段名称.
Record patterns need to include the field names.
type exp = {
var: int option;
binop: int option;
load: int option;
store: int option;
cast: int option;
inte: int option;
let_exp: int option
}
let f rexp =
match rexp with
| { var = None; binop = Some b; load = None; store = None;
cast = None; inte = None; let_exp = None
} -> b
| _ -> failwith "undefined"
示例:
# let r = { var = None; binop = Some 14; load = None; store = None;
cast = None; inte = None; let_exp = None };;
val r : exp =
{var = None; binop = Some 14; load = None; store = None; cast = None;
inte = None; let_exp = None}
# f r;;
- : int = 14
这篇关于在OCaml中具有选项类型条目的记录上的模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文