【发布时间】:2011-06-14 03:32:19
【问题描述】:
假设我有一个整数类型列表 [1; 2; 3; 4; 5个; 6; 7; 8] 我想一次匹配前三个元素。没有嵌套的匹配语句有没有办法做到这一点?
例如,可以这样吗?
let rec f (x: int list) : (int list) =
begin match x with
| [] -> []
| [a; b; c]::rest -> (blah blah blah rest of the code here)
end
我可以使用长嵌套方法,即:
let rec f (x: int list) : (int list) =
begin match x with
| [] -> []
| h1::t1 ->
begin match t1 with
| [] -> []
| h2::t2 ->
begin match t2 with
| [] -> []
| t3:: h3 ->
(rest of the code here)
end
end
end
谢谢!
【问题讨论】:
标签: ocaml design-patterns elements matching