Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Split a list when the predicate changes when comparing two neighbours *)
- let partition_by (predicate : 'a -> 'a -> bool) (xs : 'a list) : ('a list) list =
- let rec go (cur_x, cur_xs, acc : 'a * ('a list) * (('a list) list)) (xs : 'a list) : ('a list) list =
- match xs with
- | [] -> (List.rev ((List.rev cur_xs) :: acc))
- | x :: xs ->
- if predicate x cur_x
- then go (x, x :: cur_xs, acc) xs
- else go (x, [x], (List.rev cur_xs) :: acc) xs
- in
- match xs with
- | x :: xs -> go (x, [x], []) xs
- | [] -> []
Add Comment
Please, Sign In to add comment