[back]

Split By

Implement the function

val split_by : ('a -> bool) -> 'a list -> 'a list * 'a list

so that split_by f l is a pair of lists (lt, lf) where lt consists of those elements of l (in order, with repetitions) that satisfy the predicate f, and lf consists of those elements that do not satisfy the predicate f. Your solution must be tail recursive.

Examples

let _ = assert (split_by (fun x -> x > 3) [0;5;4;2;1;4] = ([5;4;4], [0;2;1]))
let _ = assert (split_by (fun x -> x = 2 || x = 4) [1;2;3;4;5;6]
                = ([2;4], [1;3;5;6]))