Guest User

Untitled

a guest
Nov 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. (* Split a list when the predicate changes when comparing two neighbours *)
  2.  
  3. let partition_by (predicate : 'a -> 'a -> bool) (xs : 'a list) : ('a list) list =
  4. let rec go (cur_x, cur_xs, acc : 'a * ('a list) * (('a list) list)) (xs : 'a list) : ('a list) list =
  5. match xs with
  6. | [] -> (List.rev ((List.rev cur_xs) :: acc))
  7. | x :: xs ->
  8. if predicate x cur_x
  9. then go (x, x :: cur_xs, acc) xs
  10. else go (x, [x], (List.rev cur_xs) :: acc) xs
  11. in
  12. match xs with
  13. | x :: xs -> go (x, [x], []) xs
  14. | [] -> []
Add Comment
Please, Sign In to add comment