File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed
Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -364,8 +364,10 @@ let init n f =
364364
365365let rec rev_append l1 l2 =
366366 match l1 with
367- [] -> l2
368- | a :: l -> rev_append l (a :: l2)
367+ | [] -> l2
368+ | [a0] -> a0::l2 (* single element is common *)
369+ | [a0 ; a1] -> a1 :: a0 :: l2
370+ | a0 ::a1 ::a2 ::rest -> rev_append rest (a2::a1::a0::l2)
369371
370372let rev l = rev_append l []
371373
@@ -442,7 +444,16 @@ let rec rev_map_append l1 l2 f =
442444let rec flat_map_aux f acc append lx =
443445 match lx with
444446 | [] -> rev_append acc append
445- | a0 ::rest -> flat_map_aux f (rev_append (f a0) acc ) append rest
447+ | a0 ::rest ->
448+ let new_acc =
449+ match f a0 with
450+ | [] -> acc
451+ | [a0] -> a0::acc
452+ | [a0;a1] -> a1::a0::acc
453+ | a0 ::a1 ::a2 ::rest ->
454+ rev_append rest (a2::a1::a0::acc)
455+ in
456+ flat_map_aux f new_acc append rest
446457
447458let flat_map lx f =
448459 flat_map_aux f [] [] lx
You can’t perform that action at this time.
0 commit comments