Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 42 lines (36 sloc) 1.35 kb
5476503 First import (caml2html post 1.3.0)
mjambon authored
1 (* The type of elements *)
2 type kind = Start | Stop | Other
3
4 (* Recursively remove consecutive start/stop pairs *)
5 let rec remove_matches = function
6 (Start, _) as start :: l ->
7 (match remove_matches l with
8 (Stop, _) :: rest -> rest
9 | rest -> start :: rest)
10 | (Stop, _) as stop :: l -> stop :: remove_matches l
11 | (Other, _) as x :: l -> x :: remove_matches l
12 | [] -> []
13
14 (* Annotate innermost start/stop pairs *)
15 let rec annotate_innermost f = function
16 (Start, a) :: l ->
17 let other, next_stop = find_stop f [] l in
18 (match next_stop with
19 (Stop, b) :: rest ->
20 (Start, f true a) :: other @ (Stop, f true b) ::
21 annotate_innermost f rest
22 | (Start, _) :: _ -> other @ annotate_innermost f next_stop
23 | (Other, _) :: _ -> assert false
24 | [] -> other)
25 | (tag, x) :: l -> (tag, f false x) :: annotate_innermost f l
26 | [] -> []
27
28 and find_stop f accu = function
29 (Other, x) :: l -> find_stop f ((Other, f false x) :: accu) l
30 | l -> (List.rev accu), l
31
32
33 let start x = (Start, x);;
34 let stop x = (Stop, x);;
35 let other x = (Other, x);;
36 let annotate b x = (x, b);;
37 let l1, l2 =
38 [ stop 1; stop 2; start 3; start 4; start 5; stop 5; start 6 ],
39 [ stop 6; start 7; stop 7; stop 4; stop 3; start 8; stop 8; start 9 ];;
40 let l = remove_matches (l1 @ [other 10] @ l2);;
41 annotate_innermost annotate l;;
Something went wrong with that request. Please try again.