[back]

Grouping

A list of integers l is valid if it satisfies the following properties:

Intuitively, a list is valid if it is made of nonempty sublists of same-sign integers, switching between positive and negative, separated by zeros.

The grouping of a valid list of integers is a list of integer lists which groups together the adjacent nonzero entries and drops the zero entries.

Implement the function

val group : int list -> int list list option

so that group l is the Some gs where gs is a grouping of l if l is valid, and is None if l is invalid.

Examples

let _ = assert (group [1;2;3;0;-1;-2;-3;0;1] = Some [[1;2;3];[-1;-2;-3];[1]])
let _ = assert (group [1;2;3;0;1;2;3;0;1] = None)
let _ = assert (group [0;1;2;3] = None)
let _ = assert (group [1;0;0;-1] = None)